Program to create one or more rectangle or circle in applet and play using thread

23:33 Unknown 0 Comments


In this program to create applet and using paint to create more design and also  play design. For example one design start from left side and move to right side and turn to other side creating like loop. In same time other design also performing same task.

Let’s start to program

1.      Create java class with any name
      package applet_demo;
      import java.applet.Applet;
      import java.awt.Color;              
      import java.awt.Graphics;
      public class Kartoon extends Applet implements Runnable{
            int width =700;
             int height= 600;
            int x=50,x1=150,x2=300;
             int y=50,y1=200,y2=50;
              @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(Color.red);
                        g.fillOval(x, y, 50, 50);
                        g.fill3DRect(x1, y1, 60, 60, true);
                        g.fillOval(x2, y2, 50, 50);
                        g.setColor(Color.ORANGE);
                        g.fillOval(300,200, 50, 50);
                        g.drawString("java pivot", 150, y);
                        g.drawString("learn java from begin", 300, 300);
                        g.setColor(Color.GREEN);
                        g.fillRect(x, 250, 35, 25);
            }

            int xdirection =10;
            int ydirection = 10;
            @Override
             public void run() {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true){
                        if(x<=0||x+50>=width){
                                    xdirection *= -1;
            
                         }  
                        if(y<=0||y+50>=height){
                                                 ydirection *= -1;
            }
            x+=xdirection;
             y+=ydirection;
            //for rect
             if(x1<=0||x1+60>=width){
                                     xdirection *= -1; 
            }  
            if(y1<=0||y1+50>=height){
                        ydirection *= -1;
             }
             x1+=xdirection;
            y1+=ydirection;
            // code for
            if(x2<=0||x2+60>=width){
                                     xdirection *= -1; 
            }  
             if(y2<=0||y2+50>=height){
                                     ydirection *= -1;
            }
             x2-=xdirection;
             y2-=ydirection;
           repaint();
           try {
                         Thread.sleep(100);
           } catch (InterruptedException e) {
       }}

      }}


Run this code.
Output:



0 comments:

e gift