Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW

Sat Mar 10, 2012 1:04 pm

Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.util.Map;
import java.util.Random;
import java.awt.image.ImageObserver;
import java.awt.event.*;
import java.awt.Window;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.GraphicsDevice;



public class Screen{
    public GraphicsDevice vc;
    public Screen(){
        GraphicsEnvironment evn=GraphicsEnvironment.getLocalGraphicsEnvironment();
        vc=evn.getDefaultScreenDevice();
    }
   
    public void setFullScreen(DisplayMode dm,JFrame window){
     window.setUndecorated(false);
     window.setResizable(true);
     vc.setFullScreenWindow(window);
     if(dm!=null && vc.isDisplayChangeSupported()){
         try{
             vc.setFullScreenWindow(window);
              }catch(Exception ex){}
     }
}
    public Window getFullScreen(){
      return vc.getFullScreenWindow();
    }
    public void restoreScreen(){
        Window w=vc.getFullScreenWindow();
        if(w!=null){
            w.dispose();
        }
    }   
}


/////////////////////////////////

public class Tank extends JFrame{
    static Screen s;
    public Tank(){
       
    }
    public static void main(String[] args){     
        DisplayMode dm=new DisplayMode(200,200,8,DisplayMode.REFRESH_RATE_UNKNOWN);
        Tank t=new Tank();
         Screen s =new Screen();
        Tank_thread ab=new Tank_thread(dm);
        Tank_thread ab1=new Tank_thread(dm);
        ab1.start();
        ab.start();
    }
   
}
////////////////////////////////
public class Tank_thread extends Tank implements Runnable{
    DisplayMode dm;
    private Image bg;
    private Image TANK_OPP;
    private boolean loaded;
    int mov_time,direction,xx=100,yy=100,width,height,scr_Width=700,scr_Height=700;
    Random r=new Random();
    Screen s=new Screen();
    Thread thread;
    Tank_thread(DisplayMode dm){
        this.dm=dm;
        //this.s=s;
        s.setFullScreen(dm,this);       
        thread =new Thread(this);
        thread.start();       
    }
       
    public void run(){
        loaded=false;
       
        repaint();
    }
   
    public void start(){
        thread.start();
    }
      public void stop(){
        thread.stop();
    }
    public void paint(Graphics g){
    //Window w=s.getFullScreen();
       int n=0;
       while(n<10){
          n++;
          int x=0;
            direction=r.nextInt(4);
            loadPics(direction);
            mov_time=r.nextInt(250);
            if(loaded==true){
               while(x<(mov_time)){
                    if(direction==0){
                        yy--;
                     }else if(direction==1){
                        xx++;
                     }else if(direction==2){
                        yy++;
                     }else{
                        xx--;
                    }
                    width=TANK_OPP.getWidth(null);
                    height=TANK_OPP.getHeight(null);
                    if(xx<20 || (xx+width)>scr_Width || yy<20 || (yy+height)>scr_Height){
                        direction=r.nextInt(4);
                        loadPics(direction);
                        continue;
                    }
                  g.drawImage(bg,0,0,null);
                  g.drawImage(TANK_OPP,xx,yy,null);
                   try{
                       Thread.sleep(20);
                   }catch(Exception ex){}
                    x++;           
            }
        }
     }
              dispose();
  }
   
    public void loadPics(int x){
        bg=new ImageIcon("C:\\A BATTLE\\WHITE_BG.JPG").getImage();
        if(x==0){
         TANK_OPP=new ImageIcon("C:\\A BATTLE\\P1.JPG").getImage();
        }else if(x==1){
            TANK_OPP=new ImageIcon("C:\\A BATTLE\\P2.JPG").getImage();
        }else if(x==2){
            TANK_OPP=new ImageIcon("C:\\A BATTLE\\P3.JPG").getImage();
        }else{
            TANK_OPP=new ImageIcon("C:\\A BATTLE\\P4.JPG").getImage();
        }
         loaded=true;
    }   

}



 Description:   PLEASE HELP ME..
THE ABOVE PROGRAMS MOVE A TANK(picture) IN RANDOM DIRECTION FOR RANDOM No OF TIMES
BUT WHEN I CREATE TWO THREADS THEN THEY ARE RU[Description]NNING ONE AFTER THE OTHER ANIMATION
ALSO THEY ARE DISPLAYING SEPARATE WINDOW FOR EACH THREAD.....I WANT THAT THEY BOTH APPEAR IN THE SAME WINDOW AND MOVE SIMULTANEOUSLY....
PLZ HELP ME FAST
(i m not good in java programing)
PLZ HELP ME FAST ...




Re: Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW

Sat Mar 10, 2012 10:40 pm

Code:

      Tank_thread ab
=new Tank_thread(dm);
        
Tank_thread ab1=new Tank_thread(dm);
        
ab1.start();
        
ab.start();
 


You are creating two instances of the Tank_thread class , and bos classes a JFrames ( extends JFrame) , i think you should redesign your software. For example ,, you can bas the same JFrame object to the Tank_thread in the constructor , also you can pass the Tank1 object to thread 1 constructor and the Tank2 to thread2, in the thread run function , you change the x,y of TankN object then call paint function to refresh the frame.

Post a reply
  Related Posts  to : Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW
 need project code for time and work management     -  
 daemon thread - What is the use of deamon thread?     -  
 Easy Copy Paste Work From Home - Online Home Based Work     -  
 Thread Safe     -  
 invokes a thread's run() method     -  
 Multi Thread Program     -  
 initial state of thread     -  
 Thread of Event Dispatcher     -  
 high-level thread states     -  
 thread state when it terminates its processing     -