问题描述
我正在构建PongClone,但遇到错误. **我认为该错误是由JPanel引起的.
I'm building a PongClone but I encounter a bug. **I think the bug is cause by JPanel.
我尝试使用Image而不是BufferedImage.我在paintComponent方法之外尝试了drawImage.我创建了另一个面板,然后将该面板添加到主面板中.
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.
菜单类
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
编辑:删除代码并添加MCVE/SSCCE代码(我不知道).还是一样.如果我将图像添加到框架内,则可以,但是其他方法则不行.我知道我想念什么,但我不知道那是什么.
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.
**是的.由JPanel引起的问题,但我不知道如何解决.
**Yes. Problem caused by JPanel but I don't know how to fix it.
推荐答案
在自定义绘画类中声明的是面板的额外面板不仅不必要,而且是问题的根源.查看代码中的其他注释.
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);
}
}
顺便说一句:
- 该代码仍然存在问题,但是我认为最好只坚持解决当前的问题.
- 显示
BufferedImage
的最简单方法是通过ImageIcon
在JLabel
中显示它.
- That code still has problems, but I thought it best to stick closely to fixing only the immediate problem.
- The easiest way to display a
BufferedImage
is to show it in aJLabel
via anImageIcon
.
这篇关于如何在JPanel中查看BufferedImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!