import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class UFOGame02 { class Bullet { int x, y; Bullet(int x, int y){ this.x = x; this.y = y; } } private JFrame frame = new JFrame("tenQ["); private JPanel pane = (JPanel)frame.getContentPane(); private int w = 400, h = 400; private int ux = 0, udx = 2; private int bx = 170; private LinkedList bullets = new LinkedList(); private Canvas canvas = new Canvas(){ @Override public void update(Graphics g){ paint(g); } @Override public void paint(Graphics g){ Rectangle rec = g.getClipBounds(); w = rec.width; h = rec.height; Image img = createImage(w, h); Graphics ofg = img.getGraphics(); ofg.setColor(Color.RED); ofg.fillOval(ux, 10, 80, 30); ofg.setColor(Color.WHITE); ofg.drawString("U F O", ux + 25, 30); ofg.setColor(Color.BLUE); ofg.fillRect(bx - 30, h - 50, 60, 20); ofg.fillPolygon(new int[]{bx, bx - 15, bx + 15}, new int[]{h - 65, h - 45, h - 45}, 3); Iterator it = bullets.iterator(); while(it.hasNext()){ Bullet b = it.next(); ofg.fillRect(b.x, b.y, 4, 10); b.y -= 3; if(b.y < 10) it.remove(); } g.drawImage(img, 0, 0, this); try{Thread.sleep(20);}catch(Exception ex){} if((udx > 0 && ux >= w - 80) || (udx < 0 && ux <= 0)){ udx = -udx; } ux += udx; repaint(); } }; public UFOGame02(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setBackground(Color.WHITE); canvas.setSize(w, h); canvas.addMouseMotionListener(new MouseMotionAdapter(){ @Override public void mouseMoved(MouseEvent ev){ bx = ev.getX(); if(bx > w - 30) bx = w - 30; else if(bx < 30) bx = 30; } }); canvas.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent ev){ bullets.add(new Bullet(bx - 2, h - 75)); } }); pane.add(canvas); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { G106WinLF.set(); new UFOGame02(); } }