import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class BadGraphics {

   public static void main(String[] args) {
   
      drawRect(10,10,480,430);
      setColor(Color.BLUE);
      fillTriangle(30,420,470,420,250,30);
      setColor(Color.RED);
      setFontSize(30);
      drawString("Blue Triangle",150,490);
   
   }

   //--------- definitions of subroutines for use in the main routine -----------
   //--------------- (Read the first line, but not the insides!) ----------------
   
   private static void setColor(Color newDrawingColor) {
      color = newDrawingColor;
   }
   
   private static void setFontSize(int newSize) {
      font = new Font("Serif",Font.PLAIN,newSize);
   }

   private static void drawLine(int x1, int y1, int x2, int y2) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.drawLine(x1,y1,x2,y2);
      g.dispose();
      panel.repaint();
   }
   
   private static void drawString(String string, int x, int y) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.setFont(font);
      g.drawString(string,x,y);
      g.dispose();
      panel.repaint();
   }
   
   private static void drawRect(int x, int y, int width, int height) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.drawRect(x,y,width,height);
      g.dispose();
      panel.repaint();
  }

   private static void fillRect(int x, int y, int width, int height) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.fillRect(x,y,width,height);
      g.dispose();
      panel.repaint();
   }

   private static void drawOval(int x, int y, int width, int height) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.drawOval(x,y,width,height);
      g.dispose();
      panel.repaint();
   }

   private static void fillOval(int x, int y, int width, int height) {
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.fillOval(x,y,width,height);
      g.dispose();
      panel.repaint();
   }
   
   private static void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
      Polygon p = new Polygon();
      p.addPoint(x1,y1);
      p.addPoint(x2,y2);
      p.addPoint(x3,y3);
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.drawPolygon(p);
      g.dispose();
      panel.repaint();
   }

   private static void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
      Polygon p = new Polygon();
      p.addPoint(x1,y1);
      p.addPoint(x2,y2);
      p.addPoint(x3,y3);
      Graphics g = canvas.getGraphics();
      g.setColor(color);
      g.fillPolygon(p);
      g.dispose();
      panel.repaint();
   }

   // ------------------ private implementation details ---------------------
   // --------------------- (don't try to read this) ------------------------

   private static JPanel panel;  // Variable declarations.
   private static Image canvas;
   private static Font font;
   private static Color color;
   
      // The next part is a weird, little-used Java thing called a 
      // "static initializer."  It is run automatically when the 
      // program starts, before the main routine is exectued.
   
   static {
      JFrame graphicsWin = new JFrame("BadGraphics");
      panel = new JPanel() {
         public void paintComponent(Graphics g) {
            g.drawImage(canvas,0,0,null);
         }
      };
      panel.setPreferredSize(new Dimension(500,500));
      panel.setBackground(Color.WHITE);
      canvas = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
      Graphics g = canvas.getGraphics();
      g.setColor(Color.WHITE);
      g.fillRect(0,0,500,500);
      g.dispose();
      font = new Font("Serif",Font.PLAIN,12);
      color = Color.BLACK;
      graphicsWin.setContentPane(panel);
      graphicsWin.setLocation(80,50);
      graphicsWin.pack();
      graphicsWin.setResizable(false);
      graphicsWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      graphicsWin.setVisible(true);
   }
      
}

