在 JPanel 上绘制图形而不使用 PaintComponent 方法和重绘调用

发布于 2024-11-19 21:23:35 字数 505 浏览 7 评论 0原文

我想在 JPanel 上绘制图形。现在,我正在做的是,我使用paintComponent方法来定义绘图:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(10,10,100,100);
}

然后我在想要放置图形的任何地方调用repaint()。

但我想知道是否有一种方法可以将 Graphics 添加到 JPanel 中,就像添加组件一样,而不使用这种 PaintComponent 方法:panel.add(myComponent)。我看到 Graphics 类型无法启动,但也许有另一种类型可以让我这样做。

我很确定许多 GUI(例如 FANG Engine)都有此选项,但我在 Swing 中看到的所有示例都使用此方法。如果没有这个,有什么建议吗?因为它有时会扰乱我程序的整体设计。

提前致谢。

I want to paint Graphics on JPanel. Right now, what I am doing is, I use the paintComponent method to define the drawing:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(10,10,100,100);
}

Then I call repaint() wherever I want to put the graphics.

But I wonder if there is a way to add Graphics to JPanel just like adding components without using this paintComponent method: panel.add(myComponent). I saw that Graphics type cannot be initiated, but maybe there might be another type to let me do that.

I'm pretty sure lots of GUIs such as FANG Engine have this option, but all the examples I saw with Swing was using this method. Any suggestions to do without this? Because it is messing up with the overall design of my program sometimes.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小霸王臭丫头 2024-11-26 21:23:35

最简单的方法是创建一个自定义 Icon 对象,它负责你的绘画。然后,您可以通过 JLabel 将图标添加到面板(或将其放在按钮或任何其他带有图标的组件上),同时抽象出绘画工作。

The easiest way would be to create a custom Icon object, which is in charge of your painting. You can then add your icon to your panel via a JLabel (or put it on a button or any other component that takes icons), while abstracting out the painting work.

無處可尋 2024-11-26 21:23:35

您可以查看图像和缓冲图像 api。除了 PaintComponent 和 Graphics g 之外,您还可以通过多种方式生成和处理缓冲图像。我想如果您生成缓冲图像然后将它们加载到 JPanel 上,您将能够获得类似的效果。您可以使用 setRGB 方法来绘制您想要的内容。它不像绘画方法那么通用,但它是一种替代方法。

You can take a look at the image and buffered image apis. You can generate and handle buffered images in a lot of ways other than the paintcomponent and graphics g. I guess if you generate buffered images and then load them on your JPanel you will be able to get a similar effect. You can use the setRGB method to paint what you want. It's not as versatile as the paint method but it is an alternative.

青芜 2024-11-26 21:23:35

一个想法是将每一个自定义绘画放入一个单独的 JPanel 中,
然后添加这个。

public class MyFrame extends JFrame {
    //initialize frame...
    //put your components

    //now put your jpanel
    MyPanel custom = new MyPanel();
    custom.setBounds(10, 10, 100, 100);

    this.add(custom);
}

public class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //draw your stuff...
    }
}

当然,这会使其动态性降低很多...

编辑:

谈论分离,如果您想将屏幕分成两部分:
一个用于自定义绘画,一个用于组件,例如游戏,
那么这个解决方案应该适合您的需求......

An idea would be to put every custom painting inside an individual JPanel,
then add this.

public class MyFrame extends JFrame {
    //initialize frame...
    //put your components

    //now put your jpanel
    MyPanel custom = new MyPanel();
    custom.setBounds(10, 10, 100, 100);

    this.add(custom);
}

public class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //draw your stuff...
    }
}

Of course, this would make it a lot less dynamic...

EDIT:

Talking about separation, if you want to separate your screen in e.g. two parts:
One for the custom painting and one for the components, for example for a game,
then this solution should fit your needs...

滴情不沾 2024-11-26 21:23:35
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JPanel viewportPanelTop = new JPanel();
        JPanel viewportPanelBottom = new JPanel();
        JPanel viewportPanelCenter = new JPanel();
        viewportPanelCenter.setBackground(Color.black);
        viewportPanelCenter.setPreferredSize(new Dimension(600, 400));
        JPanel viewportPanelWest = new JPanel();
        JPanel viewportPanelEast = new JPanel();

        JFrame frame = new JFrame();
        frame.setTitle("JFrame");
        frame.setLocation(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(viewportPanelTop, BorderLayout.NORTH);
        frame.add(viewportPanelWest, BorderLayout.WEST);
        frame.add(viewportPanelCenter, BorderLayout.CENTER);
        frame.add(viewportPanelEast, BorderLayout.EAST);
        frame.add(viewportPanelBottom, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    private Main() {
    }
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JPanel viewportPanelTop = new JPanel();
        JPanel viewportPanelBottom = new JPanel();
        JPanel viewportPanelCenter = new JPanel();
        viewportPanelCenter.setBackground(Color.black);
        viewportPanelCenter.setPreferredSize(new Dimension(600, 400));
        JPanel viewportPanelWest = new JPanel();
        JPanel viewportPanelEast = new JPanel();

        JFrame frame = new JFrame();
        frame.setTitle("JFrame");
        frame.setLocation(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(viewportPanelTop, BorderLayout.NORTH);
        frame.add(viewportPanelWest, BorderLayout.WEST);
        frame.add(viewportPanelCenter, BorderLayout.CENTER);
        frame.add(viewportPanelEast, BorderLayout.EAST);
        frame.add(viewportPanelBottom, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    private Main() {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文