Results 1 to 3 of 3
Thread: Help with NetBeans 5 GUI builder
- 07-04-2007, 07:04 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
Help with NetBeans 5 GUI builder
Just getting into building GUI's with the NetBeans 5 GUI builder and encountered an issue that has stopped me dead in my tracks...
A short discription of the interface:
1 JFrame
3 JPanel (setupPanel, dataPanel, gameSheet)
and a bunch of buttons, text feilds and all that jazz (I removed most of them though so that I can focus on my issue)
The issue:
I would like to draw to the JPanel named gameSheet...nothing too fancy...just some Rectangles.
I have been able to draw to a single JPanel writing the code from scratch, but have no idea what to do with all this code that has been generated for me..
The answer is right in front of me, I just can see it...
Can someone show me how to draw to a JPanel(gameSheet) using the following code?
Any help would be greatly appreciated. This is starting to drive me nuts.
ThanksJava Code:package my.hanoi; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JLabel; import javax.swing.JPanel; public class hanoiWin extends javax.swing.JFrame { /** Creates new form hanoiWin */ public hanoiWin() { initComponents(); //gameSheet.setBackground(Color.blue); } /** 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. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { setupPanel = new javax.swing.JPanel(); jTextField1 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); dataPanel = new javax.swing.JPanel(); gameSheet = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setupPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Setup")); jTextField1.setToolTipText("Enter the # of Disks"); jTextField1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { jTextField1KeyReleased(evt); } }); jLabel1.setText("# of Disks:"); org.jdesktop.layout.GroupLayout setupPanelLayout = new org.jdesktop.layout.GroupLayout(setupPanel); setupPanel.setLayout(setupPanelLayout); setupPanelLayout.setHorizontalGroup( setupPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(setupPanelLayout.createSequentialGroup() .addContainerGap() .add(jLabel1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(43, Short.MAX_VALUE)) ); setupPanelLayout.setVerticalGroup( setupPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(setupPanelLayout.createSequentialGroup() .addContainerGap() .add(setupPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel1) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(276, Short.MAX_VALUE)) ); dataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Center")); org.jdesktop.layout.GroupLayout dataPanelLayout = new org.jdesktop.layout.GroupLayout(dataPanel); dataPanel.setLayout(dataPanelLayout); dataPanelLayout.setHorizontalGroup( dataPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 707, Short.MAX_VALUE) ); dataPanelLayout.setVerticalGroup( dataPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 198, Short.MAX_VALUE) ); gameSheet.setBackground(Color.ORANGE); gameSheet.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { gameSheetMousePressed(evt); } }); org.jdesktop.layout.GroupLayout gameSheetLayout = new org.jdesktop.layout.GroupLayout(gameSheet); gameSheet.setLayout(gameSheetLayout); gameSheetLayout.setHorizontalGroup( gameSheetLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 550, Short.MAX_VALUE) ); gameSheetLayout.setVerticalGroup( gameSheetLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 333, Short.MAX_VALUE) ); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(dataPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(layout.createSequentialGroup() .add(gameSheet, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(setupPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(gameSheet, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(setupPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(dataPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> private void gameSheetMousePressed(java.awt.event.MouseEvent evt) { gameSheet.setBackground(Color.black); } /** *eventually this field will hold the number of rectangles to draw *this event will dynamically generate the rectangles as the # is typed *currently this is here to test */ private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) { Rectangle baybee = new Rectangle(50, 50, 50, 50); //gameSheet.paintMe(); //gameSheet.paint(g); } /** some attemps public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; Rectangle rect = new Rectangle(50, 50, 50, 50); g2.drawRect(50, 50, 50, 50); } */ /** public void paint(Graphics g){ g.drawRect(20,20, 50, 50); g.fillRect(30, 60, 40, 20); } */ /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new hanoiWin().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel dataPanel; private javax.swing.JPanel gameSheet; private javax.swing.JLabel jLabel1; private javax.swing.JTextField jTextField1; private javax.swing.JPanel setupPanel; // End of variables declaration }
Daniel:o
- 07-04-2007, 07:07 AM #2
Member
- Join Date
- Jun 2007
- Posts
- 92
- Rep Power
- 0
I had this same problem, and spent a long time scouring the web for an answer. What you have to do is create a custom Swing component.
- Create a new Class that 'extends JPanel' - call it GameSheetPanel. Put your drawing code inside this class in a method called 'paintComponent'
- Select the gameSheet component in the GUI designer, and go to the properties window.
- Select 'Code' -> 'Custom Create Code'
- In the popup window put:
If you view your source code now, under initComponents you'll see gameSheet being created as an instance of GameSheetPanelJava Code:new GameSheetPanel();
5. Update your event listeners to call gameSheet.repaint(); when an event occurs
Greetings
Marcus:cool:
- 07-04-2007, 07:08 AM #3
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
Similar Threads
-
help with netBeans
By the godfather01 in forum NetBeansReplies: 11Last Post: 02-01-2009, 04:48 PM -
Ant outside of netbeans
By ProbablyX in forum NetBeansReplies: 0Last Post: 02-14-2008, 07:06 PM -
NetBeans 5.5
By Ry4n in forum NetBeansReplies: 3Last Post: 01-26-2008, 06:09 AM -
NetBeans 5.5.1 Help.
By padutch2 in forum New To JavaReplies: 6Last Post: 12-03-2007, 03:01 AM -
About using netbeans
By yuchuang in forum NetBeansReplies: 3Last Post: 05-27-2007, 08:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks