图像对象被绘制在自身上

时间:2016-11-18 02:11:03

标签: java image jframe jlabel

我正在尝试使用划水板制作一个简单的打砖块游戏。然而,我对Java图形有点新,需要一些帮助。

现在,我的paddle对象被涂在自身上,因此它创建了这样的东西: enter image description here

我无法理解为什么会这样。以下是我的课程:

Frame.java

package STEMFairProject;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.*;

import java.awt.event.KeyListener;

public class Frame extends JFrame implements KeyListener{

    private int WIDTH = 1400, HEIGHT = 780, paddleXPos = WIDTH/2 - 46, paddleYPos = HEIGHT - 59, paddleYVal, paddleXVale;
    private Dimension screenBounds = new Dimension(WIDTH, HEIGHT);
    private Rectangle screenSize = new Rectangle(WIDTH, HEIGHT);

    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Brick Breaker");
        setResizable(false);
        paintBackground();
        paintPaddle();
        addKeyListener(this);
    }

    public void paintPaddle() {
        setLayout(null);    
        setSize(this.screenBounds);
        Paddle paddle = new Paddle();
        add (paddle);
        paddle.setBounds(paddleXPos, paddleYPos, 182, 38);
        addKeyListener(this);

        setVisible(true);
    }

    public void paintBackground() {
        BackgroundImage bgImage = new BackgroundImage();
        add (bgImage);
        bgImage.setBounds(this.screenSize);
    }

    public static void main(String[] args) {
        new Frame();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_LEFT) {
            paddleXPos -= 10;
        }
        else if(code == KeyEvent.VK_RIGHT) {
            paddleXPos += 10;

        }
        paintPaddle();  
        paintBackground();
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

}

Paddle.java

package STEMFairProject;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Paddle extends JPanel{

    private JLabel img;

    public Paddle(){
        setLayout(null);
        try {
            img = new JLabel(new ImageIcon(ImageIO.read(new File("/Users/vaibhavbajaj/Documents/Images/Ships.jpg"))));
            img.setBounds(0, 0, 182, 38);
            add(img);
        }
        catch (IOException e)
        {
            System.out.println("Paddle Image not found");
        }
    }

    public JLabel getImg() {
        return img;
    }

}

BackgroundImage.java

package STEMFairProject;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BackgroundImage extends JPanel {

    private JLabel bgImage;
    private final int WIDTH = 1400, HEIGHT = 780;

    public BackgroundImage() {

        setLayout(null);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));

        try {
            bgImage = new JLabel(new ImageIcon(ImageIO.read(new File("/Users/vaibhavbajaj/Documents/Images/BackgroundImage.jpg"))));
            bgImage.setBounds(0, 0, WIDTH, HEIGHT);
            add(bgImage);
        }
        catch (IOException e)
        {
            System.out.println("Background Image not found");
        }
    }

}

我注意到的一个奇怪的事情是我首先画了桨,然后是背景,然后背面是背面,桨是前面的。

0 个答案:

没有答案