I am new to java and now I am working on Threads.But now I have a big problem: When I run the following code, I except two images move in the screen but one of them does now move and even the other is not shown. I wanna know what is the problem: class Enemy:
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Enemy extends JLabel implements Runnable{
private BufferedImage i;
private int x,y;
Enemy()
{
x=y=0;
try{
i=ImageIO.read(new File("m2.jpg"));
}catch (Exception e){}
this.setIcon(new ImageIcon(i));
this.setLocation(x,y);
this.setBounds(new Rectangle(new Point(x,y),this.getPreferredSize()));
}
Enemy(int X,int Y)
{
x=X;
y=Y;
try{
i=ImageIO.read(new File("m2.jpg"));
}catch (Exception e){}
this.setIcon(new ImageIcon(i));
this.setLocation(x,y);
this.setBounds(new Rectangle(new Point(x,y),this.getPreferredSize()));
}
public void move()
{
while(true)
{
int p=(int)Math.random()*4+1;
switch (p)
{
case 1:x++;break;
case 2:y++;break;
case 3:x--;break;
case 4:y--;break;
}
this.setLocation(x,y);
this.setBounds(new Rectangle(new Point(x,y),this.getPreferredSize()));
}
}
public void run()
{
move();
}
}
class Player:
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Player extends JLabel implements Runnable{
private BufferedImage i;
private int x,y;
Player()
{
x=y=0;
try{
i=ImageIO.read(new File("m1.jpg"));
}catch (Exception e){}
this.setIcon(new ImageIcon(i));
this.setLocation(x,y);
this.setBounds(new Rectangle(new Point(x,y),this.getPreferredSize()));
}
public void move()
{
int p=(int)Math.random()*4+1;
switch (p)
{
case 1:x++;break;
case 2:y++;break;
case 3:x--;break;
case 4:y--;break;
}
this.setLocation(x,y);
this.setBounds(new Rectangle(new Point(x,y),this.getPreferredSize()));
}
public void run()
{
move();
}
}
main class:
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
Enemy e=new Enemy(150,150);
Player p=new Player();
JFrame f=new JFrame("Test");
f.setSize(300,300);
f.setLayout(null);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.add(e);
f.add(p);
Thread pThread=new Thread(p);
Thread eThread=new Thread(e);
pThread.start();
eThread.start();
while(true)
{
f.setVisible(true);
try
{
Thread.sleep(50);
}catch (Exception q){}
f.remove(p);
f.remove(e);
f.add(p);
f.add(e);
}
}
}
The call to Math.random()
in the Enemy
and Player
classes is always rounded to zero. You probably want:
int p = (int) (Math.random() * 4) + 1;
It is also a good idea to limit the possible values for x
and y
in the move
method (with maxX = frame width - enemy/player width; maxY = frame height - enemy/player height):
switch (p) {
case 1:
if (x < maxX)
x++;
break;
case 2:
if (y < maxY)
y++;
break;
case 3:
if (x > 0)
x--;
break;
case 4:
if (y > 0)
y--;
break;
}
Finally, you could save yourself some work by creating a common class for the Enemy
and Player
classes. Here you can put all shared code, which at this moment could be all code; both objects could be instances of the same class:
GameObject e = new GameObject(150, 150, size - 150, size - 150, "m2.jpg");
GameObject p = new GameObject(100, 100, size - 100, size - 100, "m1.jpg");
The GameObject
class could look like this:
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GameObject extends JLabel implements Runnable {
private BufferedImage i;
private int x, y;
private int maxX;
private int maxY;
public GameObject(int X, int Y, int maxX, int maxY, String imagePath) {
x = X;
y = Y;
this.maxX = maxX;
this.maxY = maxY;
try {
this.i = ImageIO.read(new File(imagePath));
} catch (Exception e) {
e.printStackTrace();
}
this.setIcon(new ImageIcon(this.i));
this.setLocation(x, y);
this.setBounds(new Rectangle(new Point(x, y), this.getPreferredSize()));
}
public void move() {
while (i != null) {
int p = (int) (Math.random() * 4) + 1;
switch (p) {
case 1:
if (x < maxX)
x++;
break;
case 2:
if (y < maxY)
y++;
break;
case 3:
if (x > 0)
x--;
break;
case 4:
if (y > 0)
y--;
break;
}
this.setLocation(x, y);
this.setBounds(new Rectangle(new Point(x, y), this.getPreferredSize()));
}
}
public void run() {
move();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.