This is code for, a smiley have to move on the screen from one place to another. But in this code Smiley create but it is moving. Please tell my? Is there any wrong in my code.

import javax.swing.*;

import java.awt.*;

class CreateSmiley extends JPanel

{

    int x = 0,y = 0;



    public void paintComponen(Graphics g)

    {

        //System.out.println("x = :"+x);

        g.setColor(Color.RED);

        g.drawOval(x,y,200,200);

        g.setColor(Color.YELLOW);

        g.fillOval(x+50,y+70,20,20);

        g.fillOval(x+130,y+70,20,20);

        g.setColor(Color.GREEN);

        g.fillArc(x+30,y+80,140,70,190,165);

        //System.out.println("XXX ="+x);

    }

    void smileFlow() throws Exception

    {

        CreateSmiley s1 = new CreateSmiley();

        while(x != 600)

        {

            Thread.sleep(100);

            x = x + 5;

            y = y + 5;

            System.out.println("x123 = :"+x);

            s1.repaint();

            System.out.println("x321 = :"+x);





        }

    }



}

public class Smiley

{

    public static void main(String a[]) throws Exception

    {



        JFrame f = new JFrame("Smiley");

        CreateSmiley s = new CreateSmiley();

        s.smileFlow();

//      SmileyFlow sf = new SmileyFlow();

        f.add(s);

        f.setSize(600,600);

        f.setVisible(true);

        f.setDefaultCloseOperation(3);



    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You may try the following changes.
Because we want the smileFlow() runs for a certain time (befor x reaches 600), you have to
(1) delete the line code s.smileFlow();
(2) insert the above code s.smileFlow(); after the f.setDefaultCloseOperation(3);
so that it runs after setting up the frame properly.
P.S. the code:f.setDefaultCloseOperation(3);
should be written as:
f.setDefaultCloseOperation(EXIT_ON_CLOSE); // so that one may clearly understand


(3) replace the name of the method: public void paintComponen(Graphics g)
by public void paint(Graphics g)
(4) inser the code line:
super.paint(g); // as the first line code of the method.
into the paint method.
so that the canvas is cleaned before the next operation of repaint().

I have modified your code a little bit so that the Smiley moves "forever".
Attached please find the modification
Please use the code tag to post your code so that one may mention about the code line number.

Edited 6 Years Ago by tong1: n/a

Attachments
import javax.swing.*;
import java.awt.*;

class CreateSmiley extends JPanel implements Runnable {

int x=0,y=0;
Thread timer=null;
boolean flag =true;

public void start(){
	if (timer==null){
	timer = new Thread(this);
	timer.start();
	}
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.drawOval(x,y,200,200);
g.setColor(Color.YELLOW);
g.fillOval(x+50,y+70,20,20);
g.fillOval(x+130,y+70,20,20);
g.setColor(Color.GREEN);
g.fillArc(x+30,y+80,140,70,190,165);
}

public void run(){
   while(timer != null){
   try {Thread.sleep(300);
   if(flag) {
	x+=5;
	y+=5;
   } else {
	x-=5;
	y-=5;
	}
   if ((x == 400) || (x==0))
	flag = !flag;
   repaint();
	}catch(Exception e){}
   }
}



}

public class Smiley extends JFrame {
	
	public Smiley(){
	super("Smiley");	
   	CreateSmiley s = new CreateSmiley();
   	add(s);
   	s.start();
	setSize(600,600);
	setVisible(true);
	setDefaultCloseOperation(3);	
	}

public static void main(String a[]) throws Exception{
	Smiley sm= new Smiley();
	}
}
import javax.swing.*;

import java.awt.*;

class CreateSmiley extends JPanel

{

int x = 0,y = 0;



public void paint(Graphics g)

{
	super.paint(g);

//System.out.println("x = :"+x);

g.setColor(Color.RED);

g.drawOval(x,y,200,200);

g.setColor(Color.YELLOW);

g.fillOval(x+50,y+70,20,20);

g.fillOval(x+130,y+70,20,20);

g.setColor(Color.GREEN);

g.fillArc(x+30,y+80,140,70,190,165);

//System.out.println("XXX ="+x);

}

void smileFlow() throws Exception

{

//CreateSmiley s1 = new CreateSmiley();

while(x != 600)

{

Thread.sleep(100);

x = x + 5;

y = y + 5;

System.out.println("x123 = :"+x);

//s1.repaint();
repaint();
System.out.println("x321 = :"+x);





}

}



}

public class Smiley1

{

public static void main(String a[]) throws Exception

{



JFrame f = new JFrame("Smiley");

CreateSmiley s = new CreateSmiley();

//s.smileFlow();

// SmileyFlow sf = new SmileyFlow();

f.add(s);


f.setSize(600,600);

f.setVisible(true);

f.setDefaultCloseOperation(3);

s.smileFlow();

}

}

Thank you, James, for the comment.

In the following code: line 17 the paintComponent() is replaced by paint(). If the paintComponent() is used the line 18 will produce a run error:

at javax.swing.JComponent.paint(JComponent.java:1027)
at CreateSmiley.paintComponent(Smiley.java:18)
(Why? I don't know. Please offer some annotation for this point.)

If the paintComponent() has to be used one has to write two lines of code as the first two lines of the method to do the job of cleaning canvas.

g.setColor(Color.white);
g.fillRect(0,0,600,600);

In order to clean the canvas by calling super.paint(g); one has to replace paintComponent() by paint().

import javax.swing.*;
import java.awt.*;

class CreateSmiley extends JPanel implements Runnable {

int x=0,y=0;
Thread timer=null;   // a Thread instance associated with repaint()
boolean flag =true;  // signal toggle controls the increment/decrement in x and y

public void start(){
	if (timer==null){
	timer = new Thread(this);
	timer.start();
	}
}

public void paint(Graphics g) {  //  paintComponent replaced otherwise the line 18 produces an error message
super.paint(g); // clean the canvas before drawing the next picture
g.setColor(Color.RED);
g.drawOval(x,y,200,200);
g.setColor(Color.YELLOW);
g.fillOval(x+50,y+70,20,20);
g.fillOval(x+130,y+70,20,20);
g.setColor(Color.GREEN);
g.fillArc(x+30,y+80,140,70,190,165);
}

public void run(){
   while(timer != null){
   try {Thread.sleep(300);
   if(flag) {
	x+=5;
	y+=5;
   } else {
	x-=5;
	y-=5;
	}
   if ((x == 400) || (x==0))
	flag = !flag;
   repaint();
	}catch(Exception e){}
   }
  }
}

public class Smiley extends JFrame {	
	public Smiley(){
	super("Smiley");	
   	CreateSmiley s = new CreateSmiley();
   	add(s);
   	s.start();
	setSize(600,600);
	setVisible(true);
	setDefaultCloseOperation(EXIT_ON_CLOSE);	
	}

public static void main(String a[]) throws Exception{
	Smiley sm= new Smiley();
	}
}

Edited 6 Years Ago by tong1: n/a

If you use paintComponent (as recommended) then the first line of the method should also be super.paintComponent(g); not super.paint(g);

This article has been dead for over six months. Start a new discussion instead.