public Coordinate move(Coordinate oldCoordinate); public Coordinate move(double x, double y); } package csdn.action.impl;import csdn.action.IMove; import csdn.model.Coordinate;//飘动类 public class FlutterMove implements IMove { private boolean isTouchMaxX; private boolean isTouchMaxY; private double minX = 0; private double maxX = 1280; private double minY = 0; private double maxY = 800; public FlutterMove(double maxX, double maxY) { this.maxX = maxX; this.maxY = maxY; } public Coordinate move(Coordinate oldCoordinate) { return move(oldCoordinate.getX(), oldCoordinate.getY()); } private void init(double x, double y) { if (x >= maxX) { isTouchMaxX = true; } if (x <= minX) { isTouchMaxX = false; } if (y >= maxY) { isTouchMaxY = true; } if (y <= minY) { isTouchMaxY = false; } } public Coordinate move(double x, double y) { double newX = x; double newY = y; init(x, y); // 边界初始化 // x<maxX x++,当x>=maxX后就实现x--一直减到0 if (!isTouchMaxX) { newX++; } else { newX--; } // y<maxY y++,y>=maxY后y--,一直减到0,在开始加 if (!isTouchMaxY) { newY++; } else { newY--; } return new Coordinate(newX, newY); } }package csdn.test;import csdn.action.IMove; import csdn.action.impl.FlutterMove; import csdn.model.Coordinate;public class Test extends Thread { private double initX; private double initY; private IMove move; public Test(double initX, double initY, double maxX, double maxY) { this.initX = initX; this.initY = initY; move = new FlutterMove(maxX, maxY); } public void run() { Coordinate c = new Coordinate(initX, initY); while (true) { try { c = move.move(c); System.out.println(c); sleep(100); //100毫秒执行一次 } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Test test = new Test(8,4 ,12,8); test.run();
//Test test2 = new Test(1270,600 ,1280,800); //test2.run(); } }
将Test类稍微修改一下:package csdn.test;import csdn.action.IMove; import csdn.action.impl.FlutterMove; import csdn.model.Coordinate;public class Test extends Thread { private IMove moveObj; private Coordinate coordinate; public Test(double initX, double initY, double maxX, double maxY) { coordinate = new Coordinate(initX, initY); moveObj = new FlutterMove(maxX, maxY); } public void run() { while (true) { try { coordinate = moveObj.move(coordinate.getX(), coordinate.getY()); //或使用 coordinate = move.move(coordinate); System.out.println(coordinate); sleep(100); // 100毫秒执行一次 } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Test test = new Test(8, 4, 12, 8); test.run(); // Test test2 = new Test(1270,600 ,1280,800); // test2.run(); }}
这个涉及到多线程的内容,你可以详细看看Core Java第14章那两个例子,会有很大启发的,下面是我仿写的一个程序,希望对你有帮助 import java.awt.*; import java.awt.event.*; import javax.swing.*;public class TestFrame extends JFrame { public static int DEFAULT_WIDTH = 800; public static int DEFAULT_HEIGHT = 600; public static int BUTTON_WIDTH = 30; public static int BUTTON_HEIGHT = 20; public static int DELAY = 10; private JButton button = new JButton("button"); private JPanel panel = new JPanel(); private int x = 0; private int y = 0; private int dx = 1; private int dy = 1;
public TestFrame() { setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); add(panel); panel.add(button); new Thread(new TestRunnable()).start(); }
class TestRunnable implements Runnable { public void run() { while (true) { button.setBounds(x, y, BUTTON_WIDTH, BUTTON_HEIGHT); if (x <= 0) dx = 1; if (y <= 0) dy = 1; if (x + BUTTON_WIDTH >= panel.getBounds().width) dx = -1; if (y + BUTTON_HEIGHT >= panel.getBounds().height) dy = -1; x += dx; y += dy; try { Thread.sleep(DELAY); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
}
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new TestFrame(); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } }
为了更好体现OO的封装性,Test类可改成如下:package csdn.test;import csdn.action.IMove; import csdn.action.impl.FlutterMove; import csdn.model.Coordinate;public class Test extends Thread { private IMove moveObj; private Coordinate coordinate; public Test(Coordinate coordinate, IMove moveObj) { this.coordinate = coordinate; this.moveObj = moveObj; } public void run() { while (true) { try { System.out.println(coordinate); coordinate = moveObj.move(coordinate.getX(), coordinate.getY()); // 或使用 coordinate = move.move(coordinate); sleep(100); // 100毫秒执行一次 } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Coordinate initCoordinate1 = new Coordinate(0, 0); IMove moveObj1 = new FlutterMove(12, 8); Test test1 = new Test(initCoordinate1, moveObj1); test1.run();// // 测试以下4句时请注释上4句 // Coordinate initCoordinate2 = new Coordinate(1270, 700); // IMove moveObj2 = new FlutterMove(1280, 800); // Test test2 = new Test(initCoordinate2, moveObj2); // test2.run(); } }
2).定义一个接口IMove, 接口中定义一个抽象方法,名字可取为move,参数是x,y,或是一个坐标对象,返回类型为一个坐标对象.
3).定义一个飘动类Flutter,实现IMove.此类中除了需要实现move方法外,需要定义4个布尔边界以进行移动方法的逻辑判断。
4).再定义一个多线程类,通过多态方法调用飘动类的move方法,用老的坐标获取新的坐标即可。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class FloatingButton extends JFrame implements ActionListener{ private JButton component = new JButton();
private int toRight = 1;
private int toBottom = 1; public FloatingButton() {
add(component);
Timer timer = new Timer(20, this);
timer.start();
component.setBounds(0, 0, 10, 10);
} public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new FloatingButton();
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
} @Override
public void actionPerformed(ActionEvent e) {
int x = component.getX();
int y = component.getY();
x += toRight;
y += toBottom;
if (x<1 || x>getContentPane().getWidth()-component.getWidth()-1)
toRight = -toRight;
if (y<1 || y>getContentPane().getHeight()-component.getHeight()-1)
toBottom = -toBottom;
component.setBounds(x, y, 10, 10);
}
}还不是很完美,自己改吧
package csdn.model;//坐标类
public class Coordinate {
private double x;
private double y;
public Coordinate() {
}
public Coordinate(double x, double y){
this.x = x;
this.y = y;
} public double getX() {
return x;
} public void setX(double x) {
this.x = x;
} public double getY() {
return y;
} public void setY(double y) {
this.y = y;
}
public String toString() {
return "x="+x+",y="+y;
}
}package csdn.action;import csdn.model.Coordinate;public interface IMove {
public Coordinate move(Coordinate oldCoordinate); public Coordinate move(double x, double y);
}
package csdn.action.impl;import csdn.action.IMove;
import csdn.model.Coordinate;//飘动类
public class FlutterMove implements IMove { private boolean isTouchMaxX;
private boolean isTouchMaxY; private double minX = 0;
private double maxX = 1280;
private double minY = 0;
private double maxY = 800; public FlutterMove(double maxX, double maxY) {
this.maxX = maxX;
this.maxY = maxY;
} public Coordinate move(Coordinate oldCoordinate) {
return move(oldCoordinate.getX(), oldCoordinate.getY());
} private void init(double x, double y) {
if (x >= maxX) {
isTouchMaxX = true;
} if (x <= minX) {
isTouchMaxX = false;
} if (y >= maxY) {
isTouchMaxY = true;
}
if (y <= minY) {
isTouchMaxY = false;
}
} public Coordinate move(double x, double y) {
double newX = x;
double newY = y; init(x, y); // 边界初始化 // x<maxX x++,当x>=maxX后就实现x--一直减到0
if (!isTouchMaxX) {
newX++;
} else {
newX--;
} // y<maxY y++,y>=maxY后y--,一直减到0,在开始加
if (!isTouchMaxY) {
newY++;
} else {
newY--;
} return new Coordinate(newX, newY);
}
}package csdn.test;import csdn.action.IMove;
import csdn.action.impl.FlutterMove;
import csdn.model.Coordinate;public class Test extends Thread { private double initX;
private double initY; private IMove move; public Test(double initX, double initY, double maxX, double maxY) {
this.initX = initX;
this.initY = initY;
move = new FlutterMove(maxX, maxY);
} public void run() {
Coordinate c = new Coordinate(initX, initY);
while (true) {
try {
c = move.move(c);
System.out.println(c);
sleep(100); //100毫秒执行一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Test test = new Test(8,4 ,12,8);
test.run();
//Test test2 = new Test(1270,600 ,1280,800);
//test2.run();
}
}
import csdn.action.impl.FlutterMove;
import csdn.model.Coordinate;public class Test extends Thread {
private IMove moveObj;
private Coordinate coordinate; public Test(double initX, double initY, double maxX, double maxY) {
coordinate = new Coordinate(initX, initY);
moveObj = new FlutterMove(maxX, maxY);
} public void run() {
while (true) {
try {
coordinate = moveObj.move(coordinate.getX(), coordinate.getY()); //或使用 coordinate = move.move(coordinate);
System.out.println(coordinate);
sleep(100); // 100毫秒执行一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Test test = new Test(8, 4, 12, 8);
test.run(); // Test test2 = new Test(1270,600 ,1280,800);
// test2.run();
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestFrame extends JFrame
{
public static int DEFAULT_WIDTH = 800;
public static int DEFAULT_HEIGHT = 600;
public static int BUTTON_WIDTH = 30;
public static int BUTTON_HEIGHT = 20;
public static int DELAY = 10;
private JButton button = new JButton("button");
private JPanel panel = new JPanel();
private int x = 0;
private int y = 0;
private int dx = 1;
private int dy = 1;
public TestFrame()
{
setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
add(panel);
panel.add(button);
new Thread(new TestRunnable()).start();
}
class TestRunnable implements Runnable
{
public void run()
{
while (true)
{
button.setBounds(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
if (x <= 0)
dx = 1;
if (y <= 0)
dy = 1;
if (x + BUTTON_WIDTH >= panel.getBounds().width)
dx = -1;
if (y + BUTTON_HEIGHT >= panel.getBounds().height)
dy = -1;
x += dx;
y += dy;
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new TestFrame();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
import csdn.action.impl.FlutterMove;
import csdn.model.Coordinate;public class Test extends Thread { private IMove moveObj;
private Coordinate coordinate; public Test(Coordinate coordinate, IMove moveObj) {
this.coordinate = coordinate;
this.moveObj = moveObj;
} public void run() {
while (true) {
try {
System.out.println(coordinate);
coordinate = moveObj.move(coordinate.getX(), coordinate.getY());
// 或使用 coordinate = move.move(coordinate);
sleep(100); // 100毫秒执行一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Coordinate initCoordinate1 = new Coordinate(0, 0);
IMove moveObj1 = new FlutterMove(12, 8);
Test test1 = new Test(initCoordinate1, moveObj1);
test1.run();// // 测试以下4句时请注释上4句
// Coordinate initCoordinate2 = new Coordinate(1270, 700);
// IMove moveObj2 = new FlutterMove(1280, 800);
// Test test2 = new Test(initCoordinate2, moveObj2);
// test2.run();
}
}