【问题标题】:How to draw graphics from different classes on jpanel如何在jpanel上绘制不同类的图形
【发布时间】:2018-05-23 02:35:05
【问题描述】:

所以我正在尝试制作一个乒乓球游戏,并且我正在尝试在桨类中绘制一个矩形,但我不知道我做错了什么。有人可以帮我解决这个问题吗?我在这里附上了我程序的整个部分,这样你就更容易帮助我了。我也在复制这个,所以我可以通过它。

主类:

package eoypongv4;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JFrame;

/**
 *
 * 
 */
public class EOYPongV4 extends JPanel {

    int x = 0;
    int y = 0;
    boolean isballdown = true;
    boolean isballright = true;
    HumanPaddle player1;

    private void ballMovement() {

        if (isballright == true) {
            x++;
        }
        if (isballright == false) {
            x--;
        }

        if (isballdown == true) {
            y++;
        }

        if (isballdown == false) {
            y--;
        }

        if (y == getHeight() - 20) {

            isballdown = false;

        }
        if (y == 0) {

            isballdown = true;

        }
        if (x == getWidth() - 20) {

            isballright = false;
        }

        if (x == 0) {

            isballright = true;
        }

    }

    public void paint(Graphics g) {
        super.paint(g);

        g.fillOval(x, y, 20, 20);

    }

    public static void main(String[] args) {

        JFrame Frame = new JFrame("EoyPongV4");

        Frame.setVisible(true);

        Frame.setSize(1068, 720);

        EOYPongV4 pong = new EOYPongV4();
        Frame.add(pong);
        for (int i = 0; i < 1;) {
            pong.ballMovement();
            pong.repaint();

            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(EOYPongV4.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

}

桨类:

    package eoypongv4;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
/**
 *
 * 
 */
public class HumanPaddle extends JPanel {
     double y;
     double yVel;
    boolean upAccel;
    boolean downAccel;
    int player, x;


    public void draw(Graphics g) {
        g.setColor(Color.red);
        g.drawRect(800, 300, 20, 80);
    }

}

【问题讨论】:

  • I can't figure what I'm doing wrong. - 1) 你永远不会创建 HumanPaddle 类的实例。 2)您永远不会将桨添加到面板上。已经说过下面的答案是更好的设计,因为绘制单个图像比绘制 Swing 组件更有效。
  • 你为什么忽略 cmets 和你的问题的答案?

标签: java swing graphics jframe jpanel


【解决方案1】:

您的问题是,当您不应该这样做时,您正在使 Paddle 类扩展 JPanel。

只有一个组件类应该进行实际渲染,一个扩展 JPanel 并具有 protected void paintComponent(Graphics g) 方法覆盖的单个类。更好的是让你的 Paddle 类成为一个 logical 类,确保有一个 public void draw(Graphics g) 方法允许它自己绘制,但是所有真正的渲染都应该在单个显示 JPanel 的 paintComponent 方法中完成.在这个单一的 JPanel 中,调用您的桨的绘制方法以及您希望绘制的任何其他精灵的绘制方法。

例如,

// main drawing JPanel where *true* rendering is done
public class MainPanel extends JPanel {
    private Paddle paddle1 = new Paddle( /* x and y init positions */ );
    private Paddle paddle2 = new Paddle( /* x and y init positions */ );
    private Ball ball = new Ball();

    public MainPanel() {
        // Swing Timer to drive the animation
        new Timer(TIMER_DELAY, new TimerListener()).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paddle1.draw(g);
        paddle2.draw(g);
        ball.draw(g);
        // .....
    }

    private class TimerListener implements ActionListneer {
        @Override
        public void actionPerformed(ActionEvent e) {
            // move all components here
            // check for collisions
            // do program logic

            repaint();
        }
    }
}

public interface Drawable {
    public void draw(Graphics g);
}

public class Paddle implements Drawable {
    private int x;
    private int y;

    @Override
    public void draw(Graphics g) {
        // use x and y to draw rectangle
    }

    public void moveY(....) {
        // ....
    }

}

实际上,您可以给它两个 Paddle 变量,以便可以绘制两个桨...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多