//file Primitives.java //showing off primitives //author: Harriet Fell 1/2007 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Primitives extends JComponent { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; int wd = getSize().width; int ht = getSize().height; //System.out.println(wd); //System.out.println(ht); // a filled red rectangle g2.setPaint(Color.red); g2.fillRect(10, 50, 100, 200); // a filled yellow oval g2.setPaint(Color.yellow); g2.fillOval(200, 30, 200, 80);// Outline a green oval // an outlined green oval g2.setPaint(Color.green); g2.setStroke(new BasicStroke(4)); g2.drawOval(200, 150, 200, 80); // a magenta line g2.setPaint(Color.magenta); g2.setStroke(new BasicStroke(12)); g2.drawLine(wd/2, ht/3, wd, 0); // 1000 black points // Use drawLine(x, y, x, y) to set a Pixel Random rnums = new Random(); g2.setStroke(new BasicStroke(1)); g2.setPaint(Color.black); for (int pnum = 0; pnum < 1000; pnum++){ int x = rnums.nextInt(wd/4); int y = rnums.nextInt(ht/4); g2.drawLine(10 + x, 300 + y, 10 + x, 300 + y); } // some text g2.setFont(new Font("Times New Roman", Font.ITALIC, Math.min(wd/20, ht/20))); g2.setPaint(new GradientPaint(0, 0, Color.red, wd, ht, Color.yellow, false)); g2.drawString("CSU540 - Computer Graphics", wd/3, 0 + ht/2); g2.setStroke(new BasicStroke(5)); // 5 pixels thick //RoundRectangle2D.Double(double x, double y, double w, double h, double arcw, double arch) g2.setPaint(Color.black); Shape rr = new RoundRectangle2D.Double(350,450,400,300,70,50); g2.draw(rr); //Ellipse2D.Double(double x, double y, double w, double h) g2.setPaint(Color.yellow); Shape el = new Ellipse2D.Double(350,450,400,300); g2.fill(el); //Arc2D.Double(double x, double y, double w, double h, double start, double extent, int type) g2.setPaint(Color.red); Shape ar1 = new Arc2D.Double(350,450,400,300,0,45,Arc2D.PIE); g2.draw(ar1); g2.setPaint(Color.green); Shape ar2 = new Arc2D.Double(350,450,400,300,90,120,Arc2D.CHORD); g2.draw(ar2); g2.setPaint(Color.blue); Shape ar3 = new Arc2D.Double(350,450,400,300,270,60,Arc2D.OPEN); g2.draw(ar3); } public static void main(String[] args){ JFrame f = new JFrame("Primitives"); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(new Primitives(), BorderLayout.CENTER); f.setSize(800, 822); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); } }