I'm having issues with basically moving the snowman itself. I know I have to repaint image in a different position, but I tried and failed. The snowman always end up having a line of color and shape following it as an aftereffect.

Can anyone help me out on how to do this? Then I can actually start moving on finally ~_~

import jpb.*;
import java.awt.*;


public class SnowMan {
  
  public static void main (String []args) {
 
    DrawableFrame df = new DrawableFrame("SnowMan");
    df.show();
    df.setSize(500,500);
    
    Graphics g = df.getGraphicsContext();
    
    g.setColor(Color.green); //grass
    g.fillRect(0, 400, 500, 500);
    g.setColor(Color.black); //sky
    g.fillRect(1,1,500,400);
    
    Color brown = new Color(128, 64, 0);
    g.setColor(brown); //ground
    g.fillRect(0, 480, 499, 300);
    g.setColor(Color.darkGray); //moon
    g.fillOval(0,20, 170, 170);
    g.setColor(Color.lightGray);
    g.drawOval(30, 50, 10, 10);
     g.drawOval(50, 70, 20, 20);
    g.drawOval(30, 90, 10, 10);
     g.drawOval(100, 70, 20, 20);
   g.drawOval(40, 150, 10, 10);
     g.drawOval(60, 100, 20, 20);
    g.drawOval(30, 90, 10, 10);
     g.drawOval(122, 150, 20, 20);
     g.drawOval(120, 114, 30, 30);
     g.drawOval(90, 100, 10, 10);
     g.drawLine(100, 200, 120, 204); //haze
     g.drawLine(155, 160, 170, 165);
     g.drawLine(168, 140, 178, 150);
     g.drawLine(176, 130, 190, 130);
     g.drawLine(178, 110, 190, 110);
     g.drawLine(170, 90, 190, 90);
     g.drawLine(168, 70, 188, 70);
     g.drawLine(170, 60, 180, 50);
     g.setColor(Color.blue); // ground grass
    
      
      g.setColor(Color.lightGray); //snowman
    g.fillOval(250,300, 150, 150);
    g.fillOval(268,200, 110, 110);
    g.setColor(Color.white);
    g.fillOval(292,140, 70, 70);
      g.setColor(Color.lightGray); //spacesuit
     g.drawOval(292,140, 70, 70);
     g.drawOval(295,140, 70, 70); 
     g.drawOval(294,140, 70, 70);
     g.drawOval(293,140, 70, 70);  
     g.drawOval(296,140, 70, 70);
     g.drawOval(297,140, 70, 70);  
     g.drawOval(298,140, 70, 70);
     g.drawOval(299,140, 70, 70); 
     
     g.setColor(Color.darkGray); //pocket
     g.fillRect(280, 250, 26, 24);
     
     g.setColor(Color.orange); //nose
     int[] xC = {325, 330, 335};
     int[] yC = {180, 200, 180};
     g.fillPolygon(xC, yC, xC.length);
     
     g.setColor(Color.black); //eyes
     g.fillOval(318, 151, 10, 10);
     g.fillOval(332, 151, 10, 10);
     
     
     
     g.setColor(Color.blue);
     for (int x = 0; x < 500; x += 20) { //grass patch in dirt
            df.repaint();
            try { Thread.sleep(100); }
            catch (InterruptedException e) { }
       
            for (int x1 = 0; x1 < 500; x1 += 20) {
            df.repaint();
            try { Thread.sleep(200); }
            catch (InterruptedException e) { }
              
              g.drawLine(x,498,x1,488); }
        }
     
     
     
    
    
    int count = 1;
  
  do {
    if( count % 2 == 0) {
      g.setColor(Color.white);
     g.fillOval(318, 151, 10, 10);
     g.fillOval(332, 151, 10, 10);
     
     
    } else {
     g.setColor(Color.black); //eyes
     g.fillOval(318, 151, 10, 10);
     g.fillOval(332, 151, 10, 10);
  
                                                                                                                      }  
      try
{

 g.fillOval(318, 151, 10, 10);
   

Thread.sleep(100); 

}

catch(Exception e)
{
System.out.println(e);
}
count++;


     df.repaint();
    }  while(count!=0);  
  }
    
      
  
  }

Never heard of the jpb package or the DrawableFrame class. Did a quick google and found this link.

http://knking.com/books/java/jpb/index.html

Is that it?

I see it extends Frame. What you describe sounds like it results from a lack of a call to a component's "super" paintComponent method. It's pretty common. Here's a thread.

http://www.coderanch.com/t/345317/GUI/java/clear-JPanel-before-repainting

One way of handling this is to write a class that extends Panel (or perhaps JPanel. I've never actually used anything but swing on all of this, so it could be different, could not) and put all of your Graphics code in that class's paintComponent rather than in main. Create that Panel and add it to your Frame. Every time "repaint" gets called, make sure this line's at the top of your Panel's paintComponent function.

super.paintComponent(g);

That'll clear the panel. and repaint with any new coordinates.

Yup JPB.

And thank you for the reply. Helped alot.

import java.awt.Graphics;

 import java.awt.Color;

 

  public class Checkers2 extends java.applet.Applet implements Runnable {

 

      Thread runner;

      int xpos;

      int ux1,ux2;

 

     public void start() {

        if (runner == null); {

        runner = new Thread(this);

        runner.start();
new AePlayWave("fly.wav.wav").start(); //music 
    }

 }



   public void stop() {

         if (runner != null) {

            runner.stop();

            runner = null;

        }

    }



    public void run() {

        setBackground(Color.black);

        while (true) {

            for (xpos = 5; xpos <= 50; xpos += 4) {

                if ( xpos == 100 ) 
                ux2 = xpos + 50;
                repaint();

                try { Thread.sleep(50); }

                catch (InterruptedException e) { }

                 if (ux1 == 0) 
                   ux1 = xpos;

             }

             for (xpos = 105; xpos > 5; xpos -= 4) {

                 ux1 = xpos;

                 repaint();

                try { Thread.sleep(70); }

                catch (InterruptedException e) { }

                if (ux2 == 0) ux2 = 0;

           }

        }
   }

    public void update(Graphics g) {
g.setColor(Color.blue);
      g.fillRect(300-xpos, 90, 50, 50);
       

        paint(g);

     }



     public void paint(Graphics g) {
super.paint(g);
       // Draw background

        g.setColor(Color.black);

       g.fillRect(0, 0, 500, 500);

       


         // Draw checker

  g.setColor(Color.green); //grass
    g.fillRect(0, 400, 500, 500);
    g.setColor(Color.black); //sky
    g.fillRect(1,1,500,400);
    
    Color brown = new Color(128, 64, 0);
    g.setColor(brown); //ground
    g.fillRect(0, 480, 499, 300);
        g.setColor(Color.darkGray); //moon
    g.fillOval(0,20, 170, 170);
    g.setColor(Color.magenta);
    g.fillOval(0,20, 160-xpos, 170);
    g.setColor(Color.orange);
    g.fillOval(0,20, 170-xpos, 170-xpos);
    g.setColor(Color.darkGray);
    g.fillOval(0,20, 170-xpos, 170);
    g.setColor(Color.lightGray);
    g.drawOval(30, 50, 10, 10);
    g.setColor(Color.lightGray);
    g.drawOval(30, 50, 10, 10);
     g.drawOval(50, 70, 20, 20);
    g.drawOval(30, 90, 10, 10);
     g.drawOval(100, 70, 20, 20);
   g.drawOval(40, 150, 10, 10);
     g.drawOval(60, 100, 20, 20);
    g.drawOval(30, 90, 10, 10);
     g.drawOval(122, 150, 20, 20);
     g.drawOval(120, 114, 30, 30);
     g.drawOval(90, 100, 10, 10);
     g.drawLine(100, 200, 120, 204); //haze
     g.drawLine(155, 160, 170, 165);
     g.drawLine(168, 140, 178, 150);
     g.drawLine(176, 130, 190, 130);
     g.drawLine(178, 110, 190, 110);
     g.drawLine(170, 90, 190, 90);
     g.drawLine(168, 70, 188, 70);
     g.drawLine(170, 60, 180, 50);
     g.setColor(Color.blue); // ground grass
    
     
       
      g.setColor(Color.lightGray); //snowman
  g.fillOval(250+xpos,300+ux1, 150, 150);
  g.fillOval(268+xpos,200+ux1, 110, 110);
  g.setColor(Color.white);
  g.fillOval(292+xpos,140+ux1, 70, 70);
  g.setColor(Color.lightGray); //spacesuit
  g.drawOval(292+xpos,140+ux1, 70, 70);
  g.drawOval(295+xpos,140+ux1, 70, 70); 
  g.drawOval(294+xpos,140+ux1, 70, 70);
  g.drawOval(293+xpos,140+ux1, 70, 70);  
  g.drawOval(296+xpos,140+ux1, 70, 70);
  g.drawOval(297+xpos,140+ux1, 70, 70);  
  g.drawOval(298+xpos,140+ux1, 70, 70);
  g.drawOval(299+xpos,140+ux1, 70, 70); 

  g.setColor(Color.darkGray); //pocket
  g.fillRect(280+xpos, 250+ux1, 26, 24);

  g.setColor(Color.orange); //nose
  int[] xC = {325+xpos, 330+xpos, 335+xpos};
  int[] yC = {180+ux1, 200+ux1, 180+ux2};
  g.fillPolygon(xC, yC, xC.length);

  g.setColor(Color.black); //eyes
  g.fillOval(318+xpos, 151+ux1, 10, 10);
  g.fillOval(332+xpos, 151+ux1, 10, 10);
  
  g.setColor(Color.lightGray); //arms
  g.fillRect(205+xpos, 230+ux1, 90, 5);
  g.fillRect(360+xpos, 230+ux1, 90, 5);
  
  g.setColor(Color.black); //mouth
  g.fillOval(315+xpos, 200+ux1, 30-ux1, 5);
  g.setColor(Color.white);
  g.fillOval(317+xpos, 199+ux1, 25, 4);
  g.setColor(Color.black); //mouth
  g.fillOval(315+xpos, 200+ux1, 30-ux1, 5);
 g.setColor(Color.white);
  g.fillOval(317+xpos, 199+ux1, 25, 4);
         // reset the drawing area

  
        ux1 = ux2 = 0;

     }

 }

I decided to just rewrite some of the code. I was wondering why the super method isn't working. I can't get the snowman to stop moving.

The irony...I got it to move now I can't make it stop moving when I want it to.

Line 52 - while(true). Infinite loop. Perhaps get rid of the infinite loop and it will stop? I don't imagine the fact that it doesn't stop has anything to do with the super method.

Line 79 seems pointless. "If a variable is equal to a certain value, then set it to the value that it already is set to".

Awesome. Got it to work now. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.