简体   繁体   English

如何在jpanel上绘制不同类别​​的图形

[英]How to draw graphics from different classes on jpanel

So I'm trying to make a pong game and i'm trying to draw a rectangle in the paddle class but I can't figure what I'm doing wrong. 所以我试图做一个乒乓球游戏,我试图在桨类中画一个矩形,但是我不知道自己在做什么错。 Can someone please try to help me fix the issue I have attached the entire part of my program here so it is easier for you to help me. 有人可以尝试帮助我解决此问题,因为我已经在此处附加了程序的整个部分,因此您可以更轻松地帮助我。 I am also copying this so I can get this through. 我也正在复制此文件,以便可以通过此操作。

Main class: 主班:

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

    }

}

Paddle class: 桨类:

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

}

Your problem is that you're making the Paddle class extend JPanel when you shouldn't be doing that. 您的问题是,当您不应该这样做时,您正在使Paddle类扩展JPanel。

Only one component class should do the actual rendering, a single class that extends JPanel and that has a protected void paintComponent(Graphics g) method override. 只有一个组件类应该执行实际的渲染,一个扩展JPanel并具有protected void paintComponent(Graphics g)方法重写的类。 Much better is to make your Paddle class a logical class, sure one that has a public void draw(Graphics g) method that allows it to draw itself, but again all real rendering should be done within a single display JPanel's paintComponent method. 更好的办法是使Paddle类成为一个逻辑类,并确保它具有允许其绘制自身的public void draw(Graphics g)方法,但所有真实渲染都应在单个显示JPanel的paintComponent方法中完成。 Within this single JPanel call your paddle's draw method and any other drawing method of sprites that you wish to draw. 在此单个JPanel中,调用桨的draw方法以及希望绘制的sprite的任何其他绘制方法。

eg, 例如,

// 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(....) {
        // ....
    }

}

Actually, you'd give it two Paddle variables so that both paddles can be drawn... 实际上,您会给它两个Paddle变量,以便可以绘制两个Paddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM