Boa noite pessoal do GUJ.
Gostaria de uma dica de vocês, estou utilizando o exemplo encontrado no LIVRO CORE JAVA ED. 2, chamado BounceThreads.
O problema está em colidir as bolas, quando elas são criadas.
Abaixo segue o código do programa, se quiserem compilar e dar uma mão.
Por enquanto estou tentando modificar o que já existe, e vou postando aqui.
[code]/**
- @version 1.20 1999-04-25
-
@author Cay Horstmann
*/
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class BounceThread
{ public static void main(String[] args)
{ JFrame frame = new BounceThreadFrame();
frame.show();
}
}
class BounceThreadFrame extends JFrame
{
int colisao;
public BounceThreadFrame()//construtor
{ setSize(300, 250);
setTitle(“Detecta Colisão”);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container cont = getContentPane();
canvas = new JPanel();
canvas.setBackground(Color.WHITE);
cont.add(canvas, "Center");
JPanel panel = new JPanel(); panel.setBackground(Color.LIGHT_GRAY);
JPanel panelSup = new JPanel();
addButton(panel, "Start",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ Ball1 b = new Ball1(canvas);
b.start();
}
});
addButton(panel, "Close",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ canvas.setVisible(false);
System.exit(0);
}
});
addLabel(panelSup, "Total de colisões");
cont.add(panelSup, "North");
cont.add(panel, "South");
}//fim do construtor
//--------------METODO ADIciONA LABEL------------------------------------
public void addLabel(Container c, String title)
{ JLabel lbl = new JLabel(title);
c.add(lbl);
}
//--------------METODO ADIciONA BOTÃO------------------------------------
public void addButton(Container c, String title,
ActionListener a)
{ JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}//FIM DA CLASSE FRAME
//---------------------------------------------------------------------
class Ball1 extends Thread
{
private JPanel box;
private static final int XSIZE = 10; //tamanho
private static final int YSIZE = 10;
private int x = 0; //reta x - trajetoria da bola
private int y = 0; //reta y - trajetoria da bola
private int dx = 2; //valor de controle da retaX
private int dy = 2; //valor de controle da retaY
public Ball1(JPanel b) //construtor
{ box = b; }
public void draw()//METODO DESENHA
{ Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);//RECEBE AS DIMENSÕES POR PARAMETRO
g.dispose();
//g.setColor(Color.red);
}
//--------------------METODO CHAMADO PELA TRHEAD PARA DETERMINAR TRAJETO
public void move()
{
System.out.println("x: "+x+"y: "+y);
if (!box.isVisible()) return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x < 0)//TESTA SE BATE NA PAREDE ESQUERDA
{ System.out.println(“bate esquerda–se x<0”);
x = 0; dx = -dx;
System.out.println("x: “+x+” dx: “+dx+” y: "+y);
}
if (x + XSIZE >= d.width)//TESTA SE BATE direita
{ System.out.println(“bate direita–x + XSIZE >= d.width”);
x = d.width - XSIZE; dx = -dx;
System.out.println("x: “+x+” dx: “+dx+” y: "+y);
}
if (y < 0)//TESTA SE BATE em cima
{ System.out.println("bate em cima se y < 0 "+y);
y = 0; dy = -dy;
System.out.println("dy: "+dy);
System.out.println("x: “+x+” dx: “+dx+” y: "+y);
}
if (y + YSIZE >= d.height)//TESTA SE BATE EMBAIXO
{ System.out.println(“bate embaixo-se-y + YSIZE >= d.height”);
System.out.println("y: “+y+” “+” YSIZE: “+YSIZE+” “+” d.height: “+d.height+” "+"dy: "+dy);
y = d.height - YSIZE; dy = -dy;
System.out.println("y depois “+y+” "+dy);
System.out.println("x: “+x+” dx: “+dx+” y: "+y);
}
if(y + 10==y-10 ){
System.out.println("y colidiu ");
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run()
{ try
{ draw();
while(true)
{ move();
sleep(2);
}
}
catch(InterruptedException e) {}
}
}
[/code]
Estamos aí pra qualquer coisa, valeu a todos.