|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今天是第29天,临到头居然又被卡掉22222222222222222222222222222,就只能记录之前学习的小球游戏了,明天早点做笔记
①
public class Balls {
private int ballX;
private int ballY;
private int BallAtt;
Balls(int ballX,int ballY,int ballAtt){
super();
this.ballX=ballX;
this.ballY=ballY;
this.BallAtt=ballAtt;
}
public int getBallX(){
return ballX;
}
public void setBallX(int ballX){
this.ballX= ballX;
}
public int getBallY(){
return ballY;
}
public void setBallY(int ballY){
this.ballY= ballY;
}
public int getBallAtt(){
return BallAtt;
}
public void setBallAtt(int ballAtt){
this.BallAtt= ballAtt;
}
}
②import javax.swing.JFrame;
import org.omg.CORBA.PRIVATE_MEMBER;
public class MyBallFrame extends JFrame {
private JFrame frame;
public MyBallFrame() {
frame = new JFrame();
}
public void showMe() {
frame.setBounds(200, 200, 300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("反弹小球");
MyBallPanel panel = new MyBallPanel();
frame.add(panel);
Thread thread = new Thread(panel);
thread.start();
frame.setVisible(true);
}
public static void main(String[] args) {
new MyBallFrame().showMe();
}
}
③import java.awt.Graphics;
import javax.swing.JPanel;
public class MyBallPanel extends JPanel implements Runnable {
private int x = 30;
private int y = 30;
private int att = 0;
private Balls ball;
public MyBallPanel() {
ball = new Balls(x, y, att);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// g.clearRect(0, 0, 300, 400);
// g.fillRect(ball.getBallX(), ball.getBallY(), 20, 20);
g.fillOval(ball.getBallX(), ball.getBallY(), 20, 20);
}
@Override
public void run() {
while (true) {
if (ball.getBallAtt() == 0) {
// x++;
// y++;
ball.setBallX(ball.getBallX() + 1);
ball.setBallY(ball.getBallY() + 1);
}
if (ball.getBallAtt() == 1) {
ball.setBallX(ball.getBallX() - 1);
ball.setBallY(ball.getBallY() + 1);
}
if (ball.getBallAtt() == 2) {
ball.setBallX(ball.getBallX() - 1);
ball.setBallY(ball.getBallY() - 1);
}
if (ball.getBallAtt() == 3) {
ball.setBallX(ball.getBallX() + 1);
ball.setBallY(ball.getBallY() - 1);
}
if (ball.getBallX() > 263) {
if (ball.getBallAtt() == 0) {
ball.setBallAtt(1);
} else {
ball.setBallAtt(2);
}
}
if (ball.getBallY() > 341) {
if (ball.getBallAtt() == 1) {
ball.setBallAtt(2);
} else {
ball.setBallAtt(3);
}
}
if (ball.getBallAtt() < 0) {
if (ball.getBallAtt() == 2) {
ball.setBallAtt(3);
} else {
ball.setBallAtt(0);
}
}
if (ball.getBallY() < 0) {
if (ball.getBallAtt() == 3) {
ball.setBallAtt(0);
} else {
ball.setBallAtt(1);
}
}
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
// TODO Auto-generated method stub
}
}
④
java.awt 包含用于创建用户界面和绘制图形图像的所有类。
java.awt.event 提供处理由 AWT 组件所激发的各类事件的接口和类。
.awt 抽象窗口工具包 Abstract Window Toolkit
awt Java提供的用来建立和设置Java的图形用户界面的基本工具
JMenu 菜单的该实现是一个包含 JMenuItem 的弹出窗口
用户选择 JMenuBar 上的项时会显示该 JMenuItem。
AbstractAction是java开发语言中swing软件包中的一个类。
AbstracAction 此类提供 JFC Action 接口的默认实现。
JFC Java Foundation Classes
是一个图形框架(Graphical Framework)
依据此框架可建构出具有移携性(Portable)的Java式GUI图形式使用者界面。
AbstractAction(String name, Icon icon)
用指定描述字符串和指定图标定义一个 Action 对象。
|
评分
-
查看全部评分
|