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

Join 149,587 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,782 people online right now. Registration is fast and FREE... Join Now!




repaint() problem

 
Reply to this topicStart new topic

repaint() problem, repaint can't erase previous image

jmmjm
10 Oct, 2007 - 05:54 PM
Post #1

New D.I.C Head
*

Joined: 24 Jul, 2007
Posts: 12


My Contributions
Hi, I am doing an assingment that creates an olympic ring image and makes it move. When it touch the window boundary, it will rebound in the same angle.

Here is my code:

CODE

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

/**
   This class makes a rebound animation using the OlymipicRing object.
*/
public class OlympicRingRebound extends JApplet  
{
     /**
      Sets up the applet.
    */
    public void init()
     {
           setPreferredSize (new Dimension(WIDTH, HEIGHT));
           setBackground(Color.white);
          
          x = 0;
          y = 0;
          moveX = 3;
          moveY = 4;
    }

   /*
        Represents the action listener for the timer.
    */
    class myTimer implements ActionListener
    {
        /*
            Updates the position of the image and possibly the direction
          of movement whenever the timer fires an action event.
        */
        public void actionPerformed(ActionEvent event)
        {
            x += moveX;
              y += moveY;
    
          if (x <= 0 || x >= WIDTH - myOlympic.getWidth())
             moveX = moveX * -1;
    
          if (y <= 0 || y >= HEIGHT - myOlympic.getHeight())
             moveY = moveY * -1;
    
            repaint();
       }
    }
    
    /*
        Sets up the timmer and starts the animation when the applet starts.
    */
    public void start()
    {
        ActionListener listener = new myTimer();
        final int DELAY = 20; // Milliseconds between timer ticks
        Timer t = new Timer(DELAY, listener);
        t.start();
    }
      
    /*
        Draws the image in the current location.
    */    
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        
        myOlympic = new OlympicRings(x, y);  
        
        myOlympic.draw(g2); // draw an olympic ring in x, y position
    }
    
    private int x;
    private int y;
    private int moveX;
    private int moveY;
    OlympicRings myOlympic;    
    public static final int WIDTH = 800, HEIGHT = 600;
}


OlympicRings class is omitted since it just draws an olympic ring.

The problems is that repaint() will update the new position, but the image with the old position is still there. How can I fix the problem?

Thank you.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Repaint() Problem
10 Oct, 2007 - 07:03 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
the first line of your paint method, needs to be a call to the container/object paint class as: super.paint(g); or super.paint(); depending on the effect you want.
that should fix the problem smile.gif
User is offlineProfile CardPM
+Quote Post

jmmjm
RE: Repaint() Problem
10 Oct, 2007 - 07:34 PM
Post #3

New D.I.C Head
*

Joined: 24 Jul, 2007
Posts: 12


My Contributions
QUOTE(William_Wilson @ 10 Oct, 2007 - 08:03 PM) *

the first line of your paint method, needs to be a call to the container/object paint class as: super.paint(g); or super.paint(); depending on the effect you want.
that should fix the problem smile.gif


Thanks. It really got fixed. Could you please explain that a little bit. I just starts to learn GUI in Java.

Thanks again.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:03PM

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