3.点击红色的×号
*/
public class MoonDemo {
public static void main(String[] args) {
//1.创建一个窗体
JFrame frame = new JFrame();
//2.给窗体添加标题
frame.setTitle(“Moon and Rocket”);
//3.给窗体添加大小
frame.setSize(1400, 820);
//4.窗体居中显示
frame.setLocationRelativeTo(null);
//类名.静态常量
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//画布
Rocket tj = new Rocket();
//添加画布到面板上
frame.add(tj);
//添加鼠标的监听事件
frame.addMouseListener(tj);
tj.addMouseListener(tj);
//添加键盘的监听事件
frame.addKeyListener(tj);
tj.addKeyListener(tj);
//5.显示窗体
frame.setVisible(true);
}
}
Rocket.java
package cn.tedu.day0401;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class Rocket extends JPanel implements KeyListener,MouseListener,Runnable{
//初始位置
int x = 300;
int y = 200;
//火箭头
int k1 = 500, t1 = 300;
//火箭身体
int k2 = 500, t2 = 300;
//火箭的火
int k3 = 500, t3 = 300;
int k4 = 500, t4 = 300;
int k5 = 500, t5 = 300;
int k6 = 500, t6 = 300;
//王冠
int k7 = 500, t7 = 200;
//月亮
int k9 = 300, t9 = 216;
int ke = 300, te = 216;
//星星
int[] a = new int[300];
int[] b = new int[300];
int position = 0;
public Rocket(){
Thread t = new Thread(this);
t.start();//启动线程
//构造方法:方法名与类同名;无任何返回值类型;无参构造
for(int i = 0 ; i
//随机产生位置
a[i] = (int)(Math.random()*1400);
b[i] = (int)(Math.random()*800);
}
}
public void paint(Graphics g) {
super.paint(g);
//修改底部背景色
this.setBackground(Color.black);
// g 代表画笔
//加logo
//g.setColor(Color.red);
//g.fillOval(x+30, y, 10, 7);
//火箭头
g.setColor(Color.red);
g.fillOval(k1 + 20, t1 + 195, 50, 40);
//火箭身体
g.setColor(Color.lightGray);
g.fillRect(k2 - 52, t2 + 195, 100, 40);
//火箭尾巴
g.setColor(Color.yellow);
g.fillOval(k3 - 90, t3 + 195, 40, 6);
g.fillOval(k4 - 90, t4 + 206, 40, 6);
g.fillOval(k5 - 90, t5 + 217, 40, 6);
g.fillOval(k6 - 90, t6 + 228, 40, 6);
//王冠
g.setColor(Color.yellow);
g.fillRect(k7+1, t7, 47, 15);// 矩形
g.fillOval(k7+1, t7-15, 15, 30); //左椭圆
g.fillOval(k7+15, t7-30, 18, 40);
g.fillOval(k7+32, t7-15, 15, 30);
g.setColor(Color.red);
g.fillOval(k7+20, t7, 10, 10);
g.fillOval(k7+5, t7-10, 4, 7);
g.fillOval(k7+22, t7-20, 4, 7);
g.fillOval(k7+38, t7-10, 4, 7);
//月亮
//外框
g.setColor(Color.ORANGE);
g.fillOval(k9+90, t9, 260, 260);
//画圆
g.setColor(Color.yellow);
g.fillOval(k9+100, t9, 250, 250);
//加眼睛
g.setColor(Color.BLACK);
g.fillOval(ke + 290, te + 120, 25,20);
g.fillOval(ke +230, te + 120, 25, 20);
//嘴巴
g.fillOval(ke + 260, te + 145, 25,20);
g.setColor(Color.ORANGE);
//火箭上面加文字
g.setColor(Color.black);
Font font = new Font(Font.DIALOG,Font.BOLD,15);
g.setFont(font);
g.drawString("火箭!", k1 - 25 , t1 + 220);
//画波纹
g.setColor(Color.yellow);
for (int i = 0; i < a.length;i++) {
g.drawString("+", a[i], b[i]);
}
}
//使用方法!!!
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("111111111");
x=e.getX();
y=e.getY();
k1 = e.getX();
t1 = e.getY();
k2 = e.getX();
t2 = e.getY();
k3 = e.getX();
t3 = e.getY();
k4 = e.getX();
t4 = e.getY();
k5 = e.getX();
t5 = e.getY();
k6 = e.getX();
t6 = e.getY();
//重绘,重复调用paint方法
repaint();
}
public void run(){
System.out.println("start");
while(true){
for(int i=0;i
a[i]--;
if(a[i]<0){
a[i] = 1400;
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
// ctrl + F
// ctrl + O
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
//System.out.println("点击了按钮。");
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
System.out.println("点击了右键");
x=x+10;
k1=k1+10;
k2=k2+10;
k3=k3+10;
k4=k4+10;
k5=k5+10;
k6=k6+10;
ke=ke+2;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
System.out.println("点击了←键");
x=x-10;
k1=k1-10;
k2=k2-10;
k3=k3-10;
k4=k4-10;
k5=k5-10;
k6=k6-10;
ke=ke-2;
}
}
@Override
//键盘松开产生反应
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
完成结果如下图: