Buon giorno, Da pochi giorni sto affrontando la programmazione in Java e sto iniziando a provare piccoli programmi scritti (male) da me.
Ora , ho realizzato un piccolo programmino ma non mi funziona come mi aspetto e non riesco a capire dove sbaglio, mi riesce a correggere e aspiegare l'errore qcuno??
Dovrebbe funzionare così: viene disegnata una finestra e tramite le frecce è possibile spostarla sul video ed imposta un parametro direzione, con i tasti + e - si agisce su un parametro passo , cliccando su start la maschera si deve spostare autonomamente con il passo e la velocità impostata, in movimento con i tasti di cui sopra (frecce +-) si variano i parametri e quindi la maschera varia il movimento. Stop ferma il movimento autonomo della maschera.
Il primo problema è che dopo aver impostato i parametri di movimento e dato lo start non è più possibile modificarli nè in movimento nè dopo aver cliccato lo stop.
Questo è il codice:
Grazie a tutticodice:package test; import java.awt.*; import java.awt.event.*; import javax.swing.*; import test.*; /** * * @author m */ public class test1 extends JFrame implements ActionListener,KeyListener { JPanel TestPanel,TestPanel2; JButton bottone ; JButton start,stop; Timer timer; JLabel lab = new JLabel("TEST"); private static int i = 1; private static int XX=200; private int YY=100; private static int direzione=0; private static int passo=0; public test1(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); 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 ); this.addKeyListener(this); setFocusable(true); setLocation(XX,YY); setSize(400,500); pack(); show(); }; public test1(String Nome){ super("default"); setLocation(50,50); setSize(200,100); show(); }; public void start() { if (timer == null) { timer = new Timer(100,this); timer.start(); } else timer.start(); } 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 stop() { if (timer != null) { timer.stop(); } } public void actionPerformed(ActionEvent e) { if (e.getSource()==stop){ System.out.println("stop"); stop(); } else if (e.getSource()==start){ start(); System.out.println("start "); } 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 { 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 dir=this.getDirezione(); int pass=this.getPasso(); 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>850) go_x=0; if (go_x<-5) go_x=850; go_y=y; } else { go_y=y+(segno*pass); if (go_y>620) go_y=0; if (go_y<-5) go_y=620; go_x=x; } this.setLocation(go_x,go_y); this.repaint(); } } /* 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) { System.out.println("Ho creato la finestra"); test1 w= new test1("Finestra "+i,10+(i*50), 10+(i*50),i); }; // class MovementAdapter 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 this.setDirezione(3); ke.getComponent().setLocation(ke.getComponent().getX(),ke.getComponent().getY()+50); ke.getComponent().repaint(); System.out.println("giu "); } else if (kc == ke.VK_LEFT) { // Left == West this.setDirezione(2); ke.getComponent().setLocation(ke.getComponent().getX() - 50,ke.getComponent().getY()); ke.getComponent().repaint(); System.out.println("sinistra"); } else if (kc == ke.VK_UP) { // up this.setDirezione(1); ke.getComponent().setLocation(ke.getComponent().getX(),ke.getComponent().getY()-50); ke.getComponent().repaint(); System.out.println("su"); } else if (kc == ke.VK_RIGHT) { // Right this.setDirezione(4); ke.getComponent().setLocation(ke.getComponent().getX() + 50,ke.getComponent().getY()); ke.getComponent().repaint(); System.out.println("destra"); } else if (kc == ke.VK_F1) { int i=this.getMyID(); System.out.println(i); if (i==1){ System.exit(1); } else { i--; hide(); } } else if (kc == ke.VK_F2) { int i=this.getMyID(); i++; System.out.println(i); // test1 w = new test1("Finestra "+i,10+(i*10), 10+(i*10)); // timed w = new timed(i); } else if (kc == ke.VK_MINUS) { if (this.getPasso() > 0 ) this.setPasso(this.getPasso()-1); System.out.println(this.getPasso()+" "+this.getDirezione()); } else if (kc == ke.VK_ADD) { if (this.getPasso() < 10 ) this.setPasso(this.getPasso()+1); System.out.println(this.getPasso()+" "+this.getDirezione()); } } class WAdapter extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.out.println("window "+1); if (i==1){ System.exit(0); } else { i--; hide(); } } } }
stabi

Rispondi quotando