1

こんにちは、私はこの問題を解決する方法に非常に興味があります: Java で fillArc、drawArc メソッドを使用して pacman を作成しました。現在、画面に pacman の男がいて、どの方向に進んでも常に右を向いています.. 私の質問オブジェクトを角度ごとに変更したり、Java で水平方向に反転したりする方法はありますか?

AffineTransform を使用しようとしましたが、ドキュメントで必要な場所が得られません... switch ステートメントを使用してこれを達成するにはどうすればよいですか? 次のことをしようとしましたが、続行方法がわからないため、この部分で行き詰まります。

DrawPacMan pacman = new DrawPacMan();
DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();

AffineTransform pac = new AffineTransform();

public void setPacManView(int waarde) {
    // set the view of pacman
    switch (waarde) {
    case 0 :
        // here one view of pacman
        break;
    case 1 :
        // here one view of pacman
        break;
    case 2 :
        // here one view of pacman
        break;
    case 3 :
        // here one view of pacman
        break;

    }
}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    // pacman movement
    diameter = 75;  
    pacman.drawPacMan(g, getHorPlaats(), getVerPlaats(), diameter, Color.yellow);

    // ghosts movement
    int g1x;
    for(g1x = 0; g1x < 10; g1x++) {

        pacman.drawGhost(g, g1x, 40, diameter, Color.red);

    }
    pacman.drawGhost(g, 170, 70, diameter, Color.blue);


}
4

3 に答える 3

6

次のようなものを試してください...

ここに画像の説明を入力してください

デモは、仮想の天使(角度<0&> 360)を介して画像を回転できるように設計されていますが、基本的な概念は同じです...

public class TestFlipImage {

    protected static final String IMAGE_PATH = "/path/to/your/image";

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

    public TestFlipImage() {
        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) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                BufferedImage image = null;
                try {
                    image = ImageIO.read(new File(IMAGE_PATH));
                } catch (IOException ex) {
                }

                JPanel mainPane = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                mainPane.add(new ImagePane(image, 0));
                mainPane.add(new ImagePane(image, 90));
                mainPane.add(new ImagePane(image, 180));

                frame.add(mainPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage masterImage;
        private BufferedImage renderedImage;

        public ImagePane(BufferedImage image, int angle) {
            masterImage = image;
            applyRotation(angle);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(renderedImage.getWidth(), renderedImage.getHeight());
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        protected int getVirtualAngle(int angle) {
            float fRotations = (float) angle / 360f;
            int rotations = (int) (fRotations - (fRotations / 1000));

            int virtual = angle - (rotations * 360);

            if (virtual < 0) {
                virtual = 360 + virtual;
            }

            return virtual;
        }

        // The code is designed to rotate an image through 90 degree
        // angles, but it can handle angle's less then 0 and greater then
        // 360 degrees
        public void applyRotation(int angle) {
            // This will only work for angles of 90 degrees...

            // Normalize the angle to make sure it's only between 0-360 degrees
            int virtualAngle = getVirtualAngle(angle);
            Dimension size = new Dimension(masterImage.getWidth(), masterImage.getHeight());
            int masterWidth = masterImage.getWidth();
            int masterHeight = masterImage.getHeight();

            double x = 0; //masterWidth / 2.0;
            double y = 0; //masterHeight / 2.0;

            switch (virtualAngle) {
                case 0:
                    break;
                case 180:
                    break;
                case 90:
                case 270:
                    size = new Dimension(masterImage.getHeight(), masterImage.getWidth());
                    x = (masterHeight - masterWidth) / 2.0;
                    y = (masterWidth - masterHeight) / 2.0;
                    break;
            }
            renderedImage = new BufferedImage(size.width, size.height, masterImage.getTransparency());
            Graphics2D g2d = renderedImage.createGraphics();

            AffineTransform at = AffineTransform.getTranslateInstance(x, y);

            at.rotate(Math.toRadians(virtualAngle), masterWidth / 2.0, masterHeight / 2.0);
            g2d.drawImage(masterImage, at, null);

            g2d.dispose();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - renderedImage.getWidth()) / 2;
            int y = (height - renderedImage.getHeight()) / 2;

            g2d.drawImage(renderedImage, x, y, this);
        }
    }
}

追加

また、AffineTransform.rotate()も確認することをお勧めします-xlate、rotate、scaleを同時に行うにはどうすればよいですか?wchは、水平軸と垂直軸で画像を反転する方法について説明しています

于 2012-10-10T19:28:18.670 に答える
1

画像が4枚しかない場合。paint/gimp/photoshop で 4 つの jpg を作成し、プログラムにロードすることをお勧めします。

BufferedImage pacUp = ImageIO.read(new File("./images/pac_up.jpg"));
BufferedImage pacDown = ImageIO.read(new File("./images/pac_down.jpg"));
//etc

そして、描画スイッチは次のようになります (Graphicsオブジェクトを想定g);

switch(direction) {
    case 0: g.drawImage(pacUp, x, y, null);
    case 1: g.drawImage(pacDown, x, y, null);
    case 2: g.drawImage(pacLeft, x, y, null);
    case 3: g.drawImage(pacRight, x, y, null);
}
于 2012-10-10T17:56:47.750 に答える
1

Swing/AWT ですか?はいの場合、AffineTransformが探しているものかもしれません。

于 2012-10-10T17:15:08.443 に答える