如果有人能幫助我,我將非常感激。繪製矩形和網格
我的程序有2個類。一個繪製矩形和一個網格,另一個繪製觀察者。 問題是,當我嘗試更改貪婪+矩形的位置時,即在Viewer類中更新X和Y常量時,比例會發生變化。只有當X = 0和Y = 0時,網格才適合。我使用相同的座標數組繪製網格和矩形,但不知何故比例是錯誤的,這讓我感到驚訝。
謝謝。
我的代碼:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.math.BigDecimal;
public class Viewer extends Applet {
public static final double R_WDTH = 12;//rectangle width
public static final double X = 10;//the grid fits in only when X=Y=0
public static final double Y = 10;
public static final int ROWS = 25;
public static final int COLUMNS = 12;
RectangleAndGrid cup = new RectangleAndGrid(X, Y, R_WDTH, ROWS, COLUMNS);
public void paint(Graphics g) {
cup.draw((Graphics2D)g);
}
}
public class RectangleAndGrid {
private double x, y, blockWidth;
private int rows, columns;
private double coordX[][], coordY[][];
private Rectangle2D.Double cupSpace;
public RectangleAndGrid(double cX, double cY, double givenBlockWidth, int r, int c){
x = cX;
y = cY;
blockWidth = givenBlockWidth;
rows = r;
columns = c;
BigDecimal currX = BigDecimal.valueOf(x);
BigDecimal currY = BigDecimal.valueOf(y);
coordX = new double[rows + 1][columns + 1];
coordY = new double[rows + 1][columns + 1];
for(int i = 0; i <= rows; i++){
for(int j = 0; j <= columns; j++){
coordX[i][j] = currX.doubleValue();
coordY[i][j] = currY.doubleValue();
currX = currX.add(BigDecimal.valueOf(blockWidth));
}
currX = BigDecimal.valueOf(x);
currY = currY.add(BigDecimal.valueOf(blockWidth));
}
cupSpace = new Rectangle2D.Double(coordX[0][0], coordY[0][0], coordX[rows][columns], coordY[rows][columns]);
}
public void draw(Graphics2D g2d){
g2d.setColor(Color.CYAN);
g2d.fill(cupSpace);
g2d.setColor(Color.BLACK);
g2d.draw(cupSpace);
Line2D.Double line = new Line2D.Double();
for(int i = 0; i <= rows; i++){
line.setLine(coordX[i][0], coordY[i][0], coordX[i][columns], coordY[i][columns]);
g2d.draw(line);
}
for(int i = 0; i <= columns; i++){
line.setLine(coordX[0][i], coordY[0][i], coordX[rows][i], coordY[rows][i]);
g2d.draw(line);
}
}
}
不要在這個千年使用AWT組件,如'Applet',使用Swing相當於'JApplet'。 –