Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,008 people online right now. Registration is fast and FREE... Join Now!




How do I repaint without resetting the drawing panel?

 
Reply to this topicStart new topic

How do I repaint without resetting the drawing panel?, Help

Feebz
12 Sep, 2008 - 08:02 AM
Post #1

New D.I.C Head
*

Joined: 5 Sep, 2008
Posts: 3

CODE
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?
User is offlineProfile CardPM
+Quote Post

pbl
RE: How Do I Repaint Without Resetting The Drawing Panel?
12 Sep, 2008 - 08:27 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(Feebz @ 12 Sep, 2008 - 09:02 AM) *

CODE
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

CODE

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


by

CODE

        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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month