1 package MyTest; 2 3 import java.awt.*; 4 import java.awt.event.KeyEvent; 5 import java.awt.event.KeyListener; 6 import java.awt.event.MouseEvent; 7 import java.awt.event.MouseListener; 8 9 import javax.swing.*; 10 11 public class WuGui{ 12 public static void main(String args[]){ 13 ImageIcon image=new ImageIcon("source/1.jpg"); 14 JLabel label=new JLabel(image); 15 16 JFrame w=new JFrame(); 17 mypanel mp=new mypanel(); 18 w.setSize(1366,768); 19 20 //设置背景图片 21 JPanel defaultPane=(JPanel)w.getContentPane(); 22 defaultPane.setOpaque(false); 23 label.setSize(image.getIconWidth(), image.getIconHeight()); 24 w.getLayeredPane().setLayout(null); 25 w.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE)); 26 27 mp.setBackground(Color.WHITE); 28 mp.setOpaque(false);//将容器的颜色设置为透明 29 w.add(mp); 30 31 w.addKeyListener(mp); 32 mp.addKeyListener(mp); 33 w.addMouseListener(mp); 34 mp.addMouseListener(mp); 35 Thread t=new Thread(mp); 36 t.start(); 37 38 w.setVisible(true); 39 40 } 41 } 42 class mypanel extends JPanel implements Runnable,KeyListener,MouseListener{ 43 int x=150;int y=180; 44 int []a=new int[255]; 45 int []b=new int[255]; 46 int []c=new int[255]; 47 int i; 48 public mypanel(){ 49 for(int i=0;i<1;i++){ 50 a[i]=(int)(Math.random()*255); 51 b[i]=(int)(Math.random()*255); 52 c[i]=(int)(Math.random()*255); 53 } 54 } 55 public void paint(Graphics g){ 56 super.paint(g); 57 g.setColor(new Color(91,207,48)); 58 g.fillOval(x-8, y-15, 31, 41);//142,165 59 g.fillOval(x+95, y-15, 31, 41);//245,165 60 g.fillOval(x-8, y+120, 31, 41);//142,300 61 g.fillOval(x+95, y+120, 31, 41);//245,300 62 g.fillOval(x+46, y-60, 35, 70);//196,120 63 g.fillOval(x+52, y+140, 11, 50);//202,320 64 65 66 g.setColor(new Color(47,104,54)); 67 g.fillOval(x, y, 120, 150);//big龟壳150,180 68 g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); 69 //g.setColor(new Color(91,207,48)); 70 //g.setColor(new Color(a[i],b[i],c[i])); 71 g.fillOval(x+7, y+7, 106, 136);//small 157,187 72 73 g.setColor(Color.BLACK); 74 g.fillOval(x+45, y-45, 10, 10);//195,135 75 g.fillOval(x+70, y-45, 10, 10);//220,135 76 /* 77 g.drawLine(194, 228, 226, 228); 78 g.drawLine(194, 276, 226, 276); 79 g.drawLine(180, 252, 194, 228); 80 g.drawLine(194, 276, 180, 252); 81 g.drawLine(226, 228, 240, 252); 82 g.drawLine(226, 276, 240, 252); 83 84 g.drawLine(194, 228, 172, 210); 85 g.drawLine(226, 228, 248, 210); 86 g.drawLine(180, 252, 158, 260); 87 g.drawLine(240, 252, 262, 260); 88 g.drawLine(194, 276, 170, 300); 89 g.drawLine(226, 276, 250, 300); 90 */ 91 g.drawLine(x+44, y+48, x+76, y+48); 92 g.drawLine(x+44, y+96, x+76, y+96); 93 g.drawLine(x+30, y+72, x+44, y+48); 94 g.drawLine(x+44, y+96, x+30, y+72); 95 g.drawLine(x+76, y+48, x+90, y+72); 96 g.drawLine(x+76, y+96, x+90, y+72); 97 98 g.drawLine(x+44, y+48, x+22, y+30); 99 g.drawLine(x+76, y+48, x+98, y+30); 100 g.drawLine(x+30, y+72, x+8, y+80); 101 g.drawLine(x+90, y+72, x+112, y+80); 102 g.drawLine(x+44, y+96, x+20, y+120); 103 g.drawLine(x+76, y+96, x+100, y+120); 104 } 105 public void run(){ 106 while(true){ 107 try{ 108 109 110 Thread.sleep(240); 111 }catch(Exception e){} 112 repaint(); 113 } 114 } 115 public void keyPressed(KeyEvent e) { 116 if(e.getKeyCode()==KeyEvent.VK_LEFT){ 117 if(x>0){ 118 x=x-5;} 119 } 120 if(e.getKeyCode()==KeyEvent.VK_UP){ 121 if(y>0){ 122 y=y-5; 123 } 124 } 125 if(e.getKeyCode()==KeyEvent.VK_RIGHT){ 126 if(x<1200){ 127 x=x+5; 128 } 129 } 130 if(e.getKeyCode()==KeyEvent.VK_DOWN){ 131 if(y<650){ 132 y=y+5; 133 } 134 } 135 repaint(); 136 } 137 public void keyReleased(KeyEvent e) { 138 // TODO Auto-generated method stub 139 140 } 141 public void keyTyped(KeyEvent e) { 142 // TODO Auto-generated method stub 143 144 } 145 public void mouseClicked(MouseEvent e) { 146 // TODO Auto-generated method stub 147 x=e.getX(); 148 y=e.getY(); 149 repaint(); 150 } 151 public void mouseEntered(MouseEvent arg0) { 152 // TODO Auto-generated method stub 153 154 } 155 public void mouseExited(MouseEvent arg0) { 156 // TODO Auto-generated method stub 157 158 } 159 public void mousePressed(MouseEvent e) { 160 // TODO Auto-generated method stub 161 162 } 163 public void mouseReleased(MouseEvent e) { 164 // TODO Auto-generated method stub 165 int x1=e.getX();int y1=e.getY(); 166 if(x1>100){ 167 repaint(); 168 //System.out.println("救命啊!!!"); 169 } 170 } 171 }
时间: 2024-11-16 21:58:41