Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2006, 02:36 AM   PM User | #1
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
Java NetBeans 5 - GUI builder - Drawing to JPanel

Just getting into building GUI's with the NetBeans 5 GUI builder and encountered an issue that has stopped me dead in my tracks...

Everything was going smooth...

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.

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                   
    
}
Yak
Yakisoba is offline   Reply With Quote
Old 11-25-2006, 10:26 AM   PM User | #2
porjo
New to the CF scene

 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
porjo is an unknown quantity at this point
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:
Quote:
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

I hope that helps.
porjo is offline   Reply With Quote
Old 06-16-2007, 11:32 AM   PM User | #3
teklu
New to the CF scene

 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
teklu is an unknown quantity at this point
porjo can you help me with the same problem

Quote:
Originally Posted by porjo View Post
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:

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

I hope that helps.
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
thanks for your support
teklu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:07 PM.


Advertisement
Log in to turn off these ads.