Results 1 to 6 of 6
  1. #1
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Question NetBeans GUI Builder + constantly updating information

    Hello, I have gotten myself into a somewhat tricky situation that i cannot for the life of me seem to figure out.

    I am trying to write a program that will read the coordinates of the mouse on the screen (NOT just a panel), and output the coordinates and RGB values into a small window.

    I can easily enough get the pixels/RGB values, but the problem lies in updating the screen. Currently I have a button which starts the program. however this button calls a method which has an infinite while loop in it, which is causing problems (freezes the program). I used the NetBeans GUI Builder to create the GUI. Here is the code:

    Java Code:
    package PixelPosAndColour;
    
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.MouseInfo;
    import java.awt.Robot;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Dustin
     */
    public class PixelInfo extends javax.swing.JFrame {
    
        /** Creates new form PixelPosandColourFromMouseCoordinates */
        public PixelInfo() throws AWTException, InterruptedException {
    	initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jX = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jY = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jGreen = new javax.swing.JTextField();
            jRed = new javax.swing.JTextField();
            jBlue = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("X:");
    
            jX.setEditable(false);
            jX.setText("0");
    
            jLabel2.setText("Y:");
    
            jY.setEditable(false);
            jY.setText("0");
    
            jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
            jLabel3.setText("Colour");
    
            jLabel4.setText("Red:");
    
            jLabel5.setText("Green:");
    
            jLabel6.setText("Blue:");
    
            jGreen.setEditable(false);
            jGreen.setText("0");
    
            jRed.setEditable(false);
            jRed.setText("0");
    
            jBlue.setEditable(false);
            jBlue.setText("0");
    
            jButton1.setText("Go");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Stop");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jX, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel2))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel6))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jBlue, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                                        .addComponent(jGreen, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                                        .addComponent(jRed, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jY, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addComponent(jLabel3))
                    .addContainerGap())
            );
    
            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jX, jY});
    
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jGreen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBlue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBlue, jGreen, jRed});
    
            pack();
        }// </editor-fold>
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    	// TODO add your handling code here:
    	on = false;
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    	try {
    	    // TODO add your handling code here:
    	    on = true;
    	    Execute();
    	} catch (InterruptedException ex) {
    	    Logger.getLogger(PixelInfo.class.getName()).log(Level.SEVERE, null, ex);
    	}
    }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) throws InterruptedException, AWTException {
    	java.awt.EventQueue.invokeLater(new Runnable() {
    
    	    public void run() {
    		try {
    
    		    PixelInfo doStuff = new PixelInfo();
    		    doStuff.setVisible(true);
    
    //			doStuff.Execute();  //<-- this failed
    
    		} catch (AWTException ex) {
    		    Logger.getLogger(PixelInfo.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (InterruptedException ex) {
    		}
    	    }
    	});
    
        }
        //<editor-fold desc="Generated Variables (for GUI)">
        // Variables declaration - do not modify
        private javax.swing.JTextField jBlue;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JTextField jGreen;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JTextField jRed;
        private javax.swing.JTextField jX;
        private javax.swing.JTextField jY;
        // End of variables declaration
        //</editor-fold>
        int X = 0;
        int Y = 0;
        int red = 0;
        int green = 0;
        int blue = 0;
        Robot robot = new Robot(); // create robot and color objects.
        Color pixel;
        boolean on = false;
    
    
        public void getVariables() {
    	try {
    	    X = MouseInfo.getPointerInfo().getLocation().x; // get the mouses coordinates
    	    Y = MouseInfo.getPointerInfo().getLocation().y;
    	    pixel = robot.getPixelColor(X, Y); // set the pixel to get RGB data from to the mouses coordinates
    	    red = pixel.getRed(); // get colour data.
    	    green = pixel.getGreen();
    	    blue = pixel.getBlue();
    	} catch (Exception e) {
    	}
        }
    
        public void Update() {
    
    	getVariables(); // call the previous method
    
    	jX.setText(Integer.toString(X)); // print all the gathered variables to
    	jY.setText(Integer.toString(Y)); // the GUI
    	jRed.setText(Integer.toString(red));
    	jGreen.setText(Integer.toString(green));
    	jBlue.setText(Integer.toString(blue));
        }
    
        public void Execute() throws InterruptedException {
    	if (on) {
    	    while (true) {  // <-- this method freezes the program.
    		Update();  // (I realize the 'stop' button would never work...)
    		Thread.sleep(100);
    	    }
    	}
        }
    
    }
    Here is what the GUI looks like, (the 'manage attachments' button is not working for me):

    PixelInfoGUI.png

    I have a feeling I may need to put the 'Execute()' method in a thread...so it may run at the same time as the event listeners which are active in the GUI. However, my knowledge on threads is Very rudimentary...any help would be greatly appreciated. Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    method which has an infinite while loop
    Why do you have the loop there? Why not call the updating code when there is a change to be made to the GUI.
    Last edited by Norm; 07-07-2010 at 08:12 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with Norm (what else is new) -- if you're coding a GUI you are doing event driven programming, and there's no reason for that infinite loop or a background thread. Use a Swing Timer instead.

  4. #4
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks a lot guys :D.
    I used your recommendation of swing timers Fubarable. It worked AMAZINGLY!
    I had no idea Swing Timers even existed...I never learned about them in school :(

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome! My first choice would have been to add some kind of MouseListener to the Robot, but I can't see how to do that -- perhaps Rob, Darryl or one of the other Swing gurus can point out a way.

    And best of luck with your project!

  6. #6
    Inventor22 is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    That's what I tried to do before using your idea, however I could only get the mouseListener to work Inside of the programs GUI, not just anywhere on the screen.

Similar Threads

  1. NetBeans losing package information when crashing
    By Kethas in forum New To Java
    Replies: 4
    Last Post: 03-03-2010, 05:38 AM
  2. Builder
    By AgentApe in forum New To Java
    Replies: 4
    Last Post: 12-02-2009, 09:33 PM
  3. Replies: 0
    Last Post: 03-26-2009, 04:07 PM
  4. Eclipse GUI Builder
    By elebal in forum Eclipse
    Replies: 1
    Last Post: 10-23-2008, 11:38 AM
  5. Help with NetBeans 5 GUI builder
    By Daniel in forum NetBeans
    Replies: 2
    Last Post: 07-04-2007, 07:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •