我有以下代码来绘制一个工作正常的矩形:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.setPaint(new Color(128, 0, 0));
Rectangle2D.Double s = new Rectangle2D.Double(20.0, 20.0, 100.0, 50.0);
g2.draw(s);
}
用我的代码替换后
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.setPaint(new Color(128, 0, 0));
Shape[] shapeList = new Shape[3];
shapeList[0] = new Rectangle2D.Double(20.0,20.0,40.0,70.0);
shapeList[1] = new Ellipse2D.Double(20.0,20.0,40.0,70.0);
int[] x = { 10, 50, 70 };
int[] y = { 20, 70, 20 };
shapeList[2] = new Polygon(x,y,3);
// now update the display
for (int i=0; i<4; i++){
g2.draw(shapeList[i]);
}
}
我收到语法错误,告诉我必须插入&#34;}&#34;在这一行
g2.draw(shapeList[i]);
我还没弄明白为什么,但我想我在某处遗漏了什么。
这是我的全部代码:
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class FrameDrawings extends JPanel {
public FrameDrawings() {
setBackground(Color.white);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.setPaint(new Color(128, 0, 0));
Rectangle2D.Double s = new Rectangle2D.Double(20.0, 20.0, 100.0, 50.0);
Shape[] shapeList = new Shape[3];
shapeList[0] = new Rectangle2D.Double(20.0,20.0,40.0,70.0);
shapeList[1] = new Ellipse2D.Double(20.0,20.0,40.0,70.0);
int[] x = { 10, 50, 70 };
int[] y = { 20, 70, 20 };
shapeList[2] = new Polygon(x,y,3);
// now update the display
for (int i=0; i<4; i++){
g2.draw(shapeList[i]);
}
}
}