我有一个工作班,我可以使用键盘围绕屏幕移动一个圆圈,而其他圆圈以不同的速度在屏幕上移动。
我达到了这一点,我正在试图弄清楚如何制作它以便我能够发现我的形状是否触及了另一种形状,但那是我坚持的部分,因为我没有关于如何使其发挥作用的许多想法。我尝试了各种方法,但大多数方法都不起作用。
public class Test extends Panel implements Runnable, KeyListener, MouseMotionListener {
static final int left = 37;
static final int right = 39;
static final int down = 40;
static final int up = 38;
static boolean leftPress = false;
static boolean rightPress = false;
static boolean upPress = false;
static boolean downPress = false;
/*****/
static int upperLeftX;
static int upperLeftY;
static int lowerLeftX;
static int lowerLeftY;
static int upperRightX;
static int upperRightY;
static int lowerRightX;
static int lowerRightY;
int eUpperLeftX;
int eUpperLeftY;
int eLowerLeftX;
int eLowerLeftY;
int eUpperRightX;
int eUpperRightY;
int eLowerRightX;
int eLowerRightY;
/*****/
static boolean eat = false;
static boolean eaten = false;
static int mouseLocationX;
static int mouseLocationY;
static String mouseLocation = "";
/**/
int x1_;
int y1_;
int eSize;
int eRandSpawn;
int sleep;
Color eColor;
/**/
static int x1 = 250;
static int y1 = 250;
static int size = 30;
static JFrame frame = new JFrame("Fishy");
static Test panel = new Test(500, 500, 9);
// static Test time1 = new Test(1000);
// static Test time2 = new Test(500);
// static Test time3 = new Test(250);
// static Test time4 = new Test(Methods.randNum(6,20),Methods.randNum(0, 450));
// static Test time5 = new Test(Methods.randNum(6,20),Methods.randNum(0, 450));
static Test eFish[] = new Test[20];
/** CONSTRUCTOR **/
public Test() {
sleep = Methods.randNum(6, 50);
y1_ = Methods.randNum(0, 450);
eRandSpawn = Methods.randNum(1, 2);
eColor = Methods.randColor();
eSize = Methods.randNum(5, 120);
if (eRandSpawn == 1) {
x1_ = -100;
} else {
x1_ = 600;
}
eUpperLeftX = x1_;
eUpperLeftY = y1_;
eLowerLeftX = x1_;
eLowerLeftY = y1_ + eSize;
eUpperRightX = x1_ + eSize;
eUpperRightY = y1_;
eLowerRightX = x1_ + eSize;
eLowerRightY = x1_ + eSize;
}
/** CONSTRUCTOR **/
public Test(int width, int length, int minusBy) {
super(width, length, minusBy);
}
/** MAIN **/
public static void main(String args[]) throws InterruptedException {
for (int i = 0; i < 20; i++) {
eFish[i] = new Test();
}
Thread time[] = new Thread[20];
for (int i = 0; i < 20; i++) {
time[i] = new Thread(eFish[i]);
}
// Thread time1_ = new Thread(time1);
// Thread time2_ = new Thread(time2);
// Thread time3_ = new Thread(time3);
// Thread time4_ = new Thread(time4);
// Thread time5_ = new Thread(time5);
frame.add(panel);
frame.addKeyListener(panel);
panel.addMouseMotionListener(panel);
Frame.showFrame(frame);
// time1_.start();
// time2_.start();
// time3_.start();
// time4_.start();
// time5_.start();
for (int i = 0; i < 20; i++) {
time[i].start();
}
while (true) {
if (upPress) {
y1--;
}
if (downPress) {
y1++;
}
if (leftPress) {
x1--;
}
if (rightPress) {
x1++;
}
if (upPress || downPress || leftPress || rightPress) {
upperLeftX = x1;
upperLeftY = y1;
lowerLeftX = x1;
lowerLeftY = y1 + size;
upperRightX = x1 + size;
upperRightY = y1;
lowerRightX = x1 + size;
lowerRightY = x1 + size;
Thread.sleep(6);
frame.repaint();
}
for (int i = 0; i < 20; i++) {
if (eat) {
size++;
eFish[i].eRandSpawn = Methods.randNum(1, 2);
if (eFish[i].eRandSpawn == 1) {
eFish[i].y1_ = Methods.randNum(0, 450);
eFish[i].x1_ = -100;
eFish[i].sleep = Methods.randNum(6, 50);
eFish[i].eSize = Methods.randNum(5, 120);
eFish[i].eColor = Methods.randColor();
} else {
eFish[i].y1_ = Methods.randNum(0, 450);
eFish[i].x1_ = 600;
eFish[i].sleep = Methods.randNum(6, 50);
eFish[i].eSize = Methods.randNum(5, 120);
eFish[i].eColor = Methods.randColor();
}
eFish[i].eUpperLeftX = eFish[i].x1_;
eFish[i].eUpperLeftY = eFish[i].y1_;
eFish[i].eLowerLeftX = eFish[i].x1_;
eFish[i].eLowerLeftY = eFish[i].y1_ + eFish[i].eSize;
eFish[i].eUpperRightX = eFish[i].x1_ + eFish[i].eSize;
eFish[i].eUpperRightY = eFish[i].y1_;
eFish[i].eLowerRightX = eFish[i].x1_ + eFish[i].eSize;
eFish[i].eLowerRightY = eFish[i].x1_ + eFish[i].eSize;
}
eat = false;
}
}
}
/** PAINT **/
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillRect(0, 0, 501, 501);
g.setColor(Color.BLACK);
mouseLocation = String.format("%s,%s,%s", x1, y1, size);
g.drawString(mouseLocation, 100, 100);
g.setColor(Color.GREEN);
g.fillOval(x1, y1, size, size);
// g.drawOval(time4.x1_,time4.y1_,50,50);
// g.drawOval(time5.x1_,time5.y1_,50,50);
for (int i = 0; i < 20; i++) {
g.setColor(eFish[i].eColor);
g.fillOval(eFish[i].x1_, eFish[i].y1_, eFish[i].eSize, eFish[i].eSize);
}
/*
* for (int i = 0; i < 10; i++) { g.setColor(Methods.randColor()); g.drawRect(x1 + i, y1 + i, width - i * 2, height - i * 2);
*
* }
*/
// g.drawString(time1.time, 20, 50);
// g.drawString(time2.time, 20, 100);
// g.drawString(time3.time, 20, 150);
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(sleep);
eUpperLeftX = x1_;
eUpperLeftY = y1_;
eLowerLeftX = x1_;
eLowerLeftY = y1_ + eSize;
eUpperRightX = x1_ + eSize;
eUpperRightY = y1_;
eLowerRightX = x1_ + eSize;
eLowerRightY = x1_ + eSize;
if (x1_ == -150) {
y1_ = Methods.randNum(0, 450);
x1_ = 600;
sleep = Methods.randNum(6, 50);
eSize = Methods.randNum(5, 120);
eColor = Methods.randColor();
}
if (x1_ == 650) {
y1_ = Methods.randNum(0, 450);
x1_ = -100;
sleep = Methods.randNum(6, 50);
eSize = Methods.randNum(5, 120);
eColor = Methods.randColor();
}
if (eRandSpawn == 1) {
x1_++;
} else {
x1_--;
}
frame.repaint();
} catch (InterruptedException e) {
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (up == e.getKeyCode()) {
upPress = true;
}
if (down == e.getKeyCode()) {
downPress = true;
}
if (left == e.getKeyCode()) {
leftPress = true;
}
if (right == e.getKeyCode()) {
rightPress = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (up == e.getKeyCode()) {
upPress = false;
}
if (down == e.getKeyCode()) {
downPress = false;
}
if (left == e.getKeyCode()) {
leftPress = false;
}
if (right == e.getKeyCode()) {
rightPress = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
mouseLocationX = e.getX();
mouseLocationY = e.getY();
mouseLocation = String.format("%s,%s", mouseLocationX, mouseLocationY);
frame.repaint();
}
}
答案 0 :(得分:0)
public boolean madeContact(Ellipse a, Ellipse b) {
//Center of ellipse
int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
//Radii
int arx = a.getRadiusX(), ary = a.getRadiusY();
int brx = b.getRadiusX(), bry = b.getRadiusY();
//Has hit x, y
boolean hitx = Math.abs(ax - bx) < arx+brx;
boolean hity = Math.abs(ay - by) < ary+bry;
return hitx && hity;
}