我可以在已经有图形的JFrame中添加JPanels吗



所以我最近开始玩java图形,遇到了一个问题。我在JFrame上绘制了图像,但没有了解到,如果图像都在一个JFrame中,则图像的边界不能相互重叠(设置另一个图像所在的边界会使图像消失)。我听说使用多个JPanel来解决这个问题。我不知道如何做到这一点,因为我在程序中使用了JFrame而不是JPanel,所以在这个问题上提供一些帮助将不胜感激。谢谢,如果这个问题很愚蠢,我深表歉意。(其他人似乎认为我的另一个问题与这个问题相同,那就是设置边界的问题,这个问题是关于边界重叠和相互覆盖的问题)以下是我的课程:

窗口类别-

package game.thirdTry;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends JFrame {
    private static Window instance;
    public static Window getInstance() {
        if(instance == null) {
            instance = new Window("Game");
        }
        return instance;
    }
    private Window(String name) {
        super(name);
        setSize(1200, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        //setUndecorated(true);
        addKeyListener(new UserInput());
        //this.getContentPane().getSize();
        WindowStructure banner = new WindowStructure("Beatles Logo.jpg", 0, 0, getWidth(), 75);
        //WindowStructure fball = new WindowStructure("fireball.100x100.png", 100, 100, 100, 100);
        WindowStructure fball = WindowStructure.getInstanceF();
        System.out.println("Fball.xSize: " + fball.xSize + ", Fball.ySize: " + fball.ySize);
        System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
        //banner.setBounds(banner.xLoc, banner.yLoc, banner.xSize, banner.ySize);
        //fball.setBounds(fball.xLoc, fball.yLoc, fball.xLoc + fball.xSize, fball.ySize + fball.ySize);
        banner.setBounds(0, 0, getWidth(), getHeight());
        fball.setBounds(0, 75, getWidth(), getHeight());
        add(fball, null);
        add(banner, null);
        setVisible(true);
        while(true){
            System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
            repaint();
            try{
            Thread.sleep(10);
            }catch (Exception e){
            }
        }
    }
/*
    public void paint(Graphics g) {
        super.paintComponents(g);
    }
*/
}

图像类别-

package game.thirdTry;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class WindowStructure extends JPanel {
    private static WindowStructure fball;
    public static WindowStructure getInstanceF(){
        if(fball == null){
            fball = new WindowStructure("fireball.100x100.png", 0, 75, 100, 100);
        }
        return fball;
    }
    ImageIcon imageIcon;
    int xLoc, yLoc, xSize, ySize;
    public WindowStructure(String bannerImg, int xLoc, int yLoc, int xSize, int ySize){
        URL bannerImgURL = getClass().getResource(bannerImg);
        imageIcon = new ImageIcon(bannerImgURL);
        this.xLoc = xLoc;
        this.yLoc = yLoc;
        this.xSize = xSize;
        this.ySize = ySize;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);
    }
}

KeyListener/用户输入类-

package game.thirdTry;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class UserInput implements KeyListener {
    // Window window = Window.getInstance();
    WindowStructure fball = WindowStructure.getInstanceF();
    boolean goUp = false;
    boolean goDown = false;
    boolean goLeft = false;
    boolean goRight = false;
    public void moveUpDown() {
        if (goUp && goDown) {
            if (fball.yLoc > 0) {
                fball.yLoc -= 10;
            }
        } else if (goUp) {
            if (fball.yLoc > -5) {
                fball.yLoc -= 10;
            }
        } else if (goDown) {
            if (fball.yLoc < 480) {
                fball.yLoc += 10;
            }
        }
        System.out.println("moveUpDown() method called");
    }
    public void moveLeftRight() {
        if (goLeft && goRight) {
            if (fball.xLoc < 1100) {
                fball.xLoc += 10;
            }
        } else if (goRight) {
            if (fball.xLoc < 1110) {
                fball.xLoc += 10;
            }
        } else if (goLeft) {
            if (fball.xLoc > 0) {
                fball.xLoc -= 10;
            }
        }
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            goUp = true;
            goDown = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            goDown = true;
            goUp = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            goLeft = true;
            goRight = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            goRight = true;
            goLeft = false;
        }
        moveUpDown();
        moveLeftRight();
        System.out.println("keyPressed() was called");
    }
    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            goUp = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            goDown = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            goLeft = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            goRight = false;
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
}
//g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);

问题是在绘制图像时缩放图像。因此,图像占据了整个窗口空间,是的,它会在第一个绘制的图像之上绘制。

只需按实际大小绘制图像:

g2d.drawImage(imageIcon.getImage(), xLoc, yLoc,  this);

这是使用JLabel的另一个原因。你不需要担心尺寸。您只需将标签定位在特定位置,即可绘制图像。代码要简单得多。使用这种方法,您不需要担心使面板不透明。

现在代码的问题是,您需要使面板变大,因为您绘制的图像是相对于WindowStructure面板的。因此,如果你的图像是(25 x 25),并且你希望图像绘制在(100,100),你需要制作面板(125,125),这意味着面板需要是透明的,这样面板中不包含图像的部分就不会被绘制。使用JLable,标签的大小将始终为(25,25),因此您只需设置标签的位置,就会只绘制(25,25%)个像素。

我稍后会详细研究JLabels

现在看看。没有比现在更好的时间了!

另外,WindowStructure类的设计也很糟糕。不需要静态方法和变量。每个图像应该彼此独立。

相关内容

  • 没有找到相关文章

最新更新