简体   繁体   English

Java2D和Swing不使用JFrame和JPanels输出图像

[英]Java2D and Swing Not Outputing Images with JFrames and JPanels

I am working on the basics of a graphics program in swing and java2D to practice. 我正在使用swing和java2D中的图形程序基础知识进行练习。 I am having a problem wherein I cannot show my images. 我遇到无法显示图像的问题。 I have divided my code into 4 classes so that when the program gets larger it's easier to manage. 我将代码分为4类,以便程序变大时更易于管理。

The idea is that I have very little in the Main, that Frame initializes my first screen, that the screens can all be subdivided into their own classes, TitleScreen being one of these, and PullImage does all of the work of buffering and printing images which bothered me. 这个想法是,我在Main中几乎没有什么东西,Frame初始化了我的第一个屏幕,所有的屏幕都可以细分为自己的类,TitleScreen是其中的一个,而PullImage负责缓冲和打印图像的所有工作,困扰着我

When I run this I get an empty window and no errors, so I cannot figure out where the problem is. 运行此程序时,我得到一个空窗口,没有错误,因此我无法弄清楚问题出在哪里。

Please and Thank you for your help. 请,谢谢您的帮助。

Main 主要

package com.game.pack;

import javax.swing.JFrame;

public class Main extends JFrame {

private static final long serialVersionUID = 1L;

public final static void main(String[] args) 
{

    new Frame().initialize();
    new TitleScreen().openScreen();

}
}

Frame

package com.game.pack;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Frame extends JFrame{

private static final long serialVersionUID = 1L;

public final void initialize()
{
    JFrame frame = new JFrame("Game");
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800,600);
    panel.setSize(800,600);
    frame.setLayout(null);
    panel.setLayout(null);
    frame.setLocationRelativeTo(null);
    this.getContentPane().add(panel);
    panel.setVisible(true);
    frame.setVisible(true);
}

public final void close()
{
    dispose();
}

}

TitleScreen 标题画面

package com.game.pack;

public class TitleScreen {

    public void openScreen()
    {
        new PullImage().printARGB("icons/titleBG.png",800,600,0,0);
        new PullImage().printARGBFromSheet("icons/titleButtons.png",
            200, 125, 400, 200, 200, 40, 0, 0);
        while (1!=2)
    {
}

PullImage PullImage

package com.game.pack;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

public class PullImage {

public void printARGB(String source, int sizeX, int sizeY, int locX, int locY)
{
    Image Icon = new ImageIcon(source).getImage();
    BufferedImage BuffedImage = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = BuffedImage.getGraphics();
    graphics.drawImage(Icon,locX,locY,null);
}

public void printARGBFromSheet(String source, int sizeX, int sizeY, int locX, int locY, int width, int height, int sheetLocX, int sheetLocY)
{
    Image Icon = new ImageIcon(source).getImage();
    BufferedImage BuffedImage = new BufferedImage(sizeX,sizeY,BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = BuffedImage.getGraphics();
    graphics.drawImage(Icon, locX, locY, locX+width, locY+height, sheetLocX, sheetLocY, sheetLocX+width, sheetLocY+height, null);
}


}

One problem lies here: 一个问题就在这里:

public final void initialize()
{  
    this.getContentPane().add(panel);
}

This is setting the content pane of your frame to the panel, not the JFrame you created. 这会将框架的内容窗格设置为面板,而不是您创建的JFrame。 Essentially you're not adding it to the actual visible window. 本质上,您没有将其添加到实际的可见窗口中。 Just replace it with 只需将其替换为

frame.getContentPane().add(panel);

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

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