如何在jpanel上绘制来自不同类的图形

时间:2018-05-23 02:35:05

标签: java swing graphics jframe jpanel

所以我正在尝试制作乒乓球游戏,我正试图在桨类中绘制一个矩形,但我无法弄清楚我做错了什么。有人可以尝试帮助我解决我已经附加我的程序的整个部分的问题,这样你就更容易帮助我。我也在复制这个,所以我可以通过。

主要课程:

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

}

1 个答案:

答案 0 :(得分:3)

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

只有一个组件类应该进行实际渲染,一个扩展JPanel并且具有protected void paintComponent(Graphics g)方法覆盖的类。更好的方法是让你的Paddle类成为一个逻辑类,确保有一个允许它自己绘制的public void draw(Graphics g)方法,但是所有真正的渲染都应该在一个显示器中完成JPanel& #39;的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变量,这样就可以绘制两个桨......