簡體   English   中英

如何不斷顯示形狀,但仍重新粉刷屏幕?

[英]how to constantly display shapes, but still repaint the screen?

在用JFrame編寫一個簡單的Tron游戲時,我遇到了一個問題。 我不確定如何將“光周期”的顯示保留為一行,一直停留在屏幕上顯示的狀態,但是同時重新粉刷屏幕以使運動仍然有效。 這是我的代碼:(僅供參考,它們都可以編譯,並且一切正常,因為這里寫的很好)

public class Tron extends JPanel{

    public static int x = 40;
    public static int y = 40;
    public static int h = 360;
    public static int k = 360;
    public final static int size = 10;
    public static int move = 1;
    public static int dir = 0;
    static final Tron m = new Tron();
    static final JFrame frame = new JFrame("1P Tron");

    public static void main(String[] args){

        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        m.setBackground(Color.black);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                dir = 1;
                x += 5;
                if(x > 390){
                    x = -5;
                };
                m.repaint();
            }
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                dir = 2;
                x -= 5;
                if(x < 0){
                    x = 395;
                };
                m.repaint();
            }
        };

        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                dir = 3;
                y -= 5;
                if(y < 0){
                    y = 375;
                };
                m.repaint();
            }
        };

        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                dir = 4;
                y += 5;
                if(y > 370){
                    y = 0;
                };
                m.repaint();
            }
        };


        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");

        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);
        m.getActionMap().put("UP", actionUp);
        m.getActionMap().put("DOWN", actionDown);

    }

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

    public void draw(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(x, y, size, size);
        g.drawString("Tron", 190, 390);
    }

}

另外,我想知道如何制作它,這樣當我按下箭頭鍵時,而不是僅從x或y坐標中進行一次加法或減法,而是不斷地對其進行添加,直到按下另一個箭頭。

編輯:為了使這個問題更容易理解,python等效項將在curses庫的x和y坐標處繪制一個字符,使用箭頭鍵在屏幕上移動它,但不要調用stdscr.clear()

編輯:在Internet上進行搜索,試圖找出如何按照MadProgrammer的建議使用g.drawPolygon() ,我遇到了一個有趣的想法,即添加一個變量來幫助程序“記住”上次按下的鍵並將其添加到基於此的坐標。 我認為這將消除對多邊形的需求,而我可以只使用Translate()方法,但是當我添加翻譯時,程序停止工作。 是否需要使用其他方法代替,還是做錯了其他事情? 這是新的代碼:

import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class Tron extends JPanel{

    public static int x = 40;
    public static int y = 40;
    public static int h = 360;
    public static int k = 360;
    public static int size = 10;
    public static int move = 1;
    public static int dir = 1;
    static final Tron m = new Tron();
    static final JFrame frame = new JFrame("1P Tron");

    public static void main(String[] args){

        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(m);
        m.setBackground(Color.black);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                dir = 1;
            };
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                dir = 2;
            };
        };

        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                dir = 3;
            };
        };

        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                dir = 4;          
            };
        };


        KeyStroke right = KeyStroke.getKeyStroke("RIGHT");
        KeyStroke left = KeyStroke.getKeyStroke("LEFT");
        KeyStroke up = KeyStroke.getKeyStroke("UP");
        KeyStroke down = KeyStroke.getKeyStroke("DOWN");

        InputMap inputMap = m.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(right, "RIGHT");
        inputMap.put(left, "LEFT");
        inputMap.put(up, "UP");
        inputMap.put(down, "DOWN");
        m.getActionMap().put("RIGHT", actionRight);
        m.getActionMap().put("LEFT", actionLeft);
        m.getActionMap().put("UP", actionUp);
        m.getActionMap().put("DOWN", actionDown);

    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        draw(g);
        p1_move(m);
    }

    public void p1_move(Tron m){
        if(dir == 1){
            if(x > 390){
                x = -5;
            };
            x += 5;
            m.repaint();
        }else if(dir == 2){
            if(x < 0){
                x = 395;
            };
            x -= 5;
            m.repaint();
        }else if(dir == 3){
            if(y < 0){
                y = 375;
            };
            y -= 5;
            m.repaint();
        }else if(dir == 4){
            if(y > 370){
                y = 0;
            };
            y += 5;
            m.repaint();
        }
    }

    public void draw(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(40, 40, size, size);
        g.translate(x, y);
    }
}

順便說一句,如果有人想知道為什么所有java.Util.*都是單獨導入的,我將使用最初的那些來創建ArrayList ,但是決定反對它,並且從來沒有繞過這些。

編輯:一個列表編輯此問題,我的程序幾乎完成。 我想知道的最后一件事是如何為CPU播放器放置一個隨機數生成器。 最初它有一條固定的路徑,如果您存活足夠長的時間,則在幾秒鍾后“殺死”自己,但我認為如果他只是走一條隨機路徑,那會更好玩。 我已經知道如何使用java.util.Random編寫隨機數生成器,但是這些代碼的最終結果總是比我在游戲中所想的要長得多,所以我想知道是否可以添加僅用五六個語句就可以了。

基本思想是需要將所有形狀放入某種List以便在調用paintComponent方法時,調用wall列表並繪制形狀

我的“一般”想法是每次玩家轉彎時將一個新的Point添加到List paintComponent方法中,您只需繪制從一點到另一點的線。

您繪畫過程中的最后一項動作,我就是簡單地從最后一點到玩家當前位置划一條線...

使用Path2D更新示例

這是使用Path2D維護循環路徑的概念的非常基本的示例。 這樣,我認為您將更容易使用PointList ,因為這將更易於構建碰撞檢測。

在此處輸入圖片說明

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LightCycles {

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

    public LightCycles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private int xVelocity;
        private int yVelocity;

        protected static final int PLAYER_SIZE = 4;
        protected static final int DELTA = 4;

        private Point player;
        private Point lastTurn;
        private Path2D playerPath;

        public TestPane() {
            setBackground(Color.BLACK);
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");

            am.put("left", new VelocityAction(-DELTA, 0));
            am.put("right", new VelocityAction(DELTA, 0));
            am.put("up", new VelocityAction(0, -DELTA));
            am.put("down", new VelocityAction(0, DELTA));

            xVelocity = DELTA;

            player = new Point(0, 100);
            lastTurn = new Point(player);
            playerPath = new Path2D.Float();
            playerPath.moveTo(0, 100); // Start position...

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    player.x += xVelocity;
                    if (player.x > getWidth()) {
                        playerPath.lineTo(getWidth(), player.y);
                        playerPath.moveTo(0, player.y);
                        player.x = 0;
                        lastTurn = new Point(player);
                    }
                    if (player.x + PLAYER_SIZE < 0) {
                        playerPath.lineTo(0, player.y);
                        playerPath.moveTo(getWidth() - 1, player.y);
                        player.x = getWidth() - 1;
                        lastTurn = new Point(player);
                    }
                    player.y += yVelocity;
                    if (player.y > getHeight()) {
                        playerPath.lineTo(player.x, getHeight());
                        playerPath.moveTo(player.x, 0);
                        player.y = 0;
                        lastTurn = new Point(player);
                    }
                    if (player.y + PLAYER_SIZE < 0) {
                        playerPath.lineTo(player.x, 0);
                        playerPath.moveTo(player.x, getHeight() - 1);
                        player.y = getHeight() - 1;
                        lastTurn = new Point(player);
                    }
                    repaint();
                }
            });
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.BLUE);
            g2d.draw(playerPath);
            g2d.draw(new Line2D.Float(lastTurn, player));
            g2d.drawRect(player.x - (PLAYER_SIZE / 2), player.y - (PLAYER_SIZE / 2), PLAYER_SIZE, PLAYER_SIZE);
            g2d.dispose();
        }

        public class VelocityAction extends AbstractAction {

            private final int xDelta;
            private final int yDelta;

            public VelocityAction(int xDelta, int yDelta) {
                this.xDelta = xDelta;
                this.yDelta = yDelta;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                xVelocity = xDelta;
                yVelocity = yDelta;
                lastTurn = new Point(player);
                playerPath.lineTo(player.x, player.y);
            }

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM