Results 1 to 4 of 4
- 11-15-2009, 07:08 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
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 40×40 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
- 11-15-2009, 07:16 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You need to repaint the shapes every time.
Custom Painting Approaches shows two ways to do this.
-
duplicate posts closed.
- 11-16-2009, 06:13 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
how to add a circle or rectangle in a jtextarea
By nicnicnic in forum Java 2DReplies: 10Last Post: 10-17-2009, 04:09 PM -
Problem with adding
By sanox in forum New To JavaReplies: 5Last Post: 09-08-2009, 11:04 AM -
Problem on adding JButton on JPanel NEED HELP
By boisk in forum AWT / SwingReplies: 15Last Post: 03-15-2009, 02:27 PM -
Problem Adding Content To JTabbedPane
By JDCAce in forum AWT / SwingReplies: 5Last Post: 10-18-2008, 09:45 AM -
Problem in adding sound.
By shanky_sanks in forum Java AppletsReplies: 6Last Post: 03-29-2008, 08:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks