小代码 飞翔的小鸟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小代码 飞翔的小鸟相关的知识,希望对你有一定的参考价值。
/********************** 【飞翔的小鸟】 已由DOS环境测试 命令 为 javac Main.java java Main *************************/ import java.awt.*; import java.util.*; import java.awt.event.*; import java.util.List; import javax.swing.JPanel; class wzpoint { int x; int y1; int y2; wzpoint(int xx, int y, int yy) { x = xx; y1 = y; y2 = yy; } public void setX(int x) { this.x = this.x-x; } } public class Main extends JPanel { /*全局變量的解釋 a 用於時間 p傳點 鏈表產生綫條 wzy 飛行高度frame面板*/ static int a = 1; static int wzy = 100; static Point p = null; static List<wzpoint> list = new ArrayList<wzpoint>(); public static Frame frame = new Frame(); public static void main(String[] args) { list.add(new wzpoint(480, 50, 220)); list.add(new wzpoint(240, 100, 220)); list.add(new wzpoint(100, 100, 500)); list.add(new wzpoint(360, 60, 360)); list.add(new wzpoint(280, 150, 300)); Main m = new Main(); m.setSize(new Dimension(800, 666)); m.setVisible(true); frame.add(m); frame.setSize(800, 666); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); m.Action(); frame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); wzy=y; p = new Point(x, y); frame.repaint(); } }); frame.setVisible(true); } private Timer timer; private void Action() { timer = new Timer(); timer.schedule(new TimerTask() { public void run() { int n=5;//速度值 for(int i=0;i<5;i++) { list.get(i).setX(n*5);//改變 x值 使器移動 } if(list.size()<6) { int x=(int) (Math.random()*100)+50; int y=(int) (Math.random()*400)+50; list.add(new wzpoint(x+300,x,y)); } wzpoint wz=list.get(0); if(wz.x<50) list.remove(0); if((wzy>wz.y1&&wzy<wz.y2)||(wzy>wz.y2&&wzy<wz.y1)) { p =new Point(1000,1000); frame.repaint(); } else { frame.repaint(); } } }, new Date(), 1000); } @Override public void paint(Graphics g) { if (a == 1) g.drawString("wz", 60, 200); g.drawString("【欢迎使用飞行机】", 550, 100); g.drawString("点击屏幕上下两状态改变飞机的位置", 520, 120); g.drawString("不点击时屏幕不断刷新新的页面", 520, 140); g.drawLine(50, 50, 500, 50); g.drawLine(50, 50, 50, 500); g.drawLine(500, 50, 500, 500); g.drawLine(50, 500, 500, 500); for (int i = 0; i <5; i++) { wzpoint wz=list.get(i); if (wz.x> 50) { g.drawLine(wz.x, wz.y1, wz.x, wz.y2); } } if (p != null) { if (p.getX()==1000)g.drawString("you had fail once",300,300); if (50 < p.getX() && p.getX() < 500 && 50 < p.getY()&& p.getY() < 500) { g.drawString("wz", 60, (int) p.getY()); } else { g.drawString("【error】", 44, 510); g.drawString("X = " + p.getX(), 44, 520); g.drawString("Y = " + p.getY(), 44, 530); } } g.drawString("我会一直打印时间 " + a + "s", 100, 40); a++; } }
以上是关于小代码 飞翔的小鸟的主要内容,如果未能解决你的问题,请参考以下文章