简体   繁体   English

仅能添加JLabel背景或JPanel背景

[英]Only able to add JLabel background or JPanel background

I am making my first game in Java. 我正在用Java做我的第一场比赛。 This is the first time using swing too. 这也是第一次使用秋千。 I am having difficulties adding objects correctly to the frame. 我在将对象正确添加到框架时遇到困难。

Some irrelevant code has been removed from other classes. 一些不相​​关的代码已从其他类中删除。 There may be several components missing that are referenced. 可能缺少一些引用的组件。

Main: 主要:

public class Main { 
    Ball ball = new Ball();
    int yBall = 0;

    public static void main(String[] args) throws InterruptedException {
        Main main = new Main();
        main.run();
        while (true) {
            main.ball(100);
        }
    }
    public void run() throws InterruptedException {
        Data data = new Data();
        Frame frame = new Frame(700, 500);
        test.setAngleX();
        frame.add(data, BorderLayout.CENTER);
    }
}

When I add ball after data(background) my ball is visible on the frame, overlapping my data. 当我在数据(背景)之后添加球时,我的球在框架上可见,与数据重叠。 If I would switch position here my data would be visible but not my ball, the ball still exists underneath. 如果我在此处切换位置,则我的数据将可见,但我的球不可见,则该球仍存在于下方。 frame.add(ball); frame.add(球); frame.setVisible(true); frame.setVisible(真);

public void ball(int yBall) throws InterruptedException {
    ball.Ball();
    yBall = ball.SendY();
}

This is my frame, no problems in here what I know: 这是我的框架,在这里我所知道的没有问题:

public class Frame extends JFrame {
    private int frameWidth;
    private int frameHeight;
    int yBall = 0;

    public Frame(int frameWidth, int frameHeight) {
        this.frameWidth=frameWidth;
        this.frameHeight = frameHeight;

        setSize(new Dimension(frameWidth, frameHeight));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

Here's my Ball. 这是我的舞会。 paintComponent is at the end, before that it is just working logics that I'm guessing will be irrelevant. paintComponent在最后,在此之前,我想它只是无关紧要的工作逻辑。

Ball: 球:

public class Ball extends JLabel{

Graphics g1;

int x = 325, y = 225;
int speed = 1;
int angleX = 0;
int angleY = 0;

public Ball() {
}

public void setAngleX() {

    angleX = ThreadLocalRandom.current().nextInt(-5, 5 + 1);    
    angleY = ThreadLocalRandom.current().nextInt(-1, 1 + 1);    

    if (angleX == 0) {
        setAngleX();
    }

    if (angleY == 0) {
        setAngleX();
    }
}

public void move() {

    if (x + angleX < 0) {
        angleX = speed;
    } else if (x + angleX > getWidth() - 25) {
        angleX = -speed;
    } else if (y + angleY < 0) {
        angleY = speed;
    } else if (y + angleY > getHeight() - 25) {
        angleY = -speed;
    }

    x = x + angleX;
    y = y + angleY;
}

public void Ball() throws InterruptedException {
    move();
    repaint();

    int sleepSpeed = angleX * speed;
    if (sleepSpeed < 0) {
        sleepSpeed = -sleepSpeed;

        Thread.sleep(10*sleepSpeed);
    }
}


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);
    g.fillOval(x, y, 25, 25);
}
public int SendY() {
    return y;
}

And finally my Data; 最后是我的数据;

public class Data extends JPanel{
    private static final long serialVersionUID = 1L;
    private Graphics2D g2;
    public Data() {
    }
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, getSize().width, getSize().height);
        g2.setColor(Color.lightGray);
        g2.fillRect(350, 0, 10, 400);
    }

So what happens is that I either get my data(background) on my frame or I get my Ball bouncing around as it should but on an empty background, depending on which one I add last in my main. 因此,发生的事情是我要么将数据(背景)放在框架上,要么将球按原样反弹,但在空的背景上,具体取决于我在主机中添加的最后一个。 I'm guessing it's bcz of overlapping but I can't find a solution working for my code, no matter how much I search.. 我猜这是重叠的,但是无论我搜索多少,我都找不到适合我的代码的解决方案。

that I either get my data(background) on my frame or I get my Ball bouncing around as it should but on an empty background, depending on which one I add last in my main. 我要么将数据(背景)放到框架上,要么将球按原样弹起,但背景为空,具体取决于我在主机中添加的最后一个。

You can't add both components to the frame. 您不能将两个组件都添加到框架中。 you need to have a parent/child relationship. 您需要建立父母/子女关系。

So the logic should be something like: 因此,逻辑应类似于:

background.add( ball );
frame.add( background );

Then the frame will be painted, then the background and finally the ball. 然后将绘制框架,然后绘制背景,最后绘制球。

Also, the Ball class will need to implement the getPreferredSize() method so component has a reasonable size. 同样,Ball类将需要实现getPreferredSize()方法,以便组件具有合理的大小。 You should be painting the Ball at offset (0, 0) in the paint component method. 您应该在绘画组件方法中以偏移量(0,0)绘画球。 When you want to move the ball on the background panel, then you just use setLocation(...) on the ball to reposition it on the background. 当您想在背景面板上移动球时,只需在球上使用setLocation(...)将球重新放置在背景上即可。 When you create the Ball you would also need to set the size of the Ball to the preferred size so the Ball has a size/location to be painted on the background panel. 创建Ball时,还需要将Ball的大小设置为首选大小,以便Ball具有要在背景面板上绘制的大小/位置。 The layout of the background panel would need to be set to null, so you can freely move the ball around the panel. 需要将背景面板的布局设置为null,以便您可以在面板上自由移动球。

You should NOT be extending JLabel, you are not using any features of the JLabel. 您不应该扩展JLabel,没有使用JLabel的任何功能。 You are doing custom painting so just extend JPanel or JComponent. 您正在执行自定义绘画,因此只需扩展JPanel或JComponent。 Read the section from the Swing tutorial on Custom Painting for more information and working examples. 阅读有关定制绘画的Swing教程中的部分,以获取更多信息和工作示例。

Also, method names should NOT start with an upper case character. 另外,方法名称不应以大写字母开头。 Ball() is not a proper method name. Ball()不是正确的方法名称。 It looks like the class name. 它看起来像类名。 The method should be more descriptive. 该方法应更具描述性。

Or the other approach, instead of having two components, is to have a single component. 或者,另一种方法不是具有两个组件,而是具有一个组件。 Then the paintComopnent(...) method of that component would paint both: 然后,该组件的paintComopnent(...)方法将同时绘制两个:

  1. the background 的背景
  2. the ball

Then this way you would paint the Ball relative to the background panel which is how your logic currently works. 然后,通过这种方式,您将相对于背景面板绘制Ball,这就是您的逻辑当前的工作方式。

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

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