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

Draw Tank with animation and using threads too

Sat Mar 10, 2012 1:09 pm

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 ...
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();
    //RENDERING IF CONDITION MISSING
       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;
    }   

}
 



Attachments
QUESTION.txt
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 ...
(4.15 KiB) Downloaded 746 times
P1.jpg
P1.jpg (4.15 KiB) Viewed 4651 times
P2.jpg
P2.jpg (4.45 KiB) Viewed 4651 times
P3.jpg
P3.jpg (4.29 KiB) Viewed 4651 times

Post a reply
  Related Posts  to : Draw Tank with animation and using threads too
 Tank game code     -  
 threads block on I/O     -  
 Introduction to threads in Java     -  
 Implementing synchronization in C++ with threads     -  
 visual c++ MFC background threads     -  
 Flashing Graphics animations with threads     -  
 Simple animation     -  
 animation using Photoshop     -  
 Sprite Animation     -  
 cloud animation     -