重绘图形问题 [英] Repainting Graphics Issue

查看:102
本文介绍了重绘图形问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试着写一个小型的侧面卷轴游戏,但遇到了重绘()而不重绘的问题。关键听众本身起作用,当方向键被按下时,它会递增和递减x和y值。

  public class SideScroller扩展了JPanel实现KeyListener {
private随机随机=新随机();
private int r,g,b;
private int x = 0,y = 0;
$ b $ public void keyPressed(KeyEvent e){
if(e.getKeyCode()== e.VK_UP){
y - = 5;
} else if(e.getKeyCode()== e.VK_DOWN){
y + = 5;
} else if(e.getKeyCode()== e.VK_LEFT){
x - = 5;
} else if(e.getKeyCode()== e.VK_RIGHT){
x + = 5;
}
repaint();
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
$ b $ public void paint(Graphics gg){
gg.setColor(new Color(r,g,b));
gg.fillRect(x,y,50,50);


public static void main(String [] args){
SideScroller ss = new SideScroller();
JFrame f = new JFrame();

f.add(new SideScroller());
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.addKeyListener(new SideScroller());


解决方案


  • 您应该重载 paintComponent 并调用 super.paintComponent ,而不是 paint

      protected void paintComponent(Graphics gg){
    super.paintComponent(gg);
    gg.setColor(new Color(r,g,b));
    gg.fillRect(x,y,50,50);
    }


  • 另外我建议您使用键绑定而不是 KeyListener

  • 你也可以在Event Dispacth Thread上运行Swing应用程序你可以通过将代码包装在 main

  • code> SwingUtilities.invokeLater(...)




下面是上面提到的所有修复程序:

  import java.awt.Color; 
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
导入javax.swing.SwingUtilities;

公共类SideScroller扩展JPanel {

private随机random = new Random();
private int r,g,b;
private int x = 0,y = 0;

public SideScroller(){
InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(UP),upaction);
am.put(upaction,new AbstractAction(){
public void actionPerformed(ActionEvent e){
y - = 5;

repaint();
}
});
im.put(KeyStroke.getKeyStroke(DOWN),downact​​ion);
am.put(downact​​ion,new AbstractAction(){
public void actionPerformed(ActionEvent e){
y + = 5;
repaint();
}
});
im.put(KeyStroke.getKeyStroke(LEFT),leftaction);
am.put(leftaction,new AbstractAction(){
public void actionPerformed(ActionEvent e){
x - = 5;
repaint();
}
});
im.put(KeyStroke.getKeyStroke(RIGHT),rightaction);
am.put(rightaction,new AbstractAction(){
public void actionPerformed(ActionEvent e){
x + = 5;
repaint();
}
});
}

保护void paintComponent(Graphics gg){
super.paintComponent(gg);

gg.setColor(new Color(r,g,b));
gg.fillRect(x,y,50,50);


public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
)JFrame f = new JFrame();

f.add(new SideScroller());
f.setSize(500,500);
f.setLocationRelativeTo(null) ;
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}


I've been trying to write a small side-scroller game, but ran into an issue with repaint() not repainting. The key listener itself works, it increments and decrements the x and y values when the directional keys are pressed. Just the repaint() doesn't seem to be repainting.

public class SideScroller extends JPanel implements KeyListener{
private Random random = new Random();
private int r,g,b;
private int x = 0, y = 0;

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == e.VK_UP) {
        y -= 5;
    } else if(e.getKeyCode() == e.VK_DOWN) {
        y += 5;
    } else if(e.getKeyCode() == e.VK_LEFT) {
        x -= 5;
    } else if(e.getKeyCode() == e.VK_RIGHT) {
        x += 5;
    }
    repaint();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

public void paint(Graphics gg) {
    gg.setColor(new Color(r, g, b));
    gg.fillRect (x, y, 50, 50);
}

public static void main(String[] args) {
    SideScroller ss = new SideScroller();
    JFrame f = new JFrame();

    f.add(new SideScroller());
    f.setSize(500, 500);
    f.setLocationRelativeTo(null);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.addKeyListener(new SideScroller());
}

解决方案

  • You should be overriding paintComponent and calling super.paintComponent, not paint.

    protected void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        gg.setColor(new Color(r, g, b));
        gg.fillRect (x, y, 50, 50);
    }
    

  • Also I'd recommend looking into using Key Bindings instead of KeyListener,

  • Also you should be running Swing apps on the Event Dispacth Thread You can d so by wrapping the code in the main in a SwingUtilities.invokeLater(...)

Here are all the fixes mentioned above

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class SideScroller extends JPanel {

    private Random random = new Random();
    private int r, g, b;
    private int x = 0, y = 0;

    public SideScroller() {
        InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap am = getActionMap();
        im.put(KeyStroke.getKeyStroke("UP"), "upaction");
        am.put("upaction", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                y -= 5;

                repaint();
            }
        });
        im.put(KeyStroke.getKeyStroke("DOWN"), "downaction");
        am.put("downaction", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                y += 5;
                repaint();
            }
        });
        im.put(KeyStroke.getKeyStroke("LEFT"), "leftaction");
        am.put("leftaction", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                x -= 5;
                repaint();
            }
        });
        im.put(KeyStroke.getKeyStroke("RIGHT"), "rightaction");
        am.put("rightaction", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                x += 5;
                repaint();
            }
        });
    }

    protected void paintComponent(Graphics gg) {
        super.paintComponent(gg);

        gg.setColor(new Color(r, g, b));
        gg.fillRect(x, y, 50, 50);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame();

                f.add(new SideScroller());
                f.setSize(500, 500);
                f.setLocationRelativeTo(null);
                f.setResizable(false);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}

这篇关于重绘图形问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆