Javaプロジェクトのグラフィックボード(三)
前のバージョンのグラフィックボード(2)では、コードの再構築を行い、1つのクラスをいくつかのクラスに再構成し、各クラスがモジュールを担当しています.このバージョンでは、主にいくつかのバグと不足を解決しています.解決すべき問題 フォームサイズを変更すると、元のフォームより大きい部分に絵が表示されていないことがわかり、フォームを変更すると が表示されます.マウスでドラッグしてグラフィックを描くときに表示されます(前のバージョンではマウスが解放されたときにのみ表示されます) .ソリューション 解決グラフィックフォームが大きくなったときにグラフィックが表示されない: なぜ表示されないのか:フォームがサイズを変更したときに、大きくなったパネルの一部が、まだリフレッシュされておらず、まだ呼び出されていないため、表示されず、再びフォームのサイズを変更したときに が表示されます.
ソリューション: ここでは、再描画、paint(Graphics g)の書き換えが必要です.再描画である以上、以前に描いた図形を保存する容器が必要です.このバージョンではLinkedListを使用し、ブラシは1画ごとに 保存されています.グラフィックを保存します.グラフィックを保存するいくつかの属性に相当します.属性はあります.グラフィックで使用される座標です.例えば、直線は始点座標と終点座標です.各グラフィックにはブラシの色、幅があります.これらの属性を保存しやすいように、各グラフィックの属性には共通点があるので、抽象的な親をパッケージします.他の具体的なグラフィッククラスを書くには継承するだけで です.
絵が一目瞭然になるように少し効果を加え、ドラッグした時に絵が表示されます. ソリューション:(口が不器用で、説明が分かりにくいのでコードを直接見る)
// , , , list , repaint() list ,
list.add(new ShapeLine(xStart, yStart, xEnd, yEnd, graphics.getColor(), graphics.getStroke()));// list
repaint();//repaint() paint() , paint() ,
// , list ,
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < list.size(); i++) {
list.get(i).repaint((Graphics2D) g);// , list
}
if (shape != null) {
shape.repaint((Graphics2D) g);//
}
}
//
public abstract class Shape {
public int x, y, x1, y1, arcWidth, arcHeight;//
public Color currentColor;//
public Stroke strock;//
public Shape(int x, int y, int x1, int y1, int arcWidth, int arcHeight, Color currentColor, Stroke strock) {
//
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.arcWidth = arcWidth;// 。
this.arcHeight = arcHeight;// 。
this.currentColor = currentColor;
this.strock = strock;
}
public Shape(int x, int y, int x1, int y1, Color currentColor, Stroke strock) {
// , ,
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.currentColor = currentColor;
this.strock = strock;
}
public abstract void repaint(Graphics2D g);// ,
}
// , Shape
public class ShapeLine extends Shape {
public ShapeLine(int x, int y, int x1, int y1, Color currentColor, Stroke strock) {
super(x, y, x1, y1, currentColor, strock);
}
//
@Override
public void repaint(Graphics2D g) {
g.setColor(currentColor);
g.setStroke(strock);
g.drawLine(x, y, x1, y1);
}
}
// , , ,
// : , , ,, , list , ,
//shape Shape ,
shape = new ShapeLine(xStart, yStart, e.getX(), e.getY(),graphics.getColor(),graphics.getStroke());
repaint();// , paint()
// paint() if
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < list.size(); i++) {
list.get(i).repaint((Graphics2D) g);// , list
}
if (shape != null) {
shape.repaint((Graphics2D) g);//
}
}