codice:
package test;
import java.awt.*;
//import java.lang.*; //non serve, importata di default
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import test.*;
/**
*
* @author m
*/
public class test2 extends JFrame implements ActionListener,Runnable {
JPanel TestPanel,TestPanel2;
JButton bottone ;
JButton start,stop;
JLabel lab = new JLabel("TEST");
private int i = 1;
private int XX=200;
private int YY=100;
private int direzione=0;
private int passo=0;
private boolean muovo= false;
static Vector mappa = new Vector();
Thread me;
public test2(String Nome, int x, int y, int k){
super(Nome);
setMyID(k);
bottone = new JButton("nuova");
start = new JButton("start");
stop = new JButton("stop");
// this.addKeyListener(new MovementAdapter());
addWindowListener(new WAdapter());
bottone.setSize(50,200);
TestPanel= new JPanel();
bottone.addActionListener(this);
start.addActionListener(this);
stop.addActionListener(this);
bottone.addKeyListener(new tasti());
start.addKeyListener(new tasti());
stop.addKeyListener(new tasti());
TestPanel.setBackground( Color.green );
TestPanel.setLayout(new GridLayout( 3, 3 ));
TestPanel2= new JPanel();
TestPanel2.setBackground( Color.red );
TestPanel2.setLayout(new GridLayout( 1, 3 ));
TestPanel.add(bottone,BorderLayout.NORTH);
TestPanel.add(lab,BorderLayout.CENTER);
TestPanel2.add(start,BorderLayout.SOUTH);
TestPanel2.add(stop,BorderLayout.SOUTH);
TestPanel.setSize(200,200);
Container c = getContentPane();
c.add( TestPanel, BorderLayout.CENTER );
c.add( TestPanel2, BorderLayout.SOUTH );
// c.add(D,BorderLayout.CENTER);
addKeyListener(new tasti());
setFocusable(true);
setLocation(XX,YY);
setSize(400,500);
mappa.addElement(this);
pack();
show();
me=new Thread(this);
me.start();
};
public test2(String Nome){
this(Nome,100,100,1);
}
public test2(int k){
this("Nuova finestra "+k,100,100,k);
}
public int getPasso(){
return this.passo;
}
public int getDirezione(){
return this.direzione;
}
public int getMyID(){
return this.i;
}
public void setMyID(int ID){
this.i=ID;
}
public void setPasso(int p){
if (p>=0) this.passo=p;
}
public void setDirezione(int d){
if (d>0 && d<5) this.direzione=d;
}
public void run(){
}
public void muovi(){
while (muovo){
int segno=1;
int go_x=0;
int go_y=0;
int x,y;
x=(int)this.getLocation().getX();
y=(int)this.getLocation().getY();
int larg = this.getWidth();
int lung = this.getHeight();
int dir=this.getDirezione();
int pass=this.getPasso();
if (muovo){
int k = this.getMyID();
int t = mappa.size();
boolean ok=false;
test2 prova;
for (int d=0;d<t;){
prova=(test2)mappa.get(d);
int kt = prova.getMyID();
d++;
int xt = (int)prova.getLocation().getX();
int yt = (int)prova.getLocation().getY();
int largt = prova.getWidth();
int lungt = prova.getHeight();
System.out.println(x+", "+(x+larg)+" , "+xt+","+(xt+largt)+" , "+y+", "+(y+lung)+","+yt+", "+(yt+lungt));
if (kt!=k){
if ( (x>xt && x<xt+largt && y>yt && y<yt+lungt) || // punto alto a sx
(x+larg>xt && x+larg<xt+largt && y>yt && y<yt+lungt) || // punto alto a dx
(x>xt && x<xt+largt && y+lung>yt && y+lung<yt+lungt) || // punto basso a sx
(x+larg>xt && x+larg<xt+largt && y+lung>yt && y+lung<yt+lungt) // punto basso a dx
){
this.muovo=false;
}
}
}
}
if (muovo){
System.out.println(">"+x+" , "+y+" , "+pass+" "+dir);
if ((dir==1) || (dir==2)) segno=-1;
if ((dir==2) || (dir==4)) {
go_x=x+(segno*pass);
if (go_x>880) go_x=0;
if (go_x<-5) go_x=880;
go_y=y;
}
else {
go_y=y+(segno*pass);
if (go_y>640) go_y=0;
if (go_y<-5) go_y=640;
go_x=x;
}
this.setLocation(go_x,go_y);
this.repaint();
}
try {
me.sleep (200);
} catch(Exception e) {}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==stop){
System.out.println("stop");
this.muovo=false;
} else
if (e.getSource()==start){
System.out.println("start ");
if (getDirezione()>0 && getPasso()>0) {
this.muovo=true;
muovi();
}
} else
if (e.getSource()==bottone){
int i = this.getMyID();
System.out.println("bottone "+i);
// timed w = new timed(i);
// w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
} else {
System.out.println(e.getSource().toString());
}
// return ;
}
/* over-ridden del metodo della classe JPanel per consentire
al pannello di ricevere il "focus" della tastiera.
Senza questo metodo il pannello non sarà in grado di
intercettare gli eventi della tastiera!!!
*/
public boolean isFocusable() {
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int i = 1;
System.out.println("Ho creato la finestra"+1);
test2 w= new test2("Finestra "+i,10+(i*50), 10+(i*50),i);
};
class tasti extends KeyAdapter {
/** Handle for the KeyListener interface
* key control for movement */
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent ke) {
int kc = ke.getKeyCode();
if (kc == ke.VK_DOWN) {
// Down
setDirezione(3);
// muovi();
System.out.println("giu ");
} else if (kc == ke.VK_LEFT) {
// Left == West
setDirezione(2);
// muovi();
System.out.println("sinistra");
} else if (kc == ke.VK_UP) {
// up
setDirezione(1);
// muovi();
System.out.println("su");
} else if (kc == ke.VK_RIGHT) {
// Right
setDirezione(4);
// muovi();
System.out.println("destra");
} else if (kc == ke.VK_F1) {
int i=getMyID();
//mappa.V.remove(this);
mappa.removeElement(this);
System.out.println(i);
if (i==1){
System.exit(1);
} else {
i--;
hide();
}
} else if (kc == ke.VK_F2) {
int i=getMyID();
i++;
System.out.println(i);
// test1 w = new test1("Finestra "+i,10+(i*10), 10+(i*10));
test2 w = new test2(i);
} else if (kc == ke.VK_MINUS) {
if (getPasso() > 0 ) setPasso(getPasso()-1);
System.out.println(getPasso()+" "+getDirezione());
} else if (kc == ke.VK_ADD) {
if (getPasso() < 10 ) setPasso(getPasso()+1);
System.out.println(getPasso()+" "+getDirezione());
}
}
}
class WAdapter extends WindowAdapter {
public void windowClosing( WindowEvent e )
{
int i=getMyID();
System.out.println("window "+i);
if (i==1){
System.exit(0);
} else {
// i--;
hide();
}
}
}
}