Java Reference
In-Depth Information
// Make the path available
Shape getShape() {
return p;
}
private Point2D.Float start; // Start point for star
private GeneralPath p; // Star path
}
We can draw stars on an applet:
import javax.swing.JApplet;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class StarApplet extends JApplet {
// Initialize the applet
public void init() {
getContentPane().add(pane); // BorderLayout.CENTER is default position
}
// Class defining a pane on which to draw
class StarPane extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
Star star = new Star(0,0); // Create a star
float delta = 60f; // Increment between stars
float starty = 0f; // Starting y position
// Draw 3 rows of 4 stars
for(int yCount = 0; yCount < 3; yCount++) {
starty += delta; // Increment row position
float startx = 0f; // Start x position in a row
// Draw a row of 4 stars
for(int xCount = 0; xCount<4; xCount++) {
g2D.draw(star.atLocation(startx += delta, starty));
}
}
}
}
StarPane pane = new StarPane(); // Pane containing stars
}
The HTML file for this applet could contain:
<applet code="StarApplet.class" width=360 height=240></applet>
Search WWH ::




Custom Search