Pentagram: Difference between revisions

355 bytes removed ,  7 years ago
→‎{{header|Java}}: more compact
(no longer draft)
(→‎{{header|Java}}: more compact)
Line 57:
 
final double degrees144 = Math.toRadians(144);
 
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Pentagram");
f.setResizable(false);
f.add(new Pentagram(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
 
public Pentagram() {
Line 76 ⟶ 63:
}
 
private void drawPentagram(Graphics2D g, int len, int x1x, int y1) {y,
Color fill, Color stroke) {
double angle = 0;
 
Path2D p = new Path2D.Float();
p.lineTomoveTo(x, y);
 
for (int i = 0; i < 5; i++) {
int x2 = x1x + (int) (Math.cos(angle) * len);
int y2 = y1y + (int) (Math.sin(-angle) * len);
gp.drawLinelineTo(x1, y1, x2, y2);
x1x = x2;
y1y = y2;
angle -= degrees144;
}
p.closePath();
}
 
g.setColor(Color.darkGrayfill);
private void fillPentagram(Graphics2D g, int len, int x, int y) {
double angle = 0g.fill(p);
 
Path2Dg.Float p = new Path2D.FloatsetColor(stroke);
pg.moveTodraw(x, yp);
 
for (int i = 0; i < 10; i++) {
 
x = x + (int) (Math.cos(angle) * len);
y = y + (int) (Math.sin(-angle) * len);
p.lineTo(x, y);
 
angle += (i % 2 == 0 ? 0.5 : -1) * degrees144;
}
 
p.closePath();
g.fill(p);
}
 
Line 118 ⟶ 97:
g.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, 0));
 
g.setColordrawPentagram(g, 500, 70, 250, new Color(0x6495ED), Color.darkGray); // CornflowerBlue
}
fillPentagram(g, 190, 72, 250);
 
public static void main(String[] args) {
g.setColor(Color.darkGray);
drawPentagramSwingUtilities.invokeLater(g,() 500,-> 70, 250);{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Pentagram");
f.setResizable(false);
f.add(new Pentagram(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}</lang>
Anonymous user