1 Replies - 4773 Views - Last Post: 12 September 2008 - 09:27 PM Rate Topic: -----

#1 Feebz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 08

How do I repaint without resetting the drawing panel?

Posted 12 September 2008 - 09:02 AM

class ShapePanel extends JPanel {
		Shape currentShape;
		public void setShape(Shape shape) {
			currentShape = shape; repaint();
		}  
		public void update(Graphics g) {
			paint(g);
		}
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (currentShape != null) currentShape.draw(g);
		}
	}


This is called whenever I want to draw a shape to a JPanel. However, everytime it is done, the panel removes all previous drawings, which is not what I intended to be. How do I do it?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I repaint without resetting the drawing panel?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How do I repaint without resetting the drawing panel?

Posted 12 September 2008 - 09:27 PM

View PostFeebz, on 12 Sep, 2008 - 09:02 AM, said:

class ShapePanel extends JPanel {
		Shape currentShape;
		public void setShape(Shape shape) {
			currentShape = shape; repaint();
		}  
		public void update(Graphics g) {
			paint(g);
		}
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (currentShape != null) currentShape.draw(g);
		}
	}


This is called whenever I want to draw a shape to a JPanel. However, everytime it is done, the panel removes all previous drawings, which is not what I intended to be. How do I do it?


replace

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (currentShape != null) currentShape.draw(g);
		}



by

		public void paint(Graphics g) {
			super.paint(g);
			if (currentShape != null) currentShape.draw(g);
		}



Actually never saw a situation where paintComponent() had to be overload
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1