1.毕业论文作品,习惯用java了所以用java做。
2.程序会在for循环中产生不断产生一系列圆的坐标,每迭代一次都会更新这一系列圆的坐标。
3.这个坐标的变化规律是朝着程序希望的方向发展的,这一点我用System.out.print输出的值得到了证实。
4.目前我是利用循环结束后得到的结果返回到Draw,即:
Draw mydraw = new Draw(for循环返回值)
再利用Graphics2D.draw()画出图形
但是这样只能得到最终的图形,怎么能看到for循环中每次迭代更新后的动态图?希望高手指教,谢谢!
2.程序会在for循环中产生不断产生一系列圆的坐标,每迭代一次都会更新这一系列圆的坐标。
3.这个坐标的变化规律是朝着程序希望的方向发展的,这一点我用System.out.print输出的值得到了证实。
4.目前我是利用循环结束后得到的结果返回到Draw,即:
Draw mydraw = new Draw(for循环返回值)
再利用Graphics2D.draw()画出图形
但是这样只能得到最终的图形,怎么能看到for循环中每次迭代更新后的动态图?希望高手指教,谢谢!
里面circle.next()应该相当于你的坐标变化函数。
坐标有变化了,就repaint,那么paint就会被调用,在paint里面重绘。import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JPanel;public class JAppletTest extends JApplet {
static final int INT_PAINT_TIME = 100;
static final int INT_MOVE_TIME = 100;
static final int radius = 10; static final int width = 400;
static final int height = 300; Graphics graphicsPanel;
JPanel panel;
boolean isStart;
Thread threadPaint;
Thread threadMove;
boolean isThreadPaintRun;
boolean isMove;
Circle circle; /**
* Create the applet
*/
public JAppletTest() {
super(); getContentPane().setLayout(null);
setSize(width, height); panel = new JPanel();
panel.setBounds(0, 0, width, height); getContentPane().add(panel);
//
isThreadPaintRun = true;
threadPaint = new Thread(new Runnable() {
public void run() {
while (isThreadPaintRun) {
try {
Thread.sleep(INT_PAINT_TIME);
repaint(); } catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}); isMove = false;
circle = new Circle();
threadMove = new Thread(new Runnable() { public void run() {
while (isThreadPaintRun) {
try {
Thread.sleep(INT_MOVE_TIME);
isMove = circle.next();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}); } public void destroy() {
isThreadPaintRun = false;
} public void init() {
isStart = true;
graphicsPanel = panel.getGraphics();
} public void start() {
isStart = true;
threadPaint.start();
threadMove.start();
} public void stop() {
isStart = false;
} public void paint(Graphics g) {
if (!isStart)
return;
if (!isMove)
return; g.clearRect(0, 0, width, height);
// g.setColor(new Color(255, 0, 255));
// g.fillRect(0, 0, width, height);
// g.setColor(new Color(255, 255, 255));
g.drawOval((int) circle.x, (int) circle.y, radius, radius);
}
class Circle {
double x0;
double y0; double x;
double y; double xx;
double yy; double xxx;
double yyy; double xxxx;
double yyyy; public Circle() {
x = width / 4;
y = height / 4;
x0 = x;
y0 = y; xx = Math.random() - 0.5;
yy = Math.random() - 0.5; } public boolean next() {
xxxx = (Math.random() - 0.5) / 4;
yyyy = (Math.random() - 0.5) / 4; xxx += xxxx;
yyy += yyyy;
xx += xxx;
yy += yyy; x += xx;
y += yy; if (x - radius < 0) {
x = -x;
xx = -xx / 2;
xxx = -xxx;
} else if (x + radius > width) {
x = 2 * width - x;
xx = -xx / 2;
xxx = -xxx;
} if (y - radius < 0) {
y = -y;
yy = -yy / 2;
yyy = -yyy;
} else if (y + radius > height) {
y = 2 * height - y;
yy = -yy / 2;
yyy = -yyy;
} boolean isMove = (((int) (x0) != (int) (x)))
|| (((int) (y0) != (int) (y)));
x0 = x;
y0 = y;
return isMove;
} }}