I have got application with class extends JPanel
. This panel is drawing an Image. here is a code:
public class BackgroundImage extends JPanel{
private BufferedImage img;
private File imageFile;
public BackgroundImage(){
super();
Samolot s1 = new Samolot(200,200,new Dimension(15,15));
s1.start();
imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_mapa.jpg");
try {
mapa = ImageIO.read(imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
}
What i want to do is a class ball
which will be extends Threads
.
Each ball will be next thread. Balls need to move, for example from left to right by simply incrementation coordinate int x
which defines X-position of created ball.
To show that ball is moving i need to draw an image in current coordinates. after changing position, thread repaint ball to next position. What is more i need to add MouseListener
to my ball, to not make problem harder after click it need just to System.out.println("somemessage");
My questions:
where i need to draw my ball? in class Ball
? or BackgroundImage
? How it should be look like?
where and how add MouseListener
to make him works propertly.
how to repaint my panel in every cycle of thread.
here is a code of my Ball
class:
class Ball extends Thread {
// The image to display
private BufferedImage img;
private int x;
private int y;
Dimension dim;
public void run() {
super.run();
try {
while(x<1000){
Thread.sleep(300);
x++;
System.out.println(x);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Instantiate the panel and perform initialization
Samolot(int x, int y, Dimension d) {
this.x = x;
this.y = y;
this.dim = d;
try {
img = ImageIO.read(new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_samolot.png"));
} catch (IOException e) { }
}
}
I read that i shouldnt draw anything in other threads then my main thread(here BackgroundImage
).
I was thinking about something like that, however it doesnt work:
class Ball
:
class Ball extends JComponent implements Runnable {
MouseListener listener;
public void addListener(MouseListener toAdd) {
listener = toAdd;
}
// The image to display
private BufferedImage img;
private int x;
private int y;
Dimension dim;
public void run() {
try {
while(x<1000){
Thread.sleep(300);
x++;
listener.mouseClicked(new MouseEvent(this, 1, 1, 0, x, y, 1, true));
System.out.println(x);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Instantiate the panel and perform initialization
Samolot(int x, int y, Dimension d) {
this.x = x;
this.y = y;
this.dim = d;
try {
img = ImageIO.read(new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_samolot.png"));
} catch (IOException e) { }
}
public BufferedImage getImg(){
return this.img;
}
}
Class BackgroundImage
:
public class BackgroundImage extends JPanel implements MouseListener{
private BufferedImage mapa;
private File imageFile;
private Ball s1;
public MapaSwiata(){
super();
s1 = new Ball(200,200,new Dimension(15,15));
Thread t = new Thread(s1);
t.start();
imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_mapa.jpg");
try {
mapa = ImageIO.read(imageFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null);
g2d.drawImage(s1.getImg(), 200, 200, 15, 15, null);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("message");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}