我有一个关于paint(graphics g)方法的问题,在拨片向左或向右移动时

gpfsuwkq  于 2021-07-06  发布在  Java
关注(0)|答案(6)|浏览(267)

我有一个左右移动的问题,这是代码。重绘方法也不能正常工作。我已经修改了很多次代码,但没有白费。
我现在不想使用paintcomponent。我很想知道代码中的确切问题是什么。谢谢你的帮助。

package com.zharawa;

public class Main {
    public static void main(String[] args) {
         new GameFrame("Brick Breaker", 800, 700);
    }
}
package com.zharawa;

import java.awt.Color;

import javax.swing.JFrame;

public class GameFrame extends JFrame{
    private String title;
    private int width, height;
    GamePanel gamePanel;

    public GameFrame(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;

        gamePanel = new GamePanel();
        this.setSize(width, height);
        this.setBackground(Color.black);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.add(gamePanel);
    }
}
package com.zharawa;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements KeyListener, ActionListener{
    private int paddleXdir=350;
    private int paddleYdir=600;
    private int paddleWidth=150;
    private int paddleHeight=10;

    public GamePanel() {
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        repaint();
    }

    public void paint(Graphics g) {     
        //draw borders
        g.setColor(Color.blue);
        g.fillRect(0, 0, 785, 5); 
        g.fillRect(0, 5, 5, 655);
        g.fillRect(780, 5, 5, 655);
        g.setColor(Color.red);
        g.fillRect(5, 655, 775, 5);

        //draw ball
        g.setColor(Color.green);
        g.fillOval(300, 450, 20, 20);

        //draw bricks
        // g.setColor(Color.blue);
        // for(int i=0;i<20;i++) {
        //   g.fillRect(50, 50, 30, 30);

        //}

        //draw paddle
        g.setColor(Color.green);
        g.fillRect(paddleXdir, paddleYdir, paddleWidth, paddleHeight);

        g.dispose();
     }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
            paddleXdir+=25;
        }

        if(e.getKeyCode()==KeyEvent.VK_LEFT)
            paddleXdir-=25;
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
mctunoxg

mctunoxg2#

我想你不明白卡米克尔的评论。您需要更改方法的名称 paintpaintComponent . 您还需要调用超类方法。此外,还需要添加对方法的调用 repaint() 在你的 keyPressed() 方法。
我对你的电脑做了必要的改动 GamePanel 班级。我还添加了一个 main() 方法使下面的代码成为可以运行的应用程序。您可以安全地移除 main() 方法。

package com.zharawa;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class GamePanel extends JPanel implements KeyListener, ActionListener{
    private int paddleXdir=350;
    private int paddleYdir=600;
    private int paddleWidth=150;
    private int paddleHeight=10;

    public GamePanel() {
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        repaint();
    }

    public void paintComponent(Graphics g) { // CHANGE METHOD NAME (from 'paint')
        super.paintComponent(g); // ADDED THIS LINE
        //draw borders
        g.setColor(Color.blue);
        g.fillRect(0, 0, 785, 5); 
        g.fillRect(0, 5, 5, 655);
        g.fillRect(780, 5, 5, 655);
        g.setColor(Color.red);
        g.fillRect(5, 655, 775, 5);

        //draw ball
        g.setColor(Color.green);
        g.fillOval(300, 450, 20, 20);

        //draw bricks
        // g.setColor(Color.blue);
        // for(int i=0;i<20;i++) {
        //   g.fillRect(50, 50, 30, 30);

        //}

        //draw paddle
        g.setColor(Color.green);
        g.fillRect(paddleXdir, paddleYdir, paddleWidth, paddleHeight);

        g.dispose();
     }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
            paddleXdir+=25;
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT) {
            paddleXdir-=25;
        }
        repaint(); // ADDED THIS LINE
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(800, 700);
            frame.add(new GamePanel());
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }
}

这个 actionPerformed() 方法时,将需要添加动画,即使“球”在屏幕上移动。为了添加动画,你需要使用一个摆动计时器

z31licg0

z31licg03#

我改变了绘画(图形g)的方法,以绘画或其他名称,但所有的图形都不见了。问题是,当它试图移动它的方向时,它有一个奇怪的行为。

efzxgjgh

efzxgjgh4#

主要的问题是您的keylister没有连接到主面板。没有录制你的键盘事件。这个答案只对桨的静止做出React: GameFrame

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class GameFrame extends JFrame {
    private String title;
    private int width, height;
    GamePanel gamePanel;

    public GameFrame(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;

        gamePanel = new GamePanel();
        this.setSize(width, height);
        this.setBackground(Color.black);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.add(gamePanel);
        this.addKeyListener( new CustomActionListener() );
    }

    class CustomActionListener implements KeyListener  {

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
                gamePanel.moveRight();
            }

            if(e.getKeyCode()==KeyEvent.VK_LEFT)
                gamePanel.moveLeft();

            // Required after any change:
            repaint();
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }
    }
}
``` `GamePanel` 班

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class GamePanel extends JPanel {
private int paddleXdir=350;
private int paddleYdir=600;
private int paddleWidth=150;
private int paddleHeight=10;

protected void moveRight() {
    paddleXdir+=25;
}

protected void moveLeft() {
    paddleXdir-=25;
}

public GamePanel() {
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    repaint();
}

public void paint(Graphics g) {
    //draw borders
    g.setColor(Color.blue);
    g.fillRect(0, 0, 785, 5);
    g.fillRect(0, 5, 5, 655);
    g.fillRect(780, 5, 5, 655);
    g.setColor(Color.red);
    g.fillRect(5, 655, 775, 5);

    //draw ball
    g.setColor(Color.green);
    g.fillOval(300, 450, 20, 20);

    //draw bricks
    // g.setColor(Color.blue);
    // for(int i=0;i<20;i++) {
    //   g.fillRect(50, 50, 30, 30);

    //}

    //draw paddle
    g.setColor(Color.green);
    g.fillRect(paddleXdir, paddleYdir, paddleWidth, paddleHeight);
}

}

kb5ga3dv

kb5ga3dv5#

1-我用过 super.paint(g); 更新桨的问题解决了,但是为什么我要用 this.addKeyListener(gamePanel); 在gameframe类中?如果我不使用 this.addKeyListener(gamePanel); 有时桨动有时不动。我是说为什么要用这行代码 this.addKeyListener(gamePanel); 在gameframe类中,当我有这个的时候 addKeyListener(this); gameframe类中的行?
2-它们之间有什么区别 public void paint(Graphics g) super.paint(g); 以及 public void paintComponent(Graphics g) super.paintComponent(g); ? 因为对于我的这个例子,我得到了相同的结果。
为什么游戏的背景不是黑色的虽然我有这条线 this.setBackground(Color.black); 在游戏框架类中?
4-为什么只有当我移除 super.paint(g); 游戏的背景颜色是黑色的,然后桨没有得到适当的更新?
package com.zharawa; public class Main {

public static void main(String[] args) {
new GameFrame("SnakeGame", 800, 700);

} } package com.zharawa;

import java.awt.Color;

import javax.swing.JFrame;

public class GameFrame extends JFrame{

private String title;
private int width, height;
GamePanel gamePanel;

public GameFrame(String title, int width, int height) {
    this.title = title;
    this.width = width;
    this.height = height;

    gamePanel = new GamePanel();
    this.setSize(width, height);
    this.setTitle("Brick Breaker");
    this.setBackground(Color.black);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.add(gamePanel);
    this.addKeyListener(gamePanel);

}

} `package com.zharawa; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel implements KeyListener, ActionListener{ private int paddleXdir=350; private int paddleYdir=600; private int paddleWidth=150; private int paddleHeight=10; public GamePanel() { addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paint(Graphics g) {
super.paint(g);

    //draw borders
    g.setColor(Color.blue);
     g.fillRect(0, 0, 785, 5); 
     g.fillRect(0, 5, 5, 655);
     g.fillRect(780, 5, 5, 655);
     g.setColor(Color.red);
     g.fillRect(5, 655, 775, 5);

     //draw ball
     g.setColor(Color.green);
     g.fillOval(300, 450, 20, 20);

     //draw bricks
    // g.setColor(Color.blue);
    // for(int i=0;i<20;i++) {
    //   g.fillRect(50, 50, 30, 30);

    //}

     //draw paddle
     g.setColor(Color.green);
     g.fillRect(paddleXdir, paddleYdir, paddleWidth, paddleHeight);

 }

@Override
public void actionPerformed(ActionEvent e) {

}
@Override
public void keyTyped(KeyEvent e) {

}
@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_RIGHT) 
    paddleXdir+=25;

    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        paddleXdir-=25;

    repaint();

}
@Override
public void keyReleased(KeyEvent e) {

}
}` 
dy1byipe

dy1byipe6#


1-我用过 super.paint(g); 更新桨的问题解决了,但是为什么我要用 this.addKeyListener(gamePanel); 在gameframe类中?如果我不使用 this.addKeyListener(gamePanel); 有时桨动有时不动。我是说为什么要用这行代码 this.addKeyListener(gamePanel); 在gameframe类中,当我有这个的时候 addKeyListener(this); gameframe类中的行?
2-它们之间有什么区别 public void paint(Graphics g) super.paint(g); 以及 public void paintComponent(Graphics g) super.paintComponent(g); ? 因为对于我的这个例子,我得到了相同的结果。
为什么游戏的背景不是黑色的虽然我有这条线 this.setBackground(Color.black); 在游戏框架类中?
4-为什么只有当我移除 super.paint(g); 游戏的背景颜色是黑色的,然后桨没有得到适当的更新?
package com.zharawa; public class Main {

public static void main(String[] args) {
new GameFrame("SnakeGame", 800, 700);

} } package com.zharawa;

import java.awt.Color;

import javax.swing.JFrame;

public class GameFrame extends JFrame{

private String title;
private int width, height;
GamePanel gamePanel;

public GameFrame(String title, int width, int height) {
    this.title = title;
    this.width = width;
    this.height = height;

    gamePanel = new GamePanel();
    this.setSize(width, height);
    this.setTitle("Brick Breaker");
    this.setBackground(Color.black);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.add(gamePanel);
    this.addKeyListener(gamePanel);

}

} `package com.zharawa; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel implements KeyListener, ActionListener{ private int paddleXdir=350; private int paddleYdir=600; private int paddleWidth=150; private int paddleHeight=10; public GamePanel() { addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paint(Graphics g) {
super.paint(g);

    //draw borders
    g.setColor(Color.blue);
     g.fillRect(0, 0, 785, 5); 
     g.fillRect(0, 5, 5, 655);
     g.fillRect(780, 5, 5, 655);
     g.setColor(Color.red);
     g.fillRect(5, 655, 775, 5);

     //draw ball
     g.setColor(Color.green);
     g.fillOval(300, 450, 20, 20);

     //draw bricks
    // g.setColor(Color.blue);
    // for(int i=0;i<20;i++) {
    //   g.fillRect(50, 50, 30, 30);

    //}

     //draw paddle
     g.setColor(Color.green);
     g.fillRect(paddleXdir, paddleYdir, paddleWidth, paddleHeight);

 }

@Override
public void actionPerformed(ActionEvent e) {

}
@Override
public void keyTyped(KeyEvent e) {

}
@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_RIGHT) 
    paddleXdir+=25;

    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        paddleXdir-=25;

    repaint();

}
@Override
public void keyReleased(KeyEvent e) {

}
}` 

相关问题