Results 1 to 6 of 6
- 07-07-2010, 08:01 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
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:
Here is what the GUI looks like, (the 'manage attachments' button is not working for me):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); } } } }
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.
- 07-07-2010, 08:09 PM #2
Why do you have the loop there? Why not call the updating code when there is a change to be made to the GUI.method which has an infinite while loopLast edited by Norm; 07-07-2010 at 08:12 PM.
-
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.
- 07-07-2010, 11:07 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
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 :(
-
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!
- 07-07-2010, 11:27 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
NetBeans losing package information when crashing
By Kethas in forum New To JavaReplies: 4Last Post: 03-03-2010, 05:38 AM -
Builder
By AgentApe in forum New To JavaReplies: 4Last Post: 12-02-2009, 09:33 PM -
Netbeans Mobility Pack 6.0 and the Game Builder tool
By gissah in forum New To JavaReplies: 0Last Post: 03-26-2009, 04:07 PM -
Eclipse GUI Builder
By elebal in forum EclipseReplies: 1Last Post: 10-23-2008, 11:38 AM -
Help with NetBeans 5 GUI builder
By Daniel in forum NetBeansReplies: 2Last Post: 07-04-2007, 07:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks