十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
;
在横峰等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站、外贸网站建设 网站设计制作按需网站开发,公司网站建设,企业网站建设,品牌网站建设,全网营销推广,成都外贸网站建设,横峰网站建设费用合理。
可以这样写:
import javax.swing.*;
import java.awt.*;
class C extends JFrame {
C() {
final Point point = new Point();
add(new JPanel() {
public void paintComponent(Graphics g) {
g.fillOval(point.x, point.y, 20, 20);
point.x = (point.x + 2) % getWidth();
point.y = (point.x * point.x) / 1000; // 造就弧线的简单二次方程。
}
});
setSize(777, 666);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
while (true) {
repaint(point.x, point.y, 50, 50);
try { Thread.sleep(10); } catch (Exception ex) {}
}
}
public static void main(String[] args) {
new C();
}
}
下面这段代码应该符合你的需求
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BounceThread
{ public static void main(String[] args)
{ JFrame frame = new BounceThreadFrame();
frame.show();
}
}
class BounceThreadFrame extends JFrame
{ public BounceThreadFrame()
{ setSize(300, 200);
setTitle("Bounce");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ Ball b = new Ball(canvas);
b.start();
}
});
addButton(p, "Close",
new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title,
ActionListener a)
{ JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
class Ball extends Thread
{ public Ball(JPanel b) { box = b; }
public void draw()
{ Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move()
{ if (!box.isVisible()) return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x 0)
{ x = 0; dx = -dx; }
if (x + XSIZE = d.width)
{ x = d.width - XSIZE; dx = -dx; }
if (y 0)
{ y = 0; dy = -dy; }
if (y + YSIZE = d.height)
{ y = d.height - YSIZE; dy = -dy; }
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run()
{ try
{ draw();
for (int i = 1; i = 1000; i++)
{ move();
sleep(5);
}
}
catch(InterruptedException e) {}
}
private JPanel box;
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
}
抛砖引玉一下
我觉得该题描述了3个事物
1 小球 Ball
2 放小球的容器 BallPanel
3 小球的控制器 BallControler
public class Ball{
double R = 0d; //球的半径
int cx;//当前小球中心X坐标
int cy;//当前小球中心Y坐标
double angle;//小球运动弧度 ,与cx,cy结合起来用来算出 小球移动到当前时的前一个坐标,及移动到下一个点的坐标
int speed;//小球运动速度,毫秒数
}
public class BallPanel{
double width;//放小球的容器宽
double height;//放小球的容器的高
BallControler []BallControlers;//放了多少个小球
}
public class BallControler extends Thread{
Ball tBall;//该控制器控制的小球
BallControler(Ball tBall){
this.tBall = tBall;
}
private int [] getNextPosition(){
/**
返回下一点的x,y坐标
*/
}
private int [] getPreviousPosition(){
/**
返回前一点的x,y坐标
*/
}
private void moveBall(int x,int y){
//移动小球到指定的x,y坐标。
//有必要的话,可以记录小球的运动轨迹到堆栈中
}
public void run(){
Thread.sleep(tBall.speed);//按照小球规定的速度移动
//下面的代码,计算并且移动小球到下一个点,计算是否碰壁(当前小球中心坐标+半径是否超过BallPanel的长,或者高等),并且移动小球
}
}
java是编程语言里比较难学的一门,如果有心从事编程方向的工作,最好到专业机构学习并有更多的项目实践,更贴近市场,这样更有利于将来的发展。
//Checkers.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Checkers类
public class Checkers extends JFrame implements ActionListener {
//变量定义
CheckersPanel checkers = new CheckersPanel();
JButton startButton = new JButton("start");
JButton stopButton = new JButton("stop");
//构造函数
public Checkers(){
super("Checkers");
setSize(210,170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout border = new BorderLayout();
pane.setLayout(border);
pane.add(checkers,"Center");
JPanel buttonPanel = new JPanel();
startButton.addActionListener(this);
buttonPanel.add(startButton);
stopButton.addActionListener(this);
stopButton.setEnabled(false);
buttonPanel.add(stopButton);
pane.add(buttonPanel,"South");
setContentPane(pane);
show();
}
//响应用户动作
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == startButton){
checkers.playAnimation();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}else{
checkers.stopAnimation();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
//主函数
public static void main(String[] arguments){
Checkers ck = new Checkers();
}
}
//CheckersPanel类
class CheckersPanel extends JPanel implements Runnable{
//变量定义
private Thread runner;
int xPos = 5;
int xMove = 4;
//播放动画
void playAnimation(){
if (runner ==null);{
runner = new Thread(this);
runner.start();
}
}
//停止动画
void stopAnimation(){
if (runner !=null);{
runner = null;
}
}
//运行
public void run(){
Thread thisThread = Thread.currentThread();
while(runner ==thisThread){
xPos += xMove;
if ((xPos 105)|(xPos 5))
xMove *= -1;
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
//画图形
public void paintComponent(Graphics comp){
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setColor(Color.blue);
comp2D.fillRect(0,0,100,100);
comp2D.setColor(Color.white);
comp2D.fillRect(100,0,100,100);
comp2D.setColor(Color.black);
comp2D.fillOval(xPos,5,90,90);
}
}