Total members 11890 |It is currently Fri Apr 19, 2024 12:11 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 ...





Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

For this message the author DrRakha has received gratitude : AabidMulani
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 Multi Thread Program     -  
 initial state of thread     -  
 Thread of Event Dispatcher     -  
 invokes a thread's run() method     -  
 high-level thread states     -  
 thread state when it terminates its processing     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com