Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a java programming class I am trying to create the illusion that a face drawn is moving across a frame. Everything is working just fine in an A.W.T. Applet or Frame. When I move things over to swing the repaint method seems to work in a different manner. Instead of a nice little hop across the frame the image leaves a print each time it is redrawn.Does anyone have any tips on how I can improve this feature.I have attached the code for this project. Thanks.


C#
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;


public class PersonMain extends Frame implements Runnable{

    private int x =0;

    public PersonMain(){
        super("Moving Face");

        setBackground(Color.BLUE);
       
        setSize(400,400);

        this.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent we){
                System.exit(0);
              }
            });

        Thread t = new Thread(this);
        t.start();

    }

    public void run() {

        int xDir = +1; //direction (horizontal)
        int incr = 5; //speed of movement
        int sleepFor = 600;
        while (true) {
            x += (incr * xDir);
            /*redraw image in new screen location*/
            repaint();
            setBackground(Color.BLUE);
            try {
                /*put thread to sleep for designated time*/
                Thread.sleep(sleepFor);
            } //ends try
            catch (InterruptedException e) {}
        }
    } //ends method

    public void paint(Graphics g){

        g.setColor(Color.WHITE);

        g.drawOval(x, 100, 200,200);
        g.fillOval(x, 100,200,200);

        drawHat();

        drawEye(x+30,130);
        drawEye(x+140,130);

        drawNose(x,150);

        drawMouth();

    }

    public void drawEye(int x, int y) {

         Graphics g = getGraphics();


         g.drawOval(x, y, 25, 15);
         g.setColor(Color.BLACK);
         g.fillOval(x, y, 25, 10);

    }

    public void drawHat(){

        Graphics g = getGraphics();

         g.drawRect(x+20, 20, 150, 100);
         g.fillRect(x+20,20,150,100);
         g.drawLine(x+20, 120, x+275, 120);

    }

     public void drawNose(int x, int y) {

         Graphics g = getGraphics();

         g.drawLine(x+100, y, x+120, 160);
         g.drawLine(x+100, y, x+120, 160);
     }

     public void drawMouth(){

         Graphics g = getGraphics();

         g.drawArc(x+60, 190, 90, 75, 180, 180);

         }


     public static void main(String args[]){
         PersonMain pm = new PersonMain();

         pm.setVisible(true);
         pm.setSize(600,400);
     }
}
Posted
Updated 30-Nov-12 10:23am
v3

1 solution

I'm kind of surprised that the earlier version didn't leave the ghost trace as well.

All your code is doing is drawing the face 5 pixels to the right each time.

It doesn't ever erase what was previously drawn.

Your paint method needs to start by erasing what was previously drawn.

The easiest way would be to paint the entire rectangle with the background color each time before you draw the face.
 
Share this answer
 
Comments
Member 8477450 30-Nov-12 17:58pm    
I will give that a try. Thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900