/** * Created by sharanya on 11/30/15. */ import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; import java.awt.Polygon; import java.awt.Color; class MyCanvas extends JComponent { //Here, we just write the comands to draw a bunch of stuff. public void paint(Graphics g) { int radius = 40; int centerX = 50; int centerY = 100; int angle = 30; //Draw a Pacman int dx = (int) (radius * Math.cos(angle * Math.PI / 180)); int dy = (int) (radius * Math.sin(angle * Math.PI / 180)); g.setColor(new Color(250, 150, 85)); g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, angle, 360 - 2 * angle); // Draw a Pentagon Polygon p = new Polygon(); centerX = 150; for (int i = 0; i < 5; i++) p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)), (int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5))); g.setColor(Color.GREEN); g.fillPolygon(p); // Draw a Spiral p = new Polygon(); centerX = 250; for (int i = 0; i < 360; i++) { double t = i / 360.0; p.addPoint((int) (centerX + radius * t * Math.cos(8 * t * Math.PI)), (int) (centerY + radius * t * Math.sin(8 * t * Math.PI))); } g.setColor(Color.RED); g.fillPolygon(p); } } public class DrawStuff { public static void main(String[] a) { //Create a frame JFrame window = new JFrame(); //Make it so that the program dies when we click on the close button window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set the boundaries of the window window.setBounds(30, 30, 600, 600); //Attach the Canvas we just described to the window window.getContentPane().add(new MyCanvas()); //make the window visible window.setVisible(true); } }