要求一个小球碰到墙就反弹出来,可以用鼠标在墙上随意的画墙,小球在碰到新画的墙也会反弹。不用角度来决定,也就是说不能根据进入角度算 。 下面是我的CODE,但是运行起来效率极底。。CPU都是100%运行。。所以求更好的算法。。感谢大家。。
代码我忘了写注释了,但大体思路是从进入方向判断下一点及附近的点,来决定下一步走向。 代码如下: import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener; public class Map extends JFrame {
int WIDTH = 300;
int HEIGHT = 400;
Map() {
this.setSize(WIDTH , HEIGHT); WoPanel wp = new WoPanel(WIDTH , HEIGHT); Thread t = new Thread(wp);
t.start(); this.addMouseListener(wp);
this.addMouseMotionListener(wp); this.add(wp);
}
public static void main(String[] args) {
Map w = new Map();
w.setVisible(true); }
}
class WoPanel extends JPanel implements Runnable , MouseListener , MouseMotionListener {
private int WIDTH;
private int HEIGHT;
int x = 0;
int y = 0;
int bx = 200;
int by = 200;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int n = 0;
boolean b = false;
boolean xy[][];
Color c = Color.RED; WoPanel(int WIDTH , int HEIGHT) {
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
xy = new boolean[WIDTH][HEIGHT]; n = (int)(Math.random()*4); for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < HEIGHT; j++) {
xy[i][j] = true;
if(i == 0 || j == 0 || i == WIDTH - 10 || j == HEIGHT - 10) {
xy[i][j] = false;
}
}
} x = bx + 5;
y = by + 5;
}
public void paint(Graphics g) {
super.paint(g);
this.setBackground(Color.BLACK);
g.setColor(c);
g.fillOval(bx , by , 10 , 10); g.setColor(Color.GREEN);
for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < HEIGHT; j++) {
if(!xy[i][j]) {
g.fillOval(i , j , 1 , 1);
}
}
}
if(x1 - x2 > 0) {
if(y1 - y2 >0) {
// 左上
g.fillRect(x2 , y2 , x1 - x2 , y1 - y2);
} else {
// 左下
g.fillRect(x2 , y1 , x1 - x2 , y2 - y1);
}
} else {
if(y1 - y2 > 0) {
// 右上
g.fillRect(x1 , y2 , x2 - x1 , y1 - y2);
} else {
// 右下
g.fillRect(x1 , y1 , x2 - x1 , y2 - y1);
}
}
}
public void run() {
while(true) {
move();
try {
Thread.sleep(5);
}
catch(Exception e) { }
}
}
public void move() {
check();
// 右下
if(n == 0) {
x++;
y++;
c = Color.YELLOW;
}
// 右上
if(n == 1) {
x++;
y--;
c = Color.GREEN;
}
// 左上
if(n == 2) {
x--;
y--;
c = Color.BLUE;
}
// 左下
if(n == 3) {
x--;
y++;
c = Color.RED;
} bx = x - 5;
by = y - 5; repaint();
}
public void check() {
if(n == 0) {
if(!xy[x + 1][y + 1]) {
if(!xy[x + 1][y - 1]) {
if(xy[x - 1][y + 1]) {
n = 3;
} else {
n = 2;
}
} else {
if(xy[x - 1][y + 1]){
n = 2;
} else {
n = 1;
}
}
}
} if(n == 1) {
if(!xy[x + 1][y - 1]) {
if(!xy[x + 1][y + 1]) {
if(xy[x - 1][y - 1]) {
n = 2;
} else {
n = 3;
}
} else {
if(xy[x - 1][y - 1]){
n = 3;
} else {
n = 0;
}
}
}
} if(n == 2) {
if(!xy[x - 1][y - 1]) {
if(!xy[x - 1][y + 1]) {
if(xy[x + 1][y - 1]) {
n = 1;
} else {
n = 0;
}
} else {
if(xy[x + 1][y - 1]){
n = 0;
} else {
n = 3;
}
}
}
} if(n == 3) {
if(!xy[x - 1][y + 1]) {
if(!xy[x - 1][y - 1]) {
if(xy[x + 1][y + 1]) {
n = 0;
} else {
n = 1;
}
} else {
if(xy[x + 1][y + 1]) {
n = 1;
} else {
n = 2;
}
}
}
}
}
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
b = true;
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
int tmp = 0;
if(x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
if(y1 > y2) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
for(int i = x1; i < x2; i++) {
for(int j = y1; j < y2; j++) {
xy[i][j] = false;
}
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {
if(b) {
x2 = e.getX();
y2 = e.getY();
repaint();
}
}
public void mouseMoved(MouseEvent e) { }
}
代码我忘了写注释了,但大体思路是从进入方向判断下一点及附近的点,来决定下一步走向。 代码如下: import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener; public class Map extends JFrame {
int WIDTH = 300;
int HEIGHT = 400;
Map() {
this.setSize(WIDTH , HEIGHT); WoPanel wp = new WoPanel(WIDTH , HEIGHT); Thread t = new Thread(wp);
t.start(); this.addMouseListener(wp);
this.addMouseMotionListener(wp); this.add(wp);
}
public static void main(String[] args) {
Map w = new Map();
w.setVisible(true); }
}
class WoPanel extends JPanel implements Runnable , MouseListener , MouseMotionListener {
private int WIDTH;
private int HEIGHT;
int x = 0;
int y = 0;
int bx = 200;
int by = 200;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int n = 0;
boolean b = false;
boolean xy[][];
Color c = Color.RED; WoPanel(int WIDTH , int HEIGHT) {
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
xy = new boolean[WIDTH][HEIGHT]; n = (int)(Math.random()*4); for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < HEIGHT; j++) {
xy[i][j] = true;
if(i == 0 || j == 0 || i == WIDTH - 10 || j == HEIGHT - 10) {
xy[i][j] = false;
}
}
} x = bx + 5;
y = by + 5;
}
public void paint(Graphics g) {
super.paint(g);
this.setBackground(Color.BLACK);
g.setColor(c);
g.fillOval(bx , by , 10 , 10); g.setColor(Color.GREEN);
for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < HEIGHT; j++) {
if(!xy[i][j]) {
g.fillOval(i , j , 1 , 1);
}
}
}
if(x1 - x2 > 0) {
if(y1 - y2 >0) {
// 左上
g.fillRect(x2 , y2 , x1 - x2 , y1 - y2);
} else {
// 左下
g.fillRect(x2 , y1 , x1 - x2 , y2 - y1);
}
} else {
if(y1 - y2 > 0) {
// 右上
g.fillRect(x1 , y2 , x2 - x1 , y1 - y2);
} else {
// 右下
g.fillRect(x1 , y1 , x2 - x1 , y2 - y1);
}
}
}
public void run() {
while(true) {
move();
try {
Thread.sleep(5);
}
catch(Exception e) { }
}
}
public void move() {
check();
// 右下
if(n == 0) {
x++;
y++;
c = Color.YELLOW;
}
// 右上
if(n == 1) {
x++;
y--;
c = Color.GREEN;
}
// 左上
if(n == 2) {
x--;
y--;
c = Color.BLUE;
}
// 左下
if(n == 3) {
x--;
y++;
c = Color.RED;
} bx = x - 5;
by = y - 5; repaint();
}
public void check() {
if(n == 0) {
if(!xy[x + 1][y + 1]) {
if(!xy[x + 1][y - 1]) {
if(xy[x - 1][y + 1]) {
n = 3;
} else {
n = 2;
}
} else {
if(xy[x - 1][y + 1]){
n = 2;
} else {
n = 1;
}
}
}
} if(n == 1) {
if(!xy[x + 1][y - 1]) {
if(!xy[x + 1][y + 1]) {
if(xy[x - 1][y - 1]) {
n = 2;
} else {
n = 3;
}
} else {
if(xy[x - 1][y - 1]){
n = 3;
} else {
n = 0;
}
}
}
} if(n == 2) {
if(!xy[x - 1][y - 1]) {
if(!xy[x - 1][y + 1]) {
if(xy[x + 1][y - 1]) {
n = 1;
} else {
n = 0;
}
} else {
if(xy[x + 1][y - 1]){
n = 0;
} else {
n = 3;
}
}
}
} if(n == 3) {
if(!xy[x - 1][y + 1]) {
if(!xy[x - 1][y - 1]) {
if(xy[x + 1][y + 1]) {
n = 0;
} else {
n = 1;
}
} else {
if(xy[x + 1][y + 1]) {
n = 1;
} else {
n = 2;
}
}
}
}
}
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
b = true;
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
int tmp = 0;
if(x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
}
if(y1 > y2) {
tmp = y1;
y1 = y2;
y2 = tmp;
}
for(int i = x1; i < x2; i++) {
for(int j = y1; j < y2; j++) {
xy[i][j] = false;
}
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {
if(b) {
x2 = e.getX();
y2 = e.getY();
repaint();
}
}
public void mouseMoved(MouseEvent e) { }
}
import javax.swing.*;
import java.awt.*;
public class ball extends JApplet implements Runnable
{
int x,y,w;
int maxx,maxy;
int i,j;
Thread s;
Graphics copy;
public void paint(Graphics g)
{
copy=g.create();
Graphics2D g2d=(Graphics2D)copy;
maxx=getWidth();
maxy=getHeight();
if(x<=0){x=0;i=2;}//当左边的时候
if(y<=0){y=0;j=2;}//当上边的时候
if(x>=maxx-w){x=maxx-w;i=-2;}//当右边的时候
if(y>=maxy-w){y=maxy-w;j=-2;}//当下边的时候
g2d.setColor(Color.black);
g2d.drawArc(x,y,w,w,0,360);
g2d.setColor(Color.red);
g2d.fillArc(x,y,w,w,0,360);
g=g2d;
x+=i;y+=j;
}
public void updata()
{repaint();}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
public void init()
{
x=1;y=1;w=10;i=2;j=2;
maxx=getWidth();
maxy=getHeight();
s=new Thread(this);
s.start();
}
public void start()
{
run();
}
public void run()
{
Thread me = Thread.currentThread();
while (s==me) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
}
try{
getGraphics().clearRect(0,0,maxx,maxy);
}catch(Exception e){}
repaint();
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -5
at pk.WoPanel.mouseReleased(Test.java:241)
at java.awt.Component.processMouseEvent(Component.java:5488)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Window.processEvent(Window.java:1161)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -14
at pk.WoPanel.mouseReleased(Test.java:241)
at java.awt.Component.processMouseEvent(Component.java:5488)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Window.processEvent(Window.java:1161)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -16
at pk.WoPanel.mouseReleased(Test.java:241)
at java.awt.Component.processMouseEvent(Component.java:5488)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Window.processEvent(Window.java:1161)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -13
at pk.WoPanel.mouseReleased(Test.java:241)
at java.awt.Component.processMouseEvent(Component.java:5488)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Window.processEvent(Window.java:1161)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)