// BasicGraphicsDemo1.java // A demo of some simple graphics operations, starting from BasicGraphics.java import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class BasicGraphicsDemo1 extends JComponent { public static void main(String[] args) { JComponent myGraphics = new BasicGraphicsDemo1(); // Your class name goes here! launch(myGraphics, 500, 300); // Set the initial dimensions here! } public void paint(Graphics page) { int width = getWidth(); int height = getHeight(); // filled blue rectangle as large as the window page.setColor(Color.blue); page.fillRect(0,0,width,height); // filled yellow oval inscribed in the blue rectangle page.setColor(Color.yellow); page.fillOval(0,0,width,height); // unfilled (drawn) black rectangle on the left page.setColor(Color.black); page.drawRect(10,10,width/2-20,height-20); // unfilled thick-lined black ovals on the right ((Graphics2D)page).setStroke(new BasicStroke(5)); // 5 pixels wide page.drawOval(width/2+10,10,width/2-20,height/2-20); page.drawOval(width/2+10,height/2+10,width/2-20,height/2-20); // unfilled thin-lined red triangle on the right ((Graphics2D)page).setStroke(new BasicStroke(1)); // set back to 1 pixels wide page.setColor(Color.red); Polygon p1 = new Polygon(); p1.addPoint(3*width/4,height/3); // addPoint(x,y) p1.addPoint(width/2+40,2*height/3); p1.addPoint(width-40,2*height/3); page.drawPolygon(p1); // filled green pentagon in the middle page.setColor(Color.green); int pWidth = width/5; int pHeight = height/10; int pLeft = width/2 - pWidth/2; int pTop = height/2 - pHeight/2; Polygon p2 = new Polygon(); p2.addPoint(pLeft , pTop); p2.addPoint(pLeft+pWidth/3 , pTop+pHeight); p2.addPoint(pLeft+2*pWidth/3, pTop+pHeight); p2.addPoint(pLeft+pWidth , pTop); page.fillPolygon(p2); // custom-colored red rectangles in the middle page.setColor(new Color(100, 0, 0)); // RGB (Red, Green, Blue) page.fillRect(width/2-10, 10, 30, 30); page.setColor(new Color(200, 25, 25)); page.fillRect(width/2-10, 50, 30, 30); page.setColor(new Color(255, 50, 50)); page.fillRect(width/2-10, 90, 30, 30); // using trig to place a small blue circle at 70 degrees // in the first quadrant of an orange circle int radius = 50; page.setColor(Color.orange); // oval will be centered at (100,100) page.fillOval(100-radius,100-radius,2*radius,2*radius); // here we use 70 degrees int blueCenterX = 100 + (int)(radius*Math.cos(Math.PI*70/180)); int blueCenterY = 100 - (int)(radius*Math.sin(Math.PI*70/180)); int blueRadius = radius/10; page.setColor(Color.blue); page.fillOval(blueCenterX-blueRadius,blueCenterY-blueRadius, 2*blueRadius,2*blueRadius); // draw a line connecting the centers of the orange and blue circles page.drawLine(100,100,blueCenterX,blueCenterY); } ////////////////////////////////////////////////////////////////////////////// ///////////////// END OF YOUR CODE ///////////////////// ///////////////// (you may ignore all the code below!!! ///////////////////// ////////////////////////////////////////////////////////////////////////////// public static void launch(JComponent jc, int width, int height) { JFrame frame = new JFrame(jc.getClass().getName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(jc); frame.setContentPane(cp); frame.setSize(new Dimension(width,height)); frame.setVisible(true); } }