Ball
小球在封闭盒子里弹碰的程序。先贴一份我在CSDN上找到的帖子,自行加工后如下
package nlday03;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Ball extends Frame {
public static final int FRAME_WIDTH = 800;
public static final int FRAME_LENGTH = 600;
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取屏幕的尺寸
Dimension screenSize = tk.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
Image image = null; // 定义画布
int x = 0,y = 0;
int flyx = 1,flyy = 1;//speed
public static void main(String[] args) {
new Ball();
}
public void update(Graphics g) {
image = this.createImage(FRAME_WIDTH, FRAME_LENGTH);
Graphics gps = image.getGraphics(); // 获取画布的画笔
g.drawImage(image, 0, 0, null); // 将每次产生的图片覆盖Frame框
g.setColor(Color.black);
g.fillOval(x, y, 50, 50);
if (flyx==1) {
x++;
}
if(flyx ==2) {
x--;
}
if(flyy==1) {
y++;
}
if(flyy==2) {
y--;
}
if(x>730) {
flyx=2;
}
if(x<0) {
flyx=1;
}
if(y<0) {
flyy=1;
}
if(y>530) {
flyy=2;}
}
public Ball() {
this.setVisible(true); // 设置窗口可见
this.setSize(FRAME_WIDTH, FRAME_LENGTH); // 设置窗口大小、设置窗口关闭模式
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// frame窗如何关闭窗口
this.setResizable(false); // 设置窗口大小不可变
this.setLocation(screenWidth / 2 - FRAME_WIDTH / 2, screenSize.height / 2 - FRAME_LENGTH / 2);
this.setBackground(Color.pink);
this.setTitle("和。。一起看球!");
new Thread(new PaintThread()).start();
}
private class PaintThread implements Runnable {
@Override
public void run() {
while (true) { // 绘制、刷新闪烁、当做线程任务去做
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
下面是我自己对老师的程序整理
package nlday03;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Ball2 extends Frame{
public static final int FRAME_WIDTH = 800;
public static final int FRAME_LENGTH = 600;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenWidth = screenSize.width;
int screenLength = screenSize.height;
Image image = null;
int x,y;
int speedx = 6;
int speedy = 6;
public static void main(String[] args) {
new Ball();
}
public void update(Graphics g){
image = this.createImage(FRAME_WIDTH,FRAME_LENGTH);
Graphics gps = image.getGraphics();
framePaint(gps);
g.drawImage(image, 0, 0, null);
}
private void framePaint(Graphics g) {
g.setColor(Color.green);
g.fillOval(x, y, 50, 50);
}
public Ball2(){
x = (int)(Math.random()*700);
y = (int)(Math.random()*500);
this.setTitle("弹跳的小球");
this.setSize(FRAME_WIDTH,FRAME_LENGTH);
this.setLocation(screenWidth / 2 - FRAME_WIDTH / 2, screenLength / 2 - FRAME_LENGTH / 2);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setBackground(Color.gray);
new Thread(new PaintThread()).start();
}
private class PaintThread implements Runnable {// 线程内部类。启动线程,进行绘制
@Override
public void run() {
while (true) {
if(x+50>FRAME_WIDTH){
x = FRAME_WIDTH - 50;
speedx = -speedx;
}
if(x<0){
x=0;
speedx = -speedx;
}
if(y<20){
y=20;
speedy = -speedy;
}
if(y+50>FRAME_LENGTH){
y = FRAME_LENGTH - 50;
speedy = -speedy;
}
x += speedx;
y += speedy;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
拖了两天的程序,终于搞好了。动画里有点瑕疵,只要改变边缘的值就可以。