无法使用getGraphics()方法加载Image一次

时间:2016-09-21 02:39:16

标签: java image swing

您好我是Java GUI编程的新手。我创建了Jframe(MainFrame)并添加JPanel(OutPanel)其他Jpanel(InnerPanel)。我尝试在InnerPanel中绘制图片,而不是绘制OutPanel。我希望OutPanel过去只是Container。所以当你看到TestA。我从Graphics InnerPanel中的OutPanel获得了paintComponent()这是一种覆盖方法。

最后,我可以使用InnerPanel Graphics OutPanel方法中的paintComponent() import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class TestA{ private static Image image = GUI.loadImage("PlayerBoard.jpg"); public static void main(String[] args) { TestA testA = new TestA(); } public TestA() { JFrame mainFrame = new JFrame("Main Frame"); mainFrame.setLayout(null); mainFrame.setSize(500, 500); mainFrame.setVisible(true); mainFrame.setBackground(Color.black); mainFrame.setLocation(800, 400); OutPanel outPanel = new OutPanel(); mainFrame.getContentPane().add(outPanel); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); outPanel.repaint(); } private class OutPanel extends JPanel { JPanel innerPanel; public OutPanel() { this.setLayout(null); this.setLocation(0, 0); this.setSize(500, 50); this.setBackground(Color.red); innerPanel = new JPanel(); this.innerPanel.setSize(400, 50); this.innerPanel.setVisible(true); this.add(innerPanel); } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); int width = 500; int height = 50; BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); Graphics gBuffer = resized.createGraphics(); gBuffer.drawImage(TestA.image, 0, 0, width, height, this); Graphics gPanel = innerPanel.getGraphics(); gPanel.drawImage(resized, 0, 0, width, height, this); } } } 进行绘制。但它不能很好地运作。程序启动时,它无法一次绘制图像。当我隐藏窗口并再次显示时,程序显示图像。即使这是Image的一部分,也不是所有的Image。

drawImage()

所以我尝试不同的方式(TestB)。唯一不同的是,我只是将getGraphics()方法和InnerPanel内容移至paintComponent() OutPanel来自paintComponent()' s {{1 }}。这是另一个Code TestB。而且效果很好。

为什么会这样。它与context有关吗?什么是Context。我可以在InnerPanel's中绘制OutPanel图片吗?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

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

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

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

public class TestB {

    private static Image image = GUI.loadImage("PlayerBoard.jpg");

    public static void main(String[] args) {
        TestB testB = new TestB();
    }

    public TestB() {

        JFrame mainFrame = new JFrame("Main Frame");
        mainFrame.setLayout(null);
        mainFrame.setSize(500, 500);
        mainFrame.setVisible(true);
        mainFrame.setBackground(Color.black);
        mainFrame.setLocation(800, 400);

        OutPanel outPanel = new OutPanel();

        mainFrame.add(outPanel);

        outPanel.repaint();

    }

    private class OutPanel extends JPanel {

        JPanel innerPanel;

        public OutPanel() {

            this.setLayout(null);
            this.setLocation(0, 0);
            this.setSize(500, 50);
            this.setBackground(Color.red);

            innerPanel = new InnerPanel(this);
            this.innerPanel.setSize(500, 50);
            this.innerPanel.setVisible(true);
            this.add(innerPanel);

            this.repaint();
        }
    }

    private class InnerPanel extends JPanel {

        OutPanel outPanel;

        public InnerPanel(OutPanel outPanel) {
            this.outPanel = outPanel;
        }

        @Override
        protected void paintComponent(Graphics g) {
            // TODO Auto-generated method stub
            super.paintComponent(g);

            int width = 500;
            int height = 50;

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawImage(TestB.image, 0, 0, width, height, this);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

组件的paintComponent()方法仅负责绘制自身。它永远不应该知道或关心任何其他组件。

  

我希望OutPanel过去只是容器。

然后就这样做。创建面板并为外部面板设置布局管理器,然后将外部面板添加到JFrame。

然后创建内部面板并将其添加到外部面板。确保覆盖内部面板的getPreferredSize()方法,以便外部面板的布局管理器可以完成其工作。

阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。