Java数字图像处理基础-------Java Swing简单使用,图形绘画—画五角星
一:简介
要画出五角星出来,我们只需要在面板上产生5个点,然后把这5个点进行连接就可实现;
二:代码演示
import javax.swing.*;
import java.awt.*;
public class FiveStar extends JPanel {
JFrame jframe = new JFrame("五角星");
int x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
public FiveStar() {
jframe.setSize(500, 500);
this.setSize(500, 500);
jframe.add(this);
jframe.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
x1 = this.getWidth() / 2;
x2 = this.getWidth() / 8;
x3 = this.getWidth() - x2;
x4 = this.getWidth() / 4;
x5 = 3 * this.getWidth() / 4;
y1 = 50;
y2 = this.getHeight() / 3;
y3 = y2;
y4 = this.getHeight() - 40;
y5 = y4;
int xPoints[] = {x2, x3, x4, x1, x5, x2};
int yPoints[] = {y2, y3, y4, y1, y5, y2};
g.setColor(Color.RED);
for (int i = 1; i < xPoints.length; i++) {
g.drawLine(xPoints[i-1], yPoints[i-1], xPoints[i], yPoints[i]);
}
}
public static void main(String[] args) {
new FiveStar();
}
}
三:结果演示
案例仿照于:《Java数字图像处理基础编程技巧与应用实践》
|