JAVA APPLETS ARE NOT PART OF THE AP SUBSET… But they are pretty cool and worth writing at least one.
A Java applet is a basically just a Java program that a browser enabled with Java can run. An applet is typically embedded inside a web page.
Assignment:
Write a Java Applet which draws a spiral, much like the one below. Your spiral will be created by drawing a series of lines. You must use a for loop -or- while loop to create the drawing.
To get started…
1) Open BlueJ
2) Create a new class of type APPLET, and cut and paste the following code in.
3) To run the applet, right click and select RUN APPLET.
Note: Java's coordinate system has the point (0,0) in the top-left corner. Positive x values are to the right and positive y values are down. All pixel values are integers; there are no partial or fractional pixels.
/**
* >>>>> JAVA APPLETS ARE NOT PART OF THE AP SUBSET <<<<<
* But they are pretty cool and worth writing at least one.
*
* Assignment: Create a Java Applet which draws a spiral.
*/
import javax.swing.JApplet;
import java.awt.*; // import Graphics, Graphics2D, and Rectangle
public class Spiral extends JApplet
{
public void paint (Graphics g)
{
g.setColor(Color.black);
/**
* Here is an example of how to draw a line from (300,200) to (400,300)
*
* You will need to replace this code with a while-loop or for-loop
* which draws a spiral. Think about drawing lines which increase in
* length each time you draw a new line.
*/
int x = 300;
int y = 200;
int i = 100;
g.drawLine(x,y,x+i,y+i); //(from x, from y, to x+i, to y+i)
} // end of paint
}
After you finish the basic spiral above try to produce another spiral of you own creation. Below is an example of a triangular spiral. Your spiral does not have to look like the one below. Make up your own!
Dostları ilə paylaş: |