import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
public class Hunter extends JFrame implements KeyListener{
public class helper extends TimerTask {
@Override
public void run() {
int randomm=(int)(Math.random()*4);
int randommm = (int) ((Math.random() * 490) + 10);
obj obj1 = new Hunter().new obj(randommm, 0);
MyThreadTS k = new MyThreadTS(obj1, getGraphics());
MyThreadS k1 = new MyThreadS(obj1, getGraphics());
MyThreadM k2= new MyThreadM(obj1, getGraphics());
MyThreadF k3 = new MyThreadF(obj1, getGraphics());
MyThreadTF k4 = new MyThreadTF(obj1, getGraphics());
switch (randomm) {
case 0:
k.start();
break;
case 1:
k1.start();
break;
case 2:
k2.start();
break;
case 3:
k3.start();
break;
case 4:
k4.start();
}
}
}
public class obj{
int x,y;
public obj(int x,int y){
this.x=x;
this.y=y;
}
}
public class MyThreadTS extends Thread{
Graphics g;
int x,y;
public MyThreadTS(obj obj,Graphics g){
this.x=obj.x;
this.y=obj.y;
this.g=g;
}
public void run(){
try {
while (y<491) {
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)
||(x+10>=positionX && x+10<=positionX+50)&&
(y+10>=positionY && y+10<=positionY+50)) {
stop();
}
g.setColor(Color.black);
g.fillRect(x, y, 10, 10);
y++;
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class MyThreadS extends Thread{
Graphics g;
int x,y;
public MyThreadS(obj obj,Graphics g){
this.x=obj.x;
this.y=obj.y;
this.g=g;
}
public void run(){
try {
while (y<491) {
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)
||(x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50)) {
stop();
}
g.fillRect(x, y, 10, 10);
y++;
g.setColor(Color.red);
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class MyThreadM extends Thread{
Graphics g;
int x,y;
public MyThreadM(obj obj,Graphics g){
this.x=obj.x;
this.y=obj.y;
this.g=g;
}
public void run(){
try {
while (y<491) {
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)
||(x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50)) {
stop();
}
Thread.sleep(50);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class MyThreadF extends Thread{
int x,y;
Graphics g;
public MyThreadF(obj obj,Graphics g){
this.x=obj.x;
this.y=obj.y;
this.g=g;
}
public void run(){
try {
while (y<491) {
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)
||(x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50)) {
stop();
}
Thread.sleep(25);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class MyThreadTF extends Thread{
int x,y;
Graphics g;
public MyThreadTF(obj obj,Graphics g){
this.x=obj.x;
this.y=obj.y;
this.g=g;
}
public void run(){
try {
while (y <491) {
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)
||(x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50)) {
stop();
}
Thread.sleep(12);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Hunter(){
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);
}
int positionX=250,positionY=250;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
g.fillRect(positionX,positionY,50,50);
g.setColor(Color.red);
}
public static void main(String [] args){
Hunter game=new Hunter();
game.setVisible(true);
Hunter.helper helper=game.new helper();
Timer timer=new Timer();
timer.schedule(helper,0, 300);
}
@Override
public void keyTyped(KeyEvent keyEvent) {}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode=keyEvent.getKeyCode();
if (keyCode==38){
if(positionY<10){
}
else {
positionY -= 10;
repaint();
}
}
else if(keyCode==40){
if (positionY>440){
}
else {
positionY += 10;
repaint();
}
}
else if(keyCode==37){
if (positionX<10){
}
else {
positionX -= 10;
repaint();
}
}
else if(keyCode==39){
if (positionX>440){
}
else {
positionX += 10;
repaint();
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {}
}
在我的代码中,我有一个猎物游戏。蓝色正方形吃掉红色和黑色的矩形。它们每0.5秒随机创建一次,并且在特定的时间内掉下来。但是我在使用红色和黑色的矩形时遇到了麻烦如果我对每种新油漆都使用重涂方法,它将无法正常工作。它们看上去浑浊。我该如何解决?
我想要创建这样的红色和黑色矩形: