简体   繁体   English

如何在JPanel中查看BufferedImage?

[英]How to see BufferedImage in JPanel?

I'm building a PongClone but I encounter a bug. 我正在构建PongClone,但遇到错误。 **I think the bug is cause by JPanel. **我认为该错误是由JPanel引起的。

I tried the Image instead of BufferedImage. 我尝试使用Image而不是BufferedImage。 I tried the drawImage outside the paintComponent method. 我在paintComponent方法之外尝试了drawImage。 I create to another panel and then add that panel inside a mainpanel. 我创建到另一个面板,然后将该面板添加到主面板中。

Menu Class 菜单类

package me.pong;

import javax.swing.*;

public class TestMenu {
    JFrame frame;

    public void createFrame () {
        TestMain main = new TestMain ();
        frame = new JFrame("TEST");
        frame.setSize (800, 450);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane ().add (main.panel);
        frame.setVisible (true);
    }
}

MainClass MainClass

package me.pong;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class TestMain extends JPanel {
    JPanel panel = new JPanel ();
    BufferedImage img;
    Graphics g;

    public static void main (String[] args) {
        TestMain testMain = new TestMain ();
        TestMenu menu = new TestMenu ();
        menu.createFrame ();
        testMain.drawGraphics ();
    }

    public void drawGraphics(){
        panel.add (new TestMain ());
        img = new BufferedImage(800, 450, BufferedImage.TYPE_INT_RGB);
        g = img.createGraphics ();
        g.drawString ("TEST STRING 2", 250,250);
    }

    @Override
    protected void paintComponent (Graphics g) {
        super.paintComponent (g);
        g.clearRect (0,0,800,450);
        g.drawImage (img, 0,0,null);
        g.setColor (Color.white);
        g.drawString ("TEST STRING", 500,250);
        g.setColor (Color.black);
        g.drawRect (150,100,10,70);
    }
}

I expect the Image fill the component but actual output is little tiny box. 我希望图像填充该组件,但实际输出很小。 Just like that 就这样

EDIT: Delete the code and added MCVE/SSCCE Code(I didn't know that). 编辑:删除代码并添加MCVE / SSCCE代码(我不知道)。 Still same. 还是一样。 If I add the image inside the frame it's works but other way doesn't. 如果我将图像添加到框架内,则可以,但是其他方法则不行。 I know I'm missing something, but I don't know what that is. 我知道我想念一些东西,但我不知道那是什么。

**Yes. **是。 Problem caused by JPanel but I don't know how to fix it. 由JPanel引起的问题,但我不知道如何解决。

The extra panel declared within the custom painted class that is a panel was not only unnecessary, but the source of problems. 在自定义绘画类中声明的额外面板不仅是不必要面板,而且是问题的根源。 See further comments in code. 请参阅代码中的其他注释。

在此处输入图片说明

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class TestMain extends JPanel {

    JFrame frame;
    // Not needed or useful! 
    //JPanel panel = new JPanel(); 
    BufferedImage img;
    Graphics g;

    public static void main(String[] args) {
        TestMain testMain = new TestMain();
        testMain.createFrame();
        testMain.drawGraphics();
    }

    public void createFrame() {
        TestMain main = new TestMain();
        frame = new JFrame("TEST");
        frame.setSize(400, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.getContentPane().add(main.panel);
        frame.getContentPane().add(main);
        frame.setVisible(true);
    }

    public void drawGraphics() {
        //panel.add(new TestMain());
        add(new TestMain());
        img = new BufferedImage(800, 450, BufferedImage.TYPE_INT_RGB);
        g = img.createGraphics();
        g.drawString("TEST STRING 2", 250, 250);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.clearRect(0, 0, 800, 450);
        // all JComponent instances are image observers
        //g.drawImage(img, 0, 0, null);
        g.drawImage(img, 0, 0, this);
        g.setColor(Color.WHITE);
        // NEW! Otherwise invisible
        g.setColor(Color.RED); 
        g.drawString("TEST STRING", 200, 100);
        g.setColor(Color.BLACK);
        g.drawRect(150, 100, 10, 70);
    }
}

As an aside: 作为旁白:

  1. That code still has problems, but I thought it best to stick closely to fixing only the immediate problem. 该代码仍然存在问题,但我认为最好还是只解决当前的问题。
  2. The easiest way to display a BufferedImage is to show it in a JLabel via an ImageIcon . 显示BufferedImage的最简单方法是通过ImageIconJLabel显示它。

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

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