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

Java-使用paintComponent进行图形,从内部调用函数?,java,swing,graphics,jpanel,paintcomponent,Java,Swing,Graphics,Jpanel,Paintcomponent,对于paintComponent函数如何在我的JPanel中工作,我对程序流程感到非常困惑。理想情况下,我希望能够访问图形对象,以便根据我的程序流从其他函数中绘制内容。我的思路如下: private Graphics myG; public void paintComponent(Graphics g) { super.paintComponent(g); myG = g; //I want a graphics object that I can just draw with

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

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.  
}
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()
函数中执行类似
g.drawRect(…)
的操作。这对我来说是可行的,但我希望有更多的灵活性

谢谢

编辑-好的,我明白我不应该尝试引用外部的图形对象。但是我应该如何将应用程序逻辑与paintComponent函数分离呢?现在这个类看起来有点混乱,因为我有以下几点:

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.  
}
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函数对我来说变得非常长和复杂,所以我想用任何可能的方式来分解它

从面板对象上的
startPanelView
方法调用
repaint
。切勿在
paint
方法之外对图形对象进行操作,也不应在
paint
方法之外维护对图形对象的引用

我希望能够访问图形对象,以便根据我的程序流从其他函数中绘制内容。我的思路如下:

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

在Swing或to中绘制/自定义绘制/2D图形的基本方法是


不要从或中调用另一个viod或类,一些有价值的示例是或,在此论坛上搜索有关
自定义绘制
2D图形
的优秀建议。其他人是对的,您不能将
图形
对象作为成员变量,但是,如果问题是
paintComponent
方法太长,您仍然可以将
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);
    }
}

当然,我会试试的。但是为什么它是这样设计的呢?出于好奇,每个Swing组件都有自己的图形对象。您能想象必须跟踪每个组件的图形对象,并在正确的时间以正确的顺序分别操作它们吗?+1提到替代方法(使用BuffereImage)时,海报允许访问paintComponent()方法之外的单独图形对象。希望中的“基于图像绘制”示例将扩展这种方法。