Program to create cartoon in applet and play using thread
In this program to create applet and using paint to create more design and also play design. For example design (cartoon) start from left side and move to right side.
Let’s start to program
1. Create java class with any name
package applet_demo;
import java.applet.Applet;
import java.awt.*;
public class Applet_demo extends Applet implements Runnable{
int width =700;
int height= 600;
int x=0,x1=0;
int y=0,y1=0;
int
a=113,b=200,c=100,d=85,e=325,f=150,g1=250,h=170,i=210,j=195,k=170;
int
a1=200,b1=288,c1=200,d1=25,e1=390,f1=150,g2=250,h1=15,i1=15,j1=195,k1=200;
@Override
public void init(){
setSize(width,height);
setBackground(new
Color(55,20,30));
}
@Override
public void start(){
Thread
t = new Thread(this);
t.start();
}
@Override
public void
paint(Graphics g){
g.setColor(new Color(255,255,255));
g.drawLine(x+113, 150, x1+200, 30);
g.drawLine(x+200, 30, x1+288, 150);
g.setColor(Color.yellow);
g.drawOval(x+100, 100, 200, 200);
g.setColor(Color.PINK);
g.fillRect(x+85, y+300, 240, 150);
g.drawLine(x+85, 360, x1+25, 460);
g.drawLine(x+325,360,x1+390,460);
g.drawLine(x+150,450,x1+150,500);
g.drawLine(x+250,450,x1+250,500);
g.drawOval(x+170, 150, 15, 25);
g.drawOval(x+210, 150, 15, 25);
g.drawLine(x+195,180,x1+195,200);//nose
g.drawLine(x+170,215,x1+200,215); //mouth
}
int xdirection =1;
int ydirection = 1;
@Override
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true){
x+=xdirection;
x1+=xdirection;
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException e) {
}}
}
}
2 comments: