Snippet
import javax.swing.*; //Import these
import java.awt.*;
public class DrawShapes extends JApplet //Public class make sure it extends JApplet
{
int xPoints[] = {10,40,40,10,10}; //Sets x coordinates to draw to/from
int yPoints[] = {10,10,40,40,10}; //Sets y coordinates to draw to/from
public void init() //Initialize
{
setBackground (Color. black); //Make window background black
setSize(500,500); //Set window size
}
public void paint (Graphics Obj ) //paint function (essential for graphics)
{
Obj. setColor(Color. green); //Sets the color to green
Obj.fillPolygon(xPoints,yPoints,xPoints.length); //Uses the x,y coordinates declared earlier to draw a filled square
for(int i = 0; i < xPoints.length;i++) //for loop to add to x values to move it across screen
xPoints[i] += 80;
Obj.drawPolyline(xPoints,yPoints,xPoints.length); //draws an empty square at the new x values and same y values
}
}
Copy & Paste
|