Java - 使用paintComponent进行图形,从内部调用函数?

发布于 2024-11-27 06:26:00 字数 1432 浏览 1 评论 0原文

我真的对 paintComponent 函数在我的 JPanel 中如何工作的程序流程感到困惑。理想情况下,我希望能够访问 Graphics 对象,以便根据我的程序流程从其他函数中绘制内容。我的想法如下:

private Graphics myG;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    myG = g; //I want a graphics object that I can just draw with. 
             //Should I just initialize to myG = new Graphics(); or something?
    //private Graphics myG = new Graphics(); does not seem to work        
  }

//this is what's getting called, where I want to call other functions that 
//can use myG to draw graphics as I please
public void startPanelView() {      

    myG.drawRect(350, 20, 400, 300); //doesn't work

    otherFunctionForGraphics(); //which will also use myG.  
}

我希望我已经在这里说清楚了。我只是希望能够随意使用 Graphics 类。目前我只能在 paintComponent() 函数内部执行诸如 g.drawRect(...) 之类的操作。这可以满足我的目的,但我想要更多的灵活性。

谢谢。

编辑 - 好吧,我知道我不应该尝试引用外部的 Graphics 对象。但是我应该如何将应用程序逻辑与 PaintComponent 函数分离呢?现在这个类看起来有点混乱,因为我有以下内容:

public void paintComponent(Graphics g) {

    if (flag1 == true) {
        //do some graphics stuff, that I would prefer to be in another function
    }

    if (flag2 == true) {
        //do some other graphics stuff, again, which I would prefer 
        //to be in another function
    }

    //... etc. More of these cases.
}

基本上,paintComponent 函数对我来说变得愚蠢又长且复杂,所以我想以任何可能的方式分解它。

I'm really confused with the program flow for how the paintComponent function works within my JPanel. Ideally I'd like to have access to the Graphics object to draw stuff from other functions based on my program flow. I'm thinking along the lines of the following:

private Graphics myG;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    myG = g; //I want a graphics object that I can just draw with. 
             //Should I just initialize to myG = new Graphics(); or something?
    //private Graphics myG = new Graphics(); does not seem to work        
  }

//this is what's getting called, where I want to call other functions that 
//can use myG to draw graphics as I please
public void startPanelView() {      

    myG.drawRect(350, 20, 400, 300); //doesn't work

    otherFunctionForGraphics(); //which will also use myG.  
}

I hope I've made myself clear here. I just want to be able to do use the Graphics class as I please. Currently I can only do stuff like g.drawRect(...) inside of the paintComponent() function. This could work for my purposes but I'd like more flexibility.

Thanks.

EDIT - Alright, I understand that I should not try and reference a Graphics object outside. But how should I go about separating application logic from the paintComponent function? Right now this class is looking a little messy because I have the following:

public void paintComponent(Graphics g) {

    if (flag1 == true) {
        //do some graphics stuff, that I would prefer to be in another function
    }

    if (flag2 == true) {
        //do some other graphics stuff, again, which I would prefer 
        //to be in another function
    }

    //... etc. More of these cases.
}

And basically the paintComponent function is getting stupidly long and complicated for me, so I would like to break it up in whatever ways possible.

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

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

发布评论

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

评论(4

情归归情 2024-12-04 06:26:00

从面板对象上的 startPanelView 方法调用 repaint。您永远不应该在 paint 方法之外对图形对象进行操作,也不应该维护对图形对象的引用以便在 paint 方法之外使用。

Call repaint from your startPanelView method on the panel object. You should never act on the graphics object outside of the paint methods, nor should you maintain a reference to the graphics object for use outside of the paint method.

青柠芒果 2024-12-04 06:26:00

其他人是对的,您不能将 Graphics 对象保留为成员变量,但如果问题是您的 paintComponent 方法变得太长,您仍然可以传递 < code>Graphics Swing 为您提供其他方法的参数:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(350, 20, 400, 300);
    doSomeStuff(g);
    if (flag1) {
        doMoreStuff(g);
    } else {
        doDifferentStuff(g);
    }
}

The others are right that you cannot keep the Graphics object as a member variable, but if the problem is that your paintComponent method is getting too long, you can still pass the Graphics Swing gives you as an argument to other methods:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(350, 20, 400, 300);
    doSomeStuff(g);
    if (flag1) {
        doMoreStuff(g);
    } else {
        doDifferentStuff(g);
    }
}
丢了幸福的猪 2024-12-04 06:26:00

我想访问 Graphics 对象,以便根据我的程序流程从其他函数中绘制内容。我的想法如下:

不,您不应该在标准 Swing 应用程序中这样做,因为获得的 Graphics 对象将不会持久,并且只要 JVM 或操作系统决定需要重新绘制,您的绘图就会消失。考虑创建列表或其他要绘制的对象集合(例如从实现 Shape 的类),并在 PaintComponent 方法中迭代这些集合。另一种技术是在 BufferedImage 上绘制,然后在 PaintComponent 方法中显示它。

I'd like to have access to the Graphics object to draw stuff from other functions based on my program flow. I'm thinking along the lines of the following:

No, you shouldn't do that in a standard Swing application as the Graphics object obtained will not persist and your drawings will disappear whenever the JVM or the operating system decide that a repaint is necessary. Consider creating Lists or other collections of objects to be drawn (such as from classes that implement Shape) and iterating through these collections in the paintComponent method. Another technique is to draw on a BufferedImage and then display it in your paintComponent method.

甜警司 2024-12-04 06:26:00

用于绘画/自定义绘画/2D 图形的基本 JComponent 在 Swing 或 Image/ImageIcon 中,是JLabel

不要调用其他视频或类(es) 来自 2D 图形CustomPainting,一些有价值的示例是 此处此处,在此论坛上搜索有关 的优秀建议自定义绘画2D图形

basic JComponent for Painting/Custom Painting/2D Graphics in Swing or for Image/ImageIcon too, is JLabel,

don't call another viod(s) or class(es) from 2D Graphics or CustomPainting, some valuable examples are here or here, search on this forum for excelent suggestions about Custom Painting and 2D Graphics

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