0

这是我的代码实际上它只是一个移动的太空入侵者游戏。我在互联网上搜索我尝试过双缓冲的动画,它消除了闪烁,但它破坏了整个输出。它还具有 java 使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。这太难了……

import javax.sound.sampled.*;
import java.applet.*;
import java.awt.*;

public class SpaceInvaders extends Applet implements Runnable{


AudioClip soundFile1; 
int Xposition=500,Xposition1=-500,Yposition=15;
Thread runner;

public void start()
{
if (runner == null);
{
runner = new Thread(this);
runner.start();
}
}

public void stop()
{
if (runner != null);
{
runner.stop();
runner = null;

}
}

public void run()
{
while(true)
{
setBackground(Color.black);
repaint();
try {Thread.sleep(20);}
catch(InterruptedException e) { }
}
}

public void init() 
{ 
soundFile1 = getAudioClip(getDocumentBase(),"si.au"); 
//soundFile1.play(); 
soundFile1.loop();
}   
public void paint( Graphics g ) {   
  g.setColor( Color.orange );
  g.fillRect(Xposition+50,10,10,10);
  g.fillRect(Xposition+60,10,10,10);
  g.fillRect(Xposition+40,20,10,10);
  g.fillRect(Xposition+50,20,10,10);
  g.fillRect(Xposition+60,20,10,10);
  g.fillRect(Xposition+70,20,10,10);
  g.fillRect(Xposition+30,30,10,10);
  g.fillRect(Xposition+40,30,10,10);
  g.fillRect(Xposition+50,30,10,10);
  g.fillRect(Xposition+60,30,10,10);
  g.fillRect(Xposition+70,30,10,10);
  g.fillRect(Xposition+80,30,10,10);
  g.fillRect(Xposition+20,40,10,10);
  g.fillRect(Xposition+30,40,10,10);
  g.fillRect(Xposition+50,40,10,10);
  g.fillRect(Xposition+60,40,10,10);
  g.fillRect(Xposition+80,40,10,10);
  g.fillRect(Xposition+90,40,10,10);
  g.fillRect(Xposition+20,50,10,10);
  g.fillRect(Xposition+30,50,10,10);
  g.fillRect(Xposition+40,50,10,10);
  g.fillRect(Xposition+50,50,10,10);
  g.fillRect(Xposition+60,50,10,10);
  g.fillRect(Xposition+70,50,10,10);
  g.fillRect(Xposition+80,50,10,10);
  g.fillRect(Xposition+90,50,10,10);
  g.fillRect(Xposition+40,60,10,10);
  g.fillRect(Xposition+70,60,10,10);
  g.fillRect(Xposition+30,70,10,10);
  g.fillRect(Xposition+50,70,10,10);
  g.fillRect(Xposition+60,70,10,10);
  g.fillRect(Xposition+80,70,10,10);
  g.fillRect(Xposition+20,80,10,10);
  g.fillRect(Xposition+40,80,10,10);
  g.fillRect(Xposition+70,80,10,10);
  g.fillRect(Xposition+90,80,10,10);

  g.setColor(Color.red);
  g.fillRect(Xposition+140,10,10,10);
  g.fillRect(Xposition+200,10,10,10);
  g.fillRect(Xposition+120,20,10,10);
  g.fillRect(Xposition+150,20,10,10);
  g.fillRect(Xposition+190,20,10,10);
  g.fillRect(Xposition+220,20,10,10);
  g.fillRect(Xposition+120,30,10,10);
  g.fillRect(Xposition+140,30,10,10);
  g.fillRect(Xposition+150,30,10,10);
  g.fillRect(Xposition+160,30,10,10);
  g.fillRect(Xposition+170,30,10,10);
  g.fillRect(Xposition+180,30,10,10);
  g.fillRect(Xposition+190,30,10,10);
  g.fillRect(Xposition+200,30,10,10);
  g.fillRect(Xposition+220,30,10,10);
  g.fillRect(Xposition+120,40,10,10);
  g.fillRect(Xposition+130,40,10,10);
  g.fillRect(Xposition+140,40,10,10);
  g.fillRect(Xposition+160,40,10,10);
  g.fillRect(Xposition+170,40,10,10);
  g.fillRect(Xposition+180,40,10,10);
  g.fillRect(Xposition+200,40,10,10);
  g.fillRect(Xposition+210,40,10,10);
  g.fillRect(Xposition+220,40,10,10);
  g.fillRect(Xposition+120,50,10,10);
  g.fillRect(Xposition+130,50,10,10);
  g.fillRect(Xposition+140,50,10,10);
  g.fillRect(Xposition+150,50,10,10);
  g.fillRect(Xposition+160,50,10,10);
  g.fillRect(Xposition+170,50,10,10);
  g.fillRect(Xposition+180,50,10,10);
  g.fillRect(Xposition+190,50,10,10);
  g.fillRect(Xposition+200,50,10,10);
  g.fillRect(Xposition+210,50,10,10);
  g.fillRect(Xposition+220,50,10,10);
  g.fillRect(Xposition+130,60,10,10);
  g.fillRect(Xposition+140,60,10,10);
  g.fillRect(Xposition+150,60,10,10);
  g.fillRect(Xposition+160,60,10,10);
  g.fillRect(Xposition+170,60,10,10);
  g.fillRect(Xposition+180,60,10,10);
  g.fillRect(Xposition+190,60,10,10);
  g.fillRect(Xposition+200,60,10,10);
  g.fillRect(Xposition+210,60,10,10);
  g.fillRect(Xposition+140,70,10,10);
  g.fillRect(Xposition+200,70,10,10);
  g.fillRect(Xposition+130,80,10,10);
  g.fillRect(Xposition+210,80,10,10);



  g.setColor(Color.blue);
  g.fillRect(Xposition+280,10,10,10);
  g.fillRect(Xposition+290,10,10,10);
  g.fillRect(Xposition+270,20,10,10);
  g.fillRect(Xposition+280,20,10,10);           
  g.fillRect(Xposition+290,20,10,10);
  g.fillRect(Xposition+300,20,10,10);
  g.fillRect(Xposition+260,30,10,10);
  g.fillRect(Xposition+270,30,10,10);
  g.fillRect(Xposition+280,30,10,10);
  g.fillRect(Xposition+290,30,10,10);
  g.fillRect(Xposition+300,30,10,10);
  g.fillRect(Xposition+310,30,10,10);
  g.fillRect(Xposition+250,40,10,10);
  g.fillRect(Xposition+260,40,10,10);
  g.fillRect(Xposition+280,40,10,10);
  g.fillRect(Xposition+290,40,10,10);
  g.fillRect(Xposition+310,40,10,10);
  g.fillRect(Xposition+320,40,10,10);
  g.fillRect(Xposition+250,50,10,10);
  g.fillRect(Xposition+260,50,10,10);
  g.fillRect(Xposition+270,50,10,10);
  g.fillRect(Xposition+280,50,10,10);
  g.fillRect(Xposition+290,50,10,10);
  g.fillRect(Xposition+300,50,10,10);
  g.fillRect(Xposition+310,50,10,10);
  g.fillRect(Xposition+320,50,10,10);               //20=250 30=260 40=270 50=280 60=290 70=300 80=310 90=320
  g.fillRect(Xposition+260,60,10,10);
  g.fillRect(Xposition+280,60,10,10);
  g.fillRect(Xposition+290,60,10,10);
  g.fillRect(Xposition+310,60,10,10);
  g.fillRect(Xposition+250,70,10,10);
  g.fillRect(Xposition+320,70,10,10);
  g.fillRect(Xposition+260,80,10,10);
  g.fillRect(Xposition+310,80,10,10);


  g.setColor(Color.yellow);
  g.fillRect(Xposition+370,10,10,10);           
  g.fillRect(Xposition+430,10,10,10);
  g.fillRect(Xposition+380,20,10,10);
  g.fillRect(Xposition+420,20,10,10);
  g.fillRect(Xposition+370,30,10,10);
  g.fillRect(Xposition+380,30,10,10);
  g.fillRect(Xposition+390,30,10,10);
  g.fillRect(Xposition+400,30,10,10);
  g.fillRect(Xposition+410,30,10,10);
  g.fillRect(Xposition+420,30,10,10);
  g.fillRect(Xposition+430,30,10,10);
  g.fillRect(Xposition+360,40,10,10);
  g.fillRect(Xposition+370,40,10,10);
  g.fillRect(Xposition+390,40,10,10);
  g.fillRect(Xposition+400,40,10,10);
  g.fillRect(Xposition+410,40,10,10);
  g.fillRect(Xposition+430,40,10,10);
  g.fillRect(Xposition+440,40,10,10);
  g.fillRect(Xposition+350,50,10,10);
  g.fillRect(Xposition+360,50,10,10);
  g.fillRect(Xposition+370,50,10,10);
  g.fillRect(Xposition+380,50,10,10);
  g.fillRect(Xposition+390,50,10,10);
  g.fillRect(Xposition+400,50,10,10);
  g.fillRect(Xposition+410,50,10,10);
  g.fillRect(Xposition+420,50,10,10);
  g.fillRect(Xposition+430,50,10,10);
  g.fillRect(Xposition+440,50,10,10);
  g.fillRect(Xposition+450,50,10,10);       //120=350 130=360 140=370 150=380 160=390 170=400 180=410 190=420 200=430 210=440 220=450
  g.fillRect(Xposition+350,60,10,10);
  g.fillRect(Xposition+370,60,10,10);
  g.fillRect(Xposition+380,60,10,10);
  g.fillRect(Xposition+390,60,10,10);
  g.fillRect(Xposition+400,60,10,10);
  g.fillRect(Xposition+410,60,10,10);
  g.fillRect(Xposition+420,60,10,10);
  g.fillRect(Xposition+430,60,10,10);
  g.fillRect(Xposition+450,60,10,10);
  g.fillRect(Xposition+350,70,10,10);
  g.fillRect(Xposition+370,70,10,10);
  g.fillRect(Xposition+430,70,10,10);
  g.fillRect(Xposition+450,70,10,10);
  g.fillRect(Xposition+380,80,10,10);
  g.fillRect(Xposition+390,80,10,10);
  g.fillRect(Xposition+410,80,10,10);
  g.fillRect(Xposition+420,80,10,10);


   Xposition--;
     if (Xposition < -500)
  {
Xposition = 500;
}

 g.setColor(Color.green);
 g.fillRect(Xposition1+230,130,10,10);
 g.fillRect(Xposition1+240,130,10,10);
 g.fillRect(Xposition1+250,130,10,10);
 g.fillRect(Xposition1+260,130,10,10);
 g.fillRect(Xposition1+270,130,10,10);
 g.fillRect(Xposition1+280,130,10,10);  
 g.fillRect(Xposition1+210,140,10,10);
 g.fillRect(Xposition1+220,140,10,10);
 g.fillRect(Xposition1+230,140,10,10);
 g.fillRect(Xposition1+240,140,10,10);
   g.fillRect(Xposition1+250,140,10,10);
  g.fillRect(Xposition1+260,140,10,10);
  g.fillRect(Xposition1+270,140,10,10);
  g.fillRect(Xposition1+280,140,10,10);
  g.fillRect(Xposition1+290,140,10,10);
  g.fillRect(Xposition1+300,140,10,10);
  g.fillRect(Xposition1+200,150,10,10);
 g.fillRect(Xposition1+210,150,10,10);
  g.fillRect(Xposition1+220,150,10,10);
 g.fillRect(Xposition1+230,150,10,10);
  g.fillRect(Xposition1+240,150,10,10);
   g.fillRect(Xposition1+250,150,10,10);
g.fillRect(Xposition1+260,150,10,10);
g.fillRect(Xposition1+270,150,10,10);
g.fillRect(Xposition1+280,150,10,10);
g.fillRect(Xposition1+290,150,10,10);
g.fillRect(Xposition1+300,150,10,10);
g.fillRect(Xposition1+310,150,10,10);
g.fillRect(Xposition1+190,160,10,10);
g.fillRect(Xposition1+200,160,10,10);
g.fillRect(Xposition1+220,160,10,10);
g.fillRect(Xposition1+230,160,10,10);
g.fillRect(Xposition1+250,160,10,10);
g.fillRect(Xposition1+260,160,10,10);
g.fillRect(Xposition1+280,160,10,10);
g.fillRect(Xposition1+290,160,10,10);
g.fillRect(Xposition1+310,160,10,10);
g.fillRect(Xposition1+320,160,10,10);
g.fillRect(Xposition1+180,170,10,10);
g.fillRect(Xposition1+190,170,10,10);
g.fillRect(Xposition1+200,170,10,10);
g.fillRect(Xposition1+210,170,10,10);
g.fillRect(Xposition1+220,170,10,10);
g.fillRect(Xposition1+230,170,10,10);
g.fillRect(Xposition1+240,170,10,10);
g.fillRect(Xposition1+250,170,10,10);
g.fillRect(Xposition1+260,170,10,10);
g.fillRect(Xposition1+270,170,10,10);
g.fillRect(Xposition1+280,170,10,10);
g.fillRect(Xposition1+290,170,10,10);
g.fillRect(Xposition1+300,170,10,10);
g.fillRect(Xposition1+310,170,10,10);
g.fillRect(Xposition1+320,170,10,10);
g.fillRect(Xposition1+330,170,10,10);
g.fillRect(Xposition1+200,180,10,10);
g.fillRect(Xposition1+210,180,10,10);
g.fillRect(Xposition1+220,180,10,10);
g.fillRect(Xposition1+250,180,10,10);
g.fillRect(Xposition1+260,180,10,10);
g.fillRect(Xposition1+290,180,10,10);
g.fillRect(Xposition1+300,180,10,10);
g.fillRect(Xposition1+310,180,10,10);
g.fillRect(Xposition1+210,190,10,10);
g.fillRect(Xposition1+300,190,10,10);


Xposition1++;
if (Xposition1 > 500)
{
    Xposition1 = -500;
}

Yposition--;
if (Yposition > 500)
{
    Yposition = -500;
}

g.setColor(Color.magenta);
g.fillRect(260,Yposition+240,10,10);
g.fillRect(260,Yposition+250,10,10);

g.setColor(Color.white);

g.fillRect(260,310,10,10);
g.fillRect(250,320,10,10);
g.fillRect(260,320,10,10);
g.fillRect(270,320,10,10);
g.fillRect(250,330,10,10);
g.fillRect(260,330,10,10);
g.fillRect(270,330,10,10);
g.fillRect(200,340,10,10);
g.fillRect(210,340,10,10);
g.fillRect(220,340,10,10);
g.fillRect(230,340,10,10);
g.fillRect(240,340,10,10);
g.fillRect(250,340,10,10);
g.fillRect(260,340,10,10);
g.fillRect(270,340,10,10);
g.fillRect(280,340,10,10);
g.fillRect(290,340,10,10);
g.fillRect(300,340,10,10);
g.fillRect(310,340,10,10);
g.fillRect(320,340,10,10);
g.fillRect(190,350,10,10);
g.fillRect(200,350,10,10);
g.fillRect(210,350,10,10);
g.fillRect(220,350,10,10);
g.fillRect(230,350,10,10);
g.fillRect(240,350,10,10);
g.fillRect(250,350,10,10);
g.fillRect(260,350,10,10);
g.fillRect(270,350,10,10);
g.fillRect(280,350,10,10);
g.fillRect(290,350,10,10);
g.fillRect(300,350,10,10);
g.fillRect(310,350,10,10);
g.fillRect(320,350,10,10);
g.fillRect(330,350,10,10);
g.fillRect(190,360,10,10);
g.fillRect(200,360,10,10);
g.fillRect(210,360,10,10);
 g.fillRect(220,360,10,10);
g.fillRect(230,360,10,10);
g.fillRect(240,360,10,10);
g.fillRect(250,360,10,10);
g.fillRect(260,360,10,10);
g.fillRect(270,360,10,10);
g.fillRect(280,360,10,10);
g.fillRect(290,360,10,10);
g.fillRect(300,360,10,10);
g.fillRect(310,360,10,10);
g.fillRect(320,360,10,10);
g.fillRect(330,360,10,10);
g.fillRect(190,370,10,10);
g.fillRect(200,370,10,10);
g.fillRect(210,370,10,10);
g.fillRect(220,370,10,10);
g.fillRect(230,370,10,10);
g.fillRect(240,370,10,10);
g.fillRect(250,370,10,10);
g.fillRect(260,370,10,10);
g.fillRect(270,370,10,10);
g.fillRect(280,370,10,10);
g.fillRect(290,370,10,10);
g.fillRect(300,370,10,10);
g.fillRect(310,370,10,10);
g.fillRect(320,370,10,10);
g.fillRect(330,370,10,10);
g.fillRect(190,380,10,10);
g.fillRect(200,380,10,10);
g.fillRect(210,380,10,10);
g.fillRect(220,380,10,10);
g.fillRect(230,380,10,10);
g.fillRect(240,380,10,10);
g.fillRect(250,380,10,10);
g.fillRect(260,380,10,10);
g.fillRect(270,380,10,10);
g.fillRect(280,380,10,10);
g.fillRect(290,380,10,10);
g.fillRect(300,380,10,10);
g.fillRect(310,380,10,10);
g.fillRect(320,380,10,10);
g.fillRect(330,380,10,10);




}
public static void main(String[] args)
{

 }
}
4

1 回答 1

1

首先,不要这样做。

public void run() {
    while (true) {
        setBackground(Color.black);
        repaint();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
        }
    }
}

虽然我认为这是从上下文内部运行的,但您正在从Event Dispatching ThreadRunnable外部影响 UI ,这绝不是一个好主意。

您最好使用javax.swing.Timer,请参阅如何使用 Swing 计时器了解更多信息。

其次。您的示例代码没有任何双重缓冲。

双缓冲将使您使用屏幕外缓冲(例如 a BufferedImage)将屏幕内容绘制到屏幕上,然后再将其渲染到屏幕上。

第三,如果您不想实现自己的双缓冲解决方案,我会使用 a JPanelorJComponent并将它们添加到JAppelt. JComponent(它是孩子们)默认支持双缓冲(你不应该直接绘制到顶级容器,你不想负责的引擎盖下有很多讨厌的东西)。

第四,我会花一些时间认真阅读Graphics 2D教程。

更新

这是我对基本想法的看法。注意,我没有添加播放器,你可以弄清楚。

public class SpaceFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
                new SpaceFrame();
            }
        });
    }

    public SpaceFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new SpacePane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    protected class SpacePane extends JPanel implements Screen {

        private BufferedImage buffer;

        public SpacePane() {
        }

        @Override
        public void addNotify() {
            super.addNotify();
            // We need to make that the screen pane is actually on the screen...
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Thread thread = new Thread(new UpdaterThread(SpacePane.this));
                    thread.setDaemon(true);
                    thread.start();

                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (buffer != null) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(buffer, 0, 0, this);
            }
        }

        @Override
        public void renderBuffer(final BufferedImage image) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    buffer = image;
                    repaint();
                }
            });
        }
    }

    protected interface Screen {
        public Dimension getSize();
        public void renderBuffer(final BufferedImage image);
    }

    protected class UpdaterThread implements Runnable {

        private int cycle = 0;
        private BufferedImage[] buffers = new BufferedImage[2];
        private Screen screen;
        // A row of aliens...
        private RowOfAliens rowOfAliens;
        // Adjusts the speed at which the aliens move
        private int speed = 10;

        public UpdaterThread(Screen screen) {
            this.screen = screen;
        }

        public Screen getScreen() {
            return screen;
        }

        @Override
        public void run() {
            // We're only using 1 row...
            rowOfAliens = new RowOfAliens();
            rowOfAliens.setLocation(new Point(0, 0));
            rowOfAliens.setDirection(10);

            while (true) {

                try {
                    Thread.sleep(16); // 60 fps
                } catch (InterruptedException ex) {
                }

                cycle++;

                // Update the offscreen buffer...
                BufferedImage buffer = getBuffer();
                if (buffer != null) {

                    // Move the alients...
                    updateAliens();

                    // Paint the various layers
                    int width = buffer.getWidth();
                    int height = buffer.getHeight();
                    Graphics2D g2d = buffer.createGraphics();
                    // You could paint a space scape here
                    paintBackground(g2d, width, height);
                    // The aliens and other game arifcates
                    paintMidground(g2d, width, height);
                    // Special effects
                    paintForeground(g2d, width, height);
                    g2d.dispose();

                    // Send the buffer to the screen
                    paintBuffer(buffer);

                }

            }

        }

        protected void updateAliens() {
            Screen screen = getScreen();
            if (screen != null) {

                // Controls the speed at which the aliens move
                if (cycle % speed == 0) {
                    // Get the aliens current location and direction...
                    Point p = rowOfAliens.getLocation();
                    p.x += rowOfAliens.getDirection();

                    int screenWidth = screen.getSize().width;
                    int screenHeight = screen.getSize().height;
                    // Boundray check...
                    if (p.x + rowOfAliens.getBounds().width > screenWidth) {
                        p.x = screenWidth - rowOfAliens.getBounds().width;
                        // Reverse direction
                        rowOfAliens.setDirection(-10);
                        p.y += 10;
                    } else if (p.x < 0) {
                        p.x = 0;
                        p.y += 10;
                        // Reverse direction
                        rowOfAliens.setDirection(10);
                    }
                    // Set the location of the row...
                    rowOfAliens.setLocation(p);

                }

            }

        }

        protected void paintBackground(Graphics2D g2d, int width, int height) {
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, width, height);
        }

        protected void paintMidground(Graphics2D g2d, int width, int height) {
            Point p = rowOfAliens.getLocation();
            g2d.translate(p.x, p.y);
            rowOfAliens.paint(g2d);
            g2d.translate(-p.x, -p.y);
        }

        protected void paintForeground(Graphics2D g2d, int width, int height) {
        }

        protected BufferedImage getBuffer() {
            BufferedImage buffer = null;

            Screen screen = getScreen();
            if (screen != null) {
                // Determine which buffer to use
                int index = 0;
                if (cycle % 2 != 0) {
                    index = 1;
                }

                buffer = buffers[index];
                if (buffer == null || buffer.getWidth() != screen.getSize().width || buffer.getHeight() != screen.getSize().height) {
                    // Create a compataible graphics object, so it will render faster on the screen...
                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    GraphicsDevice gs = ge.getDefaultScreenDevice();
                    GraphicsConfiguration gc = gs.getDefaultConfiguration();

                    buffer = gc.createCompatibleImage(screen.getSize().width, screen.getSize().height, Transparency.OPAQUE);
                    buffers[index] = buffer;
                }

            }

            return buffer;

        }

        protected void paintBuffer(BufferedImage buffer) {
            // Send the buffer to the screen...
            Screen screen = getScreen();
            if (screen != null) {
                screen.renderBuffer(buffer);
            }
        }
    }

    protected class RowOfAliens {

        private List<Alien> lstAliens;
        private Point location;
        private int direction;
        private Rectangle bounds;

        public RowOfAliens() {
            lstAliens = new ArrayList<Alien>(25);
            lstAliens.add(new AlienType1());
            lstAliens.add(new AlienType2());
            lstAliens.add(new AlienType3());
            lstAliens.add(new AlienType4());

            setLocation(new Point(0, 0));
            // Calculate the size of the row...
            bounds = new Rectangle(Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 0);

            int index = 0;
            for (Alien alien : lstAliens) {

                Rectangle aBounds = alien.getBounds();
                aBounds.setLocation(alien.getLocation());
                System.out.println(index + ": " + aBounds);

                bounds.x = Math.min(bounds.x, aBounds.x);
                bounds.y = Math.min(bounds.y, aBounds.y);

                bounds.width = Math.max(bounds.width, aBounds.x + aBounds.width);
                bounds.height = Math.max(bounds.height, aBounds.y + aBounds.height);

            }

            System.out.println(bounds);

        }

        protected void paint(Graphics2D g2d) {
            // Paint the aliens
            Point pos = getLocation();

            for (Alien alien : lstAliens) {
                g2d.setColor(alien.getColor());
                Point p = alien.getLocation();
                // We need to take into account the current location of the row
                // in relation to the alien...
                int xPos = p.x - pos.x;
                g2d.translate(xPos, 0);
                g2d.fill(alien);
                g2d.translate(-xPos, 0);
            }

        }

        protected void layoutRow() {
            // Layout the aliens within there row based on the currrent location...
            // Techniqually, we could offset the aliens at 0x0 and simply adjust the
            // the x position by the width of each alien and it's gap
            // This would eliminate the need to adjust the position in the
            // paint method, but hay...
            Point p = new Point(getLocation());
            for (Alien alien : lstAliens) {
                alien.setLocation(p);
                p = new Point(p.x, p.y);
                p.x += alien.getBounds().width + 10;
            }

        }

        public Rectangle getBounds() {
            return bounds;
        }

        public void setLocation(Point point) {
            if (location == null || point == null || (!location.equals(point))) {
                location = point;
                layoutRow();
            }
        }

        public Point getLocation() {
            return new Point(location);
        }

        public void setDirection(int value) {
            direction = value;
        }

        public int getDirection() {
            return direction;
        }
    }

    protected abstract class Alien extends Path2D.Float {

        private Point location;
        private Color color;

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public void setLocation(Point location) {
            this.location = location;
        }

        public Point getLocation() {
            return location;
        }

        public void addRectangle(int x, int y, int width, int height) {
            moveTo(x, y);
            lineTo(x + width, y);
            lineTo(x + width, y + height);
            lineTo(x, y + height);
            lineTo(x, y);
        }
    }

    protected class AlienType1 extends Alien {

        public AlienType1() {
            setColor(Color.ORANGE);
            addRectangle(30, 0, 10, 10);
            addRectangle(40, 0, 10, 10);
            addRectangle(20, 10, 10, 10);
            addRectangle(30, 10, 10, 10);
            addRectangle(40, 10, 10, 10);
            addRectangle(50, 10, 10, 10);
            addRectangle(10, 20, 10, 10);
            addRectangle(20, 20, 10, 10);
            addRectangle(30, 20, 10, 10);
            addRectangle(40, 20, 10, 10);
            addRectangle(50, 20, 10, 10);
            addRectangle(60, 20, 10, 10);
            addRectangle(0, 30, 10, 10);
            addRectangle(10, 30, 10, 10);
            addRectangle(30, 30, 10, 10);
            addRectangle(40, 30, 10, 10);
            addRectangle(60, 30, 10, 10);
            addRectangle(70, 30, 10, 10);
            addRectangle(0, 40, 10, 10);
            addRectangle(10, 40, 10, 10);
            addRectangle(20, 40, 10, 10);
            addRectangle(30, 40, 10, 10);
            addRectangle(40, 40, 10, 10);
            addRectangle(50, 40, 10, 10);
            addRectangle(60, 40, 10, 10);
            addRectangle(70, 40, 10, 10);
            addRectangle(20, 50, 10, 10);
            addRectangle(50, 50, 10, 10);
            addRectangle(10, 60, 10, 10);
            addRectangle(30, 60, 10, 10);
            addRectangle(40, 60, 10, 10);
            addRectangle(60, 60, 10, 10);
            addRectangle(0, 70, 10, 10);
            addRectangle(20, 70, 10, 10);
            addRectangle(50, 70, 10, 10);
            addRectangle(70, 70, 10, 10);
            closePath();
        }
    }

    protected class AlienType2 extends Alien {

        public AlienType2() {
            setColor(Color.RED);
            addRectangle(20, 0, 10, 10);
            addRectangle(80, 0, 10, 10);
            addRectangle(0, 10, 10, 10);
            addRectangle(30, 10, 10, 10);
            addRectangle(70, 10, 10, 10);
            addRectangle(100, 10, 10, 10);
            addRectangle(0, 20, 10, 10);
            addRectangle(20, 20, 10, 10);
            addRectangle(30, 20, 10, 10);
            addRectangle(40, 20, 10, 10);
            addRectangle(50, 20, 10, 10);
            addRectangle(60, 20, 10, 10);
            addRectangle(70, 20, 10, 10);
            addRectangle(80, 20, 10, 10);
            addRectangle(100, 20, 10, 10);
            addRectangle(0, 30, 10, 10);
            addRectangle(10, 30, 10, 10);
            addRectangle(20, 30, 10, 10);
            addRectangle(40, 30, 10, 10);
            addRectangle(50, 30, 10, 10);
            addRectangle(60, 30, 10, 10);
            addRectangle(80, 30, 10, 10);
            addRectangle(90, 30, 10, 10);
            addRectangle(100, 30, 10, 10);
            addRectangle(0, 40, 10, 10);
            addRectangle(10, 40, 10, 10);
            addRectangle(20, 40, 10, 10);
            addRectangle(30, 40, 10, 10);
            addRectangle(40, 40, 10, 10);
            addRectangle(50, 40, 10, 10);
            addRectangle(60, 40, 10, 10);
            addRectangle(70, 40, 10, 10);
            addRectangle(80, 40, 10, 10);
            addRectangle(90, 40, 10, 10);
            addRectangle(100, 40, 10, 10);
            addRectangle(10, 50, 10, 10);
            addRectangle(20, 50, 10, 10);
            addRectangle(30, 50, 10, 10);
            addRectangle(40, 50, 10, 10);
            addRectangle(50, 50, 10, 10);
            addRectangle(60, 50, 10, 10);
            addRectangle(70, 50, 10, 10);
            addRectangle(80, 50, 10, 10);
            addRectangle(90, 50, 10, 10);
            addRectangle(20, 60, 10, 10);
            addRectangle(80, 60, 10, 10);
            addRectangle(10, 70, 10, 10);
            addRectangle(90, 70, 10, 10);
            closePath();
        }
    }

    protected class AlienType3 extends Alien {

        public AlienType3() {
            setColor(Color.YELLOW);
            addRectangle(20, 0, 10, 10);
            addRectangle(20, 0, 10, 10);
            addRectangle(80, 0, 10, 10);
            addRectangle(30, 10, 10, 10);
            addRectangle(70, 10, 10, 10);
            addRectangle(20, 20, 10, 10);
            addRectangle(30, 20, 10, 10);
            addRectangle(40, 20, 10, 10);
            addRectangle(50, 20, 10, 10);
            addRectangle(60, 20, 10, 10);
            addRectangle(70, 20, 10, 10);
            addRectangle(80, 20, 10, 10);
            addRectangle(10, 30, 10, 10);
            addRectangle(20, 30, 10, 10);
            addRectangle(40, 30, 10, 10);
            addRectangle(50, 30, 10, 10);
            addRectangle(60, 30, 10, 10);
            addRectangle(80, 30, 10, 10);
            addRectangle(90, 30, 10, 10);
            addRectangle(0, 40, 10, 10);
            addRectangle(10, 40, 10, 10);
            addRectangle(20, 40, 10, 10);
            addRectangle(30, 40, 10, 10);
            addRectangle(40, 40, 10, 10);
            addRectangle(50, 40, 10, 10);
            addRectangle(60, 40, 10, 10);
            addRectangle(70, 40, 10, 10);
            addRectangle(80, 40, 10, 10);
            addRectangle(90, 40, 10, 10);
            addRectangle(100, 40, 10, 10);
            addRectangle(0, 50, 10, 10);
            addRectangle(20, 50, 10, 10);
            addRectangle(30, 50, 10, 10);
            addRectangle(40, 50, 10, 10);
            addRectangle(50, 50, 10, 10);
            addRectangle(60, 50, 10, 10);
            addRectangle(70, 50, 10, 10);
            addRectangle(80, 50, 10, 10);
            addRectangle(100, 50, 10, 10);
            addRectangle(0, 60, 10, 10);
            addRectangle(20, 60, 10, 10);
            addRectangle(80, 60, 10, 10);
            addRectangle(100, 60, 10, 10);
            addRectangle(30, 70, 10, 10);
            addRectangle(40, 70, 10, 10);
            addRectangle(60, 70, 10, 10);
            addRectangle(70, 70, 10, 10);
            closePath();
        }
    }

    protected class AlienType4 extends Alien {

        public AlienType4() {
            setColor(Color.GREEN);
            addRectangle(50, 0, 10, 10);
            addRectangle(60, 0, 10, 10);
            addRectangle(70, 0, 10, 10);
            addRectangle(80, 0, 10, 10);
            addRectangle(90, 0, 10, 10);
            addRectangle(100, 0, 10, 10);
            addRectangle(30, 10, 10, 10);
            addRectangle(40, 10, 10, 10);
            addRectangle(50, 10, 10, 10);
            addRectangle(60, 10, 10, 10);
            addRectangle(70, 10, 10, 10);
            addRectangle(80, 10, 10, 10);
            addRectangle(90, 10, 10, 10);
            addRectangle(100, 10, 10, 10);
            addRectangle(110, 10, 10, 10);
            addRectangle(120, 10, 10, 10);
            addRectangle(20, 20, 10, 10);
            addRectangle(30, 20, 10, 10);
            addRectangle(40, 20, 10, 10);
            addRectangle(50, 20, 10, 10);
            addRectangle(60, 20, 10, 10);
            addRectangle(70, 20, 10, 10);
            addRectangle(80, 20, 10, 10);
            addRectangle(90, 20, 10, 10);
            addRectangle(100, 20, 10, 10);
            addRectangle(110, 20, 10, 10);
            addRectangle(120, 20, 10, 10);
            addRectangle(130, 20, 10, 10);
            addRectangle(10, 30, 10, 10);
            addRectangle(20, 30, 10, 10);
            addRectangle(40, 30, 10, 10);
            addRectangle(50, 30, 10, 10);
            addRectangle(70, 30, 10, 10);
            addRectangle(80, 30, 10, 10);
            addRectangle(100, 30, 10, 10);
            addRectangle(110, 30, 10, 10);
            addRectangle(130, 30, 10, 10);
            addRectangle(140, 30, 10, 10);
            addRectangle(0, 40, 10, 10);
            addRectangle(10, 40, 10, 10);
            addRectangle(20, 40, 10, 10);
            addRectangle(30, 40, 10, 10);
            addRectangle(40, 40, 10, 10);
            addRectangle(50, 40, 10, 10);
            addRectangle(60, 40, 10, 10);
            addRectangle(70, 40, 10, 10);
            addRectangle(80, 40, 10, 10);
            addRectangle(90, 40, 10, 10);
            addRectangle(100, 40, 10, 10);
            addRectangle(110, 40, 10, 10);
            addRectangle(120, 40, 10, 10);
            addRectangle(130, 40, 10, 10);
            addRectangle(140, 40, 10, 10);
            addRectangle(150, 40, 10, 10);
            addRectangle(20, 50, 10, 10);
            addRectangle(30, 50, 10, 10);
            addRectangle(40, 50, 10, 10);
            addRectangle(70, 50, 10, 10);
            addRectangle(80, 50, 10, 10);
            addRectangle(110, 50, 10, 10);
            addRectangle(120, 50, 10, 10);
            addRectangle(130, 50, 10, 10);
            addRectangle(30, 60, 10, 10);
            addRectangle(120, 60, 10, 10);
            closePath();
        }
    }
}
于 2012-10-06T03:42:19.443 回答