java 求大神帮我写个小程序,谢谢!(必定追加分数,我只想把分数留给对我有帮助的大神。。3Q!)
谢谢!我数学不太好,不会写。。。在线等。。。。 展开
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Curve extends JFrame {
private Circle circle = new Circle();
/**
* 构造函数
*/
public Curve() {
setTitle("DrawCurve[绘制一份沿着曲线运动的圆形]");
MyPanel panel = new MyPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
pack();
}
public static void main(String[] args) {
Curve c = new Curve();
c.setVisible(true);
}
/**
*
* Circle 主要功能为:圆 记录当前圆的x坐标,y坐标和圆半径
*
*/
public class Circle {
int x = 0;
int y = 0;
int r = 15;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
public class MyPanel extends JPanel {
private static final int WIDTH = 480;
private static final int HEIGHT = 480;
public MyPanel() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
Thread thread = new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
/**
*
* 根据曲线横坐标获取纵坐标值
*
* @param x
*/
public double getY(int x) {
double a = Math.sin(x * Math.PI / 180);
return (80 + 40 * a);
}
/**
*
* 绘制曲线
*
* @param g
*/
public void drawCurve(Graphics g) {
for (int i = 0; i < this.getWidth(); i++) {
g.drawString("*", i, (int) getY(i));
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
drawCurve(g);
drawCircle(g);
}
/**
*
* 以当前的x坐标画一个圆
*
* @param g
*/
public void drawCircle(Graphics g) {
circle.setX(circle.getX() + 1);
g.fillOval(circle.getX(), (int) getY(circle.getX()), circle.getR(),
circle.getR());
}
}
}
类似于这样的东西么?
是的。3Q!
这么我复制到Eclipse无法运行???
看看你的jar包有没有引用上
另外注意类名
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
public class TestApplet extends Applet implements Runnable {
private int x = 0;
private int y = 0;
private int d = 2;
private int r = 20;
Image buffer;
Graphics bufferg;
boolean flag = true;
/**
*
*/
private static final long serialVersionUID = 1L;
public void init() {
this.setSize(500, 500);
new Thread(this).start();
Dimension d = getSize();
buffer = createImage(d.width, d.height);
}
public void start() {
}
public void paint(Graphics g) {
if (x >= 500) {
g.drawString("OVER", 200, 200);
this.flag = false;
return;
}
if (bufferg == null)
bufferg = buffer.getGraphics();
Dimension d = getSize();
bufferg.setColor(Color.white);
bufferg.fillRect(0, 0, d.width, d.height);
bufferg.setColor(Color.red);
bufferg.fillOval(x, y, r, r);
g.drawImage(buffer, 0, 0, this);
x += this.d;
if(x <= 100){
y = (int) (200 - Math.sqrt(Math.abs(100 * 100 - x * x)));
}else if(x <= 300){
y = (int) (200 - Math.sqrt(Math.abs(100 * 100 - (x - 200) * (x - 200))));
}else if(x <= 500){
y = (int) (200 - Math.sqrt(Math.abs(100 * 100 - (x - 400) * (x - 400))));
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while (flag) {
this.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
你粘过去自己看吧 Applet小程序, 也没什么意思
主要是你的运动轨迹怎么计算
不是这种效果,这个我也会写。
类似苍蝇在那里飞来飞去的哪种效果,谢谢!
我也说了, 关键是运动轨迹是怎么计算的
.....
我这个和他那个有什么区别呢, 他是用正弦, 我是用半圆......, 不都是曲线么