简体   繁体   English

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

[英]Can't load Image one time using getGraphics() method

Hi I'm new to java GUI Programming. 嗨,我是Java GUI编程的新手。 I created Jframe(MainFrame) and add JPanel(OutPanel) Which has another Jpanel(InnerPanel) . 我创建了Jframe(MainFrame)并添加了具有另一个JPanel(OutPanel) Jpanel(InnerPanel) I try to achieve drawing Image in InnerPanel , not drawing OutPanel . 我尝试在InnerPanel实现绘制Image,而不是绘制OutPanel I want OutPanel used to be just Container . 我希望OutPanel以前只是Container So as you see TestA. 如您所见,TestA。 I get Graphics from InnerPanel in OutPanel 's paintComponent() which is overided method. 我从InnerPanelpaintComponent()中的OutPanel获取了Graphics ,这是一种覆盖方法。

So finally I can draw using InnerPanel 's Graphics in OutPanel 's paintComponent() method. 所以最后我可以在OutPanelpaintComponent()方法中使用InnerPanelGraphics进行OutPanel but It couldn't work well. 但是效果不好。 It couldn't draw Image one time when program starts. 程序启动时无法绘制图像一次。 when I hided window and shown again, the program shows image. 当我隐藏窗口并再次显示时,程序显示图像。 Even though that is part of Image, not all Image. 即使那是Image的一部分,也不是全部Image。

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);
        }
    }
}

So I try diffrerent way(TestB). 所以我尝试了不同的方式(TestB)。 Only different thing is I just moved drawImage() method and getGraphics() thing to InnerPanel 's paintComponent() from OutPanel 's paintComponent() . 唯一不同的是,我只是将drawImage()方法和getGraphics()东西从InnerPanelpaintComponent() OutPanelpaintComponent() Here's another Code TestB. 这是另一个代码TestB。 and It works well. 而且效果很好。

Why this happens. 为什么会这样。 Is it relates to context ?. 它与context吗? What is Context . 什么是Context and could I draw InnerPanel's Image in OutPanel ? 我可以在OutPanel绘制InnerPanel's图像吗?

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);
        }
    }
}

The paintComponent() method of a component is responsible for painting itself only. 组件的paintComponent()方法仅负责绘画自身。 It should never know or care about any other component. 它永远都不应知道或关心任何其他组件。

I want OutPanel used to be just Container. 我希望OutPanel以前只是Container。

Then do just that. 然后就那样做。 Create the panel and set the layout manager for the outer panel and then add the outer panel to the JFrame. 创建面板并为外部面板设置布局管理器,然后将外部面板添加到JFrame。

Then create your inner panel and add it to the outer panel. 然后创建您的内部面板并将其添加到外部面板。 Make sure you override the getPreferredSize() method of the inner panel so the layout manager of the outer panel can do its job. 确保覆盖内部面板的getPreferredSize()方法,以便外部面板的布局管理器可以执行其工作。

Read the section from the Swing tutorial on Custom Painting for more information and working examples to start with. 阅读有关定制绘画的Swing教程中的部分,以获取更多信息和工作示例。

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

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