私は簡単なペイントプログラムを書いています。 私はJPanel
を作成しており、 "public void paintComponent(Graphics g)
"と書かれており、適切なListeners
も作成しました。問題は、私が新しい形を描くたびに、前の形が消えてしまうことです。以前の形をどのように保持できるのか誰にも分かりますか?私はsuper.paintComponent(g)
を持ち去ることができますが、Jpanel's
layout
は歪んでしまいます。 どのような提案も高く評価されています。 :) これは私のpaintComponentメソッドです:paintComponentは以前塗装されたコンポーネントを削除します
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = xend-xstart;
int height = yend - ystart;
if(width<0)
width *= -1;
if(height <0)
height *= -1;
if(color!= null && shape !=null){
if(fill.isSelected())
{
g.setColor(color);
if(shape.equals("Rectangle"))
g.fillRect(xstart, ystart, width, height);
if(shape.equals("Square"))
g.fillRect(xstart, ystart, width, width);
if(shape.equals("Circle"))
g.fillOval(xstart,ystart,width ,width);
}
}
}
@ Andrew Thompsonありがとうございましたが、これがより多くの形を描くので、これは効率に影響しませんでしたか?なぜなら、paintComponentはrepaint()を呼び出すたびに多くのシェイプを描画する必要があるからです。 – BBB
ありがとうございます、あなたもそうです:)また、元に戻す機能を書く必要があります。リストもその場合に役立ちます:)助けてくれてありがとう – BBB