Results 1 to 4 of 4
  1. #1
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default problem with adding circle and rectangle

    hi i have a little problem

    i have 3 buttons in my application when i click on the circle
    i can paint the circle

    but when i swith to rectangle , i can paint vector of rectangle
    but my circle dissapear

    and when i switch back to circle button my circle show up again but not the rectangles

    any idea why this happening ?

    i think i have problem abit to know when to use repaint()

    here is the code

    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    // This class represents a canvas on which 4040 pixel squares can be drawn.
    // The squares are centered around where the user clicks.
    public class SquareCanvas extends JPanel implements ActionListener,MouseListener {
    
    // Keep track of all square center positions
    private Vector squares,circles;
    String st="";
    private Button circleButton,rectangleButton,rectangleButton1;
    // Default constructor
    public SquareCanvas() 
    {
    squares = new Vector();
    circles = new Vector();
    setBackground(Color.white);
    circleButton = new Button("Circle"); 
    add(circleButton); 
    circleButton.addActionListener(this); 
    rectangleButton = new Button("Rectangle"); 
    add(rectangleButton); 
    rectangleButton.addActionListener(this); 
    rectangleButton1 = new Button("FillRectangle"); 
    add(rectangleButton1); 
    rectangleButton1.addActionListener(this); 
    addMouseListener(this);
    
    }
    
    // This is the method that is responsible for displaying the contents of the canvas
    public void paintComponent(Graphics graphics) 
    {
    	// Draw the component as before (i.e., default look)
    	super.paintComponent(graphics);
    
    
    	if(st.equals("Rectangle"))
    	{
    		// Now add all of our squares
    		graphics.setColor(Color.RED);	
    		Enumeration enumeration = squares.elements();
    		
    		while(enumeration.hasMoreElements()) 
    		{
    			Point center = (Point)(enumeration.nextElement());
    			graphics.drawRect(center.x-20, center.y-20, 40, 40);
    		}	
    		repaint();
    	}
    	else if(st.equals("Circle"))
    	{
    		graphics.setColor(Color.magenta);	
    		graphics.fillRect(20, 20, 40, 40);
    		repaint();
    	}
    }
    
    // These are unused MouseEventHandlers. Note that we could have
    // used an Adapter class here. However, a typical drawing
    // application would make use of these other events as well.
    public void mouseClicked(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
    
    // Store the mouse location when it is pressed
    public void mousePressed(MouseEvent event) 
    {
    	squares.add(event.getPoint());
    	repaint(); // this will call paintComponent()
    }
    
    public void actionPerformed(ActionEvent event) 
    { 
    	 if (event.getSource() == circleButton) 
    	 {
    		 st ="Circle";
    		 //repaint();
    	 }
    	 if (event.getSource() == rectangleButton)
    	 { 
    		 st ="Rectangle";
    		 //repaint();
    	 }
    	 if (event.getSource() == rectangleButton1) 
    	 {
    		 st ="FillRectangle";
    		 repaint(); 
    	 }
    	
    } 
    
    public static void main(String args[]) 
    {
    JFrame frame = new JFrame("Square Drawing Example");
    frame.getContentPane().add(new SquareCanvas());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setVisible(true);
    
    }
    }

    thankz in advance for this big help

  2. #2
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    You need to repaint the shapes every time.

    Custom Painting Approaches shows two ways to do this.

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

    Default

    duplicate posts closed.

  4. #4
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

Similar Threads

  1. how to add a circle or rectangle in a jtextarea
    By nicnicnic in forum Java 2D
    Replies: 10
    Last Post: 10-17-2009, 04:09 PM
  2. Problem with adding
    By sanox in forum New To Java
    Replies: 5
    Last Post: 09-08-2009, 11:04 AM
  3. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  4. Problem Adding Content To JTabbedPane
    By JDCAce in forum AWT / Swing
    Replies: 5
    Last Post: 10-18-2008, 09:45 AM
  5. Problem in adding sound.
    By shanky_sanks in forum Java Applets
    Replies: 6
    Last Post: 03-29-2008, 08:37 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
  •