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クラスを好きなように使えるようにしたいと思っています。g.drawRect(...)
現在、私は関数内のようなことしかできませんpaintComponent()
。これは私の目的には役立つかもしれませんが、もっと柔軟性が欲しいです。
ありがとう。
編集-了解しました。外部の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関数は私にとってばかげて長く複雑になっているので、可能な限りそれを分割したいと思います。