当前位置: 首页 > 知识库问答 >
问题:

如何不断显示形状,但仍重新绘制屏幕?

岳英锐
2023-03-14

在用JFrame编写一个简单的Tron游戏时,我遇到了一点问题。我不确定我如何能保持显示为“光循环”作为一条线停留在屏幕上显示,但同时重新油漆屏幕,使移动仍然有效。以下是我的代码:(仅供参考,它全部编译,所有工作都像它在这里写的一样完美)

公共类Tron扩展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键中添加或减去一次,而是不断地添加到x或y键,直到按下另一个箭头。

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编写随机数生成器,但是这些生成器的代码最终总是比我在游戏中想象的要长得多,所以我想知道是否可以在五六条语句中添加它。

共有1个答案

龚伯寅
2023-03-14

基本思想是,您需要将所有形状放入某种列表中,以便在调用PaintComponent方法时,调用wall列表并绘制形状

我的“一般”想法是在玩家每次回合时将一个新的添加到列表中。在PaintComponent方法中,只需绘制从一点到下一点的线条。

在你的绘画过程中的最后一个动作之一,我会简单地从最后一点到球员的当前位置画一条线...

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);
            }

        }
    }
}
 类似资料:
  • 我想重新绘制我的屏幕。到目前为止,它所做的只是在第一个屏幕上的头部应该在的地方显示一个点。这很好,但是我在代码中写了我想每秒将头部向下移动10个像素。我正在打印头部应该在的位置,在命令提示符中它显示y值确实在增加。但是在我的屏幕上,头部没有移动。 我尝试过使用revalidate方法,尝试扩展canvas类而不是jframe,我尝试过只为paint方法使用不同的类,我尝试过用paintCompon

  • 通过 Entity 添加形状 先来看一个添加立方体的例子 var viewer = new Cesium.Viewer('cesiumContainer'); var redBox = viewer.entities.add({ name : 'Red box with black outline', position: Cesium.Cartesian3.fromDegrees(-107

  • 问题内容: 以前没有做过,所以显然我很烂。在此处,当前鼠标位置周围的64个像素会在窗体上绘制得更大一些。问题是,它有点“慢”,而且我不知道从哪里开始修复。 除此之外,我还创建了一个线程,该线程在完成时会不断调用更新图形,并像文本一样以每秒fps的速度调用,以真正显示绘制速度。 替代文字 代码示例: 所做的更改: 添加了“ gc.dispose();” 添加了“ isdone”,因此不能更快地调用重

  • 最初我用的是: 但它只是给了我一个错误: 显示类中的代码:

  • 问题内容: 我在程序开始时,根据数据库中的某些内容,以编程方式在JScrollPane中添加了许多组件(JPanels,JLabels等)。 似乎对于GUI(?)而言,此过程太快了,因此JScrollPane并不总是正确更新,即,即使内部JPanel大于可见区域,滚动条也不可见。 调整窗口大小(JFrame)可以解决此问题,因为我认为Java在调整组件大小时会重新打印它们。 作为测试,我添加了一个

  • 我在CSV文件中有一些数据,想打印出来,但图形没有显示出来 这是CSV文件http://www.mediafire.com/file/2gtbxm5adom7m4j/pop.csv