Afficher une étoile avec java2d

Contenu du snippet

ce code montre comment on peut afficher une étoile avec les classes de Java2D, il vous suffit d'exécuter la classe Star

Source / Exemple :


/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
  • /
package test; /** *
  • @author CHIHAOUI
  • /
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.GeneralPath; import javax.swing.JFrame; import javax.swing.JPanel; public class Star extends JPanel { double points[][] = { { 0, 85 }, { 75, 75 }, { 100, 10 }, { 125, 75 }, { 200, 85 }, { 150, 125 }, { 160, 190 }, { 100, 150 }, { 40, 190 }, { 50, 125 }, { 0, 85 } }; public void paint(Graphics g) { super.paint(g); int h = getHeight(); int w = getWidth(); Graphics2D g2d = (Graphics2D)g; /* g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);*/ //g2d.translate(25, 5); GeneralPath star = new GeneralPath(); star.moveTo(points[0][0], points[0][1]); for (int k = 1; k < points.length; k++) star.lineTo(points[k][0], points[k][1]); //star.closePath(); g2d.fill(star); } public static void main(String[] args) { JFrame frame = new JFrame("Star"); frame.add(new Star()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350, 250); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.