Results 1 to 3 of 3
  1. #1
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default 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.

    Java 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                   
        
    }
    Thanks

    Daniel:o

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    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.

    1. Create a new Class that 'extends JPanel' - call it GameSheetPanel. Put your drawing code inside this class in a method called 'paintComponent'
    2. Select the gameSheet component in the GUI designer, and go to the properties window.
    3. Select 'Code' -> 'Custom Create Code'
    4. In the popup window put:


    Java Code:
    new GameSheetPanel();
    If you view your source code now, under initComponents you'll see gameSheet being created as an instance of GameSheetPanel
    5. Update your event listeners to call gameSheet.repaint(); when an event occurs

    Greetings

    Marcus:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    I be able to draw it on the panel as you described but how could I be able to change my drawing from the mouse click event in the JFrame please if you have any idea.

    Greetings

    Felissa:p

Similar Threads

  1. help with netBeans
    By the godfather01 in forum NetBeans
    Replies: 11
    Last Post: 02-01-2009, 05:48 PM
  2. Ant outside of netbeans
    By ProbablyX in forum NetBeans
    Replies: 0
    Last Post: 02-14-2008, 08:06 PM
  3. NetBeans 5.5
    By Ry4n in forum NetBeans
    Replies: 3
    Last Post: 01-26-2008, 07:09 AM
  4. NetBeans 5.5.1 Help.
    By padutch2 in forum New To Java
    Replies: 6
    Last Post: 12-03-2007, 04:01 AM
  5. About using netbeans
    By yuchuang in forum NetBeans
    Replies: 3
    Last Post: 05-27-2007, 09:06 PM

Posting Permissions

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