Results 1 to 17 of 17
  1. #1
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default Draw shapes by pressing a button

    I have the following code shown below, which will be able to draw a shape once clicking on a button. For example, if you press the "Draw Rectangle" button a rectangle should be drawn and so forth.

    I have reached to the point where to determine the button pressed, but UNABLE to add the drawing functionality after that. How can I draw a shape after pressing a button?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class DisposeShapes{
    	
    	JLabel label = new JLabel();
    	JButton addRectangle;
    	JButton addSquare;
    	JButton addCircle;
    	Rectangle rectangle;
    	Graphics2D g2;
    	JPanel panel;
    	
    	public void paintComponent(Graphics g)
    	{
    		g2 = (Graphics2D)g;
    		rectangle = new Rectangle(100,100,100,100);
    		
    	}
    	
    	public DisposeShapes()
    	{
    	    JFrame frame = new JFrame("Dispose shapes");
    		panel = new JPanel();
    		addRectangle = new JButton("Add Rectangle");
    		addRectangle.setSize(50, 100);
    		addRectangle.addActionListener(new ButtonListener());
    		addSquare = new JButton("Add Square");
    		addSquare.setSize(50,100);
    		addSquare.addActionListener(new ButtonListener());
    		addCircle = new JButton("Add Circle");
    		addCircle.setSize(50,100);
    		addCircle.addActionListener(new ButtonListener());
    		panel.setLayout(new FlowLayout());
    		panel.add(addRectangle);
    		panel.add(addSquare);
    		panel.add(addCircle);
    		panel.add(label);
    		frame.getContentPane().add(panel);
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    	}
    	
    		
    		public class ButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(e.getSource() == addRectangle)
    				{
    					
    				}
    				
    				else if (e.getSource() == addSquare)
    				{
    					
    				}
    				
    				else if(e.getSource() == addCircle)
    				{
    					
    				}
    				
    			}
    		}
    	
    		
    		public static void main(String[] args)
    		{
    			DisposeShapes disposeShapes = new DisposeShapes();
    		}
    }
    Thanks.

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

    Default

    First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel.

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

    Default

    For e.g.,
    Java Code:
    public class DisposeShapes {
    
      private boolean drawRectangle = false;
      private boolean drawCircle = false;
      JLabel label = new JLabel();
      JButton addRectangle;
      JButton addSquare;
      JButton addCircle;
      Rectangle rectangle;
      Graphics2D g2;
      JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          myPaint(g); // to off load this into the body of the class
        }
      };
    
      // offloaded to here
      private void myPaint(Graphics g) {
        if (drawRectangle) {
          g.drawRect(10, 10, 100, 100);
        }
        if (drawCircle) {
          g.drawOval(10, 10, 100, 100);
        }
        //.... etc...
    
      }
    
      public DisposeShapes() {
        JFrame frame = new JFrame("Dispose shapes");
        // panel = new JPanel();  // don't need this any more

  4. #4
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    Thank you so much for your thorough reply.

    The code is now as follows, but still NOT working, and have an error in: super.paintComponent(g);

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class DisposeShapes{
    	
    	boolean drawRectangle = false;
    	boolean drawCircle = false;
    	boolean drawSquare = false;
    	JLabel label = new JLabel();
    	JButton addRectangle;
    	JButton addSquare;
    	JButton addCircle;
    	Rectangle rectangle;
    	Graphics2D g2;
    	JPanel panel = new JPanel();
    	
    	public void paintComponent(Graphics g)
    	{
    		//super.paintComponent(g);
    		myPaint(g);
    		
    	}
    	
    	public void myPaint(Graphics g)
    	{
    		if (drawRectangle)
    		{
    			g.drawRect(10,10,100,100);
    		}
    		
    		if (drawCircle)
    		{
    			g.drawOval(10,10, 100, 100);
    		}
    		
    		if (drawSquare)
    		{
    			
    		}
    	}
    	
    	public DisposeShapes()
    	{
    	    JFrame frame = new JFrame("Dispose shapes");
    		addRectangle = new JButton("Add Rectangle");
    		addRectangle.setSize(50, 100);
    		addRectangle.addActionListener(new ButtonListener());
    		addSquare = new JButton("Add Square");
    		addSquare.setSize(50,100);
    		addSquare.addActionListener(new ButtonListener());
    		addCircle = new JButton("Add Circle");
    		addCircle.setSize(50,100);
    		addCircle.addActionListener(new ButtonListener());
    		panel.setLayout(new FlowLayout());
    		panel.add(addRectangle);
    		panel.add(addSquare);
    		panel.add(addCircle);
    		panel.add(label);
    		frame.getContentPane().add(panel);
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    	}
    	
    		
    		public class ButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(e.getSource() == addRectangle)
    				{
    					drawRectangle = true;
    				}
    				
    				else if (e.getSource() == addSquare)
    				{
    					
    				}
    				
    				else if(e.getSource() == addCircle)
    				{
    					
    				}
    				
    			}
    		}
    	
    		
    		public static void main(String[] args)
    		{
    			DisposeShapes disposeShapes = new DisposeShapes();
    		}
    }
    Thanks.
    Last edited by SWEngineer; 03-13-2010 at 02:49 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by SWEngineer View Post
    Thank you so much for your thorough reply.

    The code is now as follows, but still NOT working, and have an error in: super.paintComponent(g);
    I don't believe the compiler (or the JVM itself) said nothing but "error". Please don't make us guess.

    kind regards,

    Jos

  6. #6
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    The error says:

    The method paintComponent(Grpahics) is undefined for the type Object

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

    Default

    Again as noted above, you need to have paintComponent within a class that extends JPanel. Please look more carefully at my example to see what I mean.

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

    Default

    Cross-post: http://forums.sun.com/thread.jspa?messageID=10950549

    OP, please read up on the proper etiquette for notifying us about cross-posts here: JavaRanch - Be Forthright When Cross Posting To Other Sites

    Your cooperation on this would be greatly appreciated (and the converse is true!).

    Best of luck.
    Last edited by Fubarable; 03-13-2010 at 03:31 PM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Another cross post:...
    hm, and he never replied to my query regarding cross-posting, so perhaps you're right after all Darryl. Another one for the list.

  11. #11
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    I cross posted to get answers from different people.

    What is the matter with that? I really don't know, especially that I will be walking with each answer I get...

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

    Default

    Quote Originally Posted by SWEngineer View Post
    I cross posted to get answers from different people.

    What is the matter with that? I really don't know, especially that I will be walking with each answer I get...
    Please read the link provided to understand the issue. It is from JavaRanch, one site that you've used. Feel free to ask if anything about it is confusing.

  13. #13
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    I will keep this post at this forum. And, will just follow up in this forum. Is that OK?

    Can we complete our discussion? And, HERE ONLY?

    Thanks.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    And how are the readers on the other forums supposed to know that you've abandoned the threads there?

    db

  15. #15
    SWEngineer is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    I don't know. But, I will not make any reply there.

  16. #16
    nnur is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    I've read this post; though it is a cross-post, I still get benefit from the logic of the code. The code written above (+ correction from Fubarable) doesn't get error, but it draws nothing. But when modifying it, I still get a trouble.
    I'm using NetBeans, and if I click button "bOval", it says: Error on java.lang.NullPointer on code that calls mypaint(g).
    I've switched between Graphics and Graphics2D and do other alternatives, but it still get errors. Would you like to help me? THANK YOU.
    And here's some of my code:

    ================================
    public class ToolbarKanvas extends javax.swing.JFrame {

    boolean pilihOval = false;
    Graphics g;

    JPanel KanvasDasar = new JPanel()
    {
    @Override
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    myPaint(g);
    }
    };

    /** Creates new form ToolbarKanvas */
    public ToolbarKanvas()
    {
    initComponents();
    this.getContentPane().add(KanvasDasar, BorderLayout.CENTER);
    this.pack();
    }

    // untuk menggambar geometri
    private void myPaint(Graphics g)
    {
    if (pilihOval)
    {
    g.drawOval(5,5,40,40);
    }
    }

    //panggil u/ gambar.
    private void bOvalMouseClicked(java.awt.event.MouseEvent evt)
    { this.pilihOval = true;
    myPaint(g);
    }

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

    Default

    Yours is a new question by a different user and thus deserves its own thread. I recommend that you start your own thread, post a link in it to this one (or pseudo-link if the software still doesn't let newbies link), and use code tags when you post your formatted code so it will retain its formatting (see link in my signature). Much luck.

    Locking old thread.

    Also your class has no business with a Graphics class field as this is not how Graphics is used. Start a new thread and we can discuss this further.
    Last edited by Fubarable; 05-14-2010 at 05:33 AM.

Similar Threads

  1. Replies: 6
    Last Post: 12-23-2009, 09:10 AM
  2. Efficient Looping to Draw Shapes
    By clam in forum New To Java
    Replies: 18
    Last Post: 10-31-2009, 04:05 AM
  3. Focusing the JTextField and so on while pressing Tab
    By britto_bicsjohn in forum AWT / Swing
    Replies: 1
    Last Post: 08-28-2009, 08:24 AM
  4. Replies: 2
    Last Post: 04-12-2009, 10:39 AM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 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
  •