How to see BufferedImage in JPanel?

纵饮孤独 提交于 2019-12-24 12:34:21

问题


I'm building a PongClone but I encounter a bug. **I think the bug is cause by JPanel.

I tried the Image instead of BufferedImage. I tried the drawImage outside the paintComponent method. 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

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). 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.


回答1:


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.


来源:https://stackoverflow.com/questions/55580715/how-to-see-bufferedimage-in-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!