嗨,我是Java GUI编程的新手。我创建了Jframe(MainFrame)并添加了具有另一个JPanel(OutPanel)Jpanel(InnerPanel)。我尝试在InnerPanel中而不是在OutPanel中实现绘制图像。我希望OutPanel过去只是Container。如您所见,TestA。我从GraphicsInnerPanelOutPanel中获得了paintComponent(),这是一种覆盖方法。

所以最后我可以在InnerPanelGraphics方法中使用OutPanelpaintComponent()进行绘制。但是效果不好。程序启动时无法绘制图像一次。当我隐藏窗口并再次显示时,程序显示图像。即使这是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);
        }
    }
}


所以我尝试了不同的方式(TestB)。唯一不同的是,我只是将drawImage()方法和getGraphics()内容从InnerPanelpaintComponent()移到了OutPanelpaintComponent()。这是另一个代码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);
        }
    }
}

最佳答案

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


  我希望OutPanel以前只是Container。


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

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

阅读有关Custom Painting的Swing教程中的部分,以获取更多信息和工作示例。

09-04 08:34