daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Apr 15th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. /**
  6.  * Class for building a graphical window and drawing on it. Also listens for
  7.  * keyboard events so that this class can be used for interactive games. This
  8.  * class is intended for use in a Java programming class where objects are
  9.  * introduced late. It exports several static methods, which other classes can
  10.  * call to manipulate the window. These methods handle object creation and
  11.  * manipulation, thus hiding it from the other classes. Window is drawn by
  12.  * creating a window (JFrame) and a Paint object (Paint extends JPanel) and
  13.  * setting the Paint object to be the content panel of the window. Paint
  14.  * overrides paintComponent(), which then draws all of the shapes that have been
  15.  * specified via calls to static methods below (e.g., fillRect). Although this
  16.  * class does hide objects, it attempts to make the interface for drawing shapes
  17.  * similar to the actual interface. So the methods are the same (although
  18.  * static) and take the same kinds of arguments for the most part: fillOval,
  19.  * drawOval, fillRect, drawRect, setColor, etc. There are a couple of
  20.  * exceptions, however. setFont takes different arguments. In Java, setFont
  21.  * takes a Font object. In the Paint class below it takes the three arguments to
  22.  * the Font constructor: font class ("serif" or "sanserif"), font type (int,
  23.  * e.g., Font.BOLD), and size (int 16). In addition, for convenience, in Paint
  24.  * there are methods for drawing triangles. With the Graphics class, one must
  25.  * use the drawPolygon or fillPolygon methods.
  26.  *
  27.  */
  28. public class Paint extends JPanel implements KeyListener {
  29.   /** Constant representing the maximum number of objects allowed */
  30.   public static final int MAX_OBJECTS = 1000000;
  31.  
  32.   /** Constant representing no direction (for arrow input) */
  33.   public static final int NONE = -1;
  34.  
  35.   /** Constant representing some other direction (for arrow input) */
  36.   public static final int OTHER = -2;
  37.  
  38.   /** Constant representing the direction up (for arrow input) */
  39.   public static final int UP = 0;
  40.  
  41.   /** Constant representing the direction down (for arrow input) */
  42.   public static final int DOWN = 1;
  43.  
  44.   /** Constant representing the direction left (for arrow input) */
  45.   public static final int LEFT = 2;
  46.  
  47.   /** Constant representing the direction right (for arrow input) */
  48.   public static final int RIGHT = 3;
  49.  
  50.   /*
  51.    * Note: variables below are 'static' so that programmers using this class do
  52.    * not have to know about objects. Instead, they can make static calls to the
  53.    * methods below, which then privately handle object creation and
  54.    * manipulation.
  55.    */
  56.  
  57.   /** Window to draw on */
  58.   private static JFrame window;
  59.  
  60.   /** Content panel of window (a Paint panel) */
  61.   private static Paint content;
  62.  
  63.   /** Array of objects (rectangles, ovals, strings, etc.) */
  64.   private static DrawObject[] drawObjects;
  65.  
  66.   /** Number of drawn objects */
  67.   private static int drawObjectsNum;
  68.  
  69.   /** Current color to paint with */
  70.   private static Color currentColor;
  71.  
  72.   /** Current font to draw strings with */
  73.   private static Font currentFont;
  74.  
  75.   /** A string buffer for holding text entered via keyboard */
  76.   private static StringBuffer keyBuffer;
  77.  
  78.   /**
  79.    * An integer holding the direction pushed for the last arrow (NONE means
  80.    * arrow was not entered)
  81.    */
  82.   private static int arrow = NONE;
  83.  
  84.   /**
  85.    * If true, every drawRect and similar command results in a repaint after the
  86.    * command. If false, repaints do not occur until paint() is called (a
  87.    * one-time repaint) or setAutoRepaint(true) is called.
  88.    */
  89.   private static boolean repaint_ = true;
  90.  
  91.   /**
  92.    * Paint constructor Creates the panel and initializes all variables
  93.    *
  94.    * @param bgColor
  95.    *          the background color.
  96.    */
  97.   public Paint ( Color bgColor ) {
  98.     // set this class to listen for keyboard events
  99.     addKeyListener(this);
  100.     // set the background color
  101.     setBackground(bgColor);
  102.     // copy the color to currentColor
  103.     currentColor =
  104.       new Color(bgColor.getRed(),bgColor.getGreen(),bgColor.getBlue());
  105.     // set the default font
  106.     currentFont = new Font("Dialog",Font.PLAIN,12);
  107.     // initialize the objects array and initialize length
  108.     drawObjects = new DrawObject[MAX_OBJECTS];
  109.     drawObjectsNum = 0;
  110.     // initialize the key buffer
  111.     keyBuffer = new StringBuffer();
  112.   }
  113.  
  114.   /**
  115.    * Override paintComponent and draw panel Draws all the objects (rectangles,
  116.    * ovals, string, etc.) on the window.
  117.    *
  118.    * @param g
  119.    *          a graphics object
  120.    */
  121.   public void paintComponent ( Graphics g ) {
  122.     // call parent paintComponent method - draws background
  123.     super.paintComponent(g);
  124.  
  125.     // draw shapes
  126.     for ( int i = 0 ; i < drawObjectsNum ; i++ )
  127.       drawObjects[i].draw(g);
  128.  
  129.     // request the focus for this panel to listen for keys entered
  130.     requestFocusInWindow();
  131.   }
  132.  
  133.   /**
  134.    * Define the abstract keyReleased method Pushes key events onto the string
  135.    * buffer
  136.    *
  137.    * @param event
  138.    *          the key event object
  139.    */
  140.   public void keyReleased ( KeyEvent event ) {
  141.     if ( event.getKeyCode() == KeyEvent.VK_UP ) arrow = UP;
  142.     else if ( event.getKeyCode() == KeyEvent.VK_DOWN ) arrow = DOWN;
  143.     else if ( event.getKeyCode() == KeyEvent.VK_LEFT ) arrow = LEFT;
  144.     else if ( event.getKeyCode() == KeyEvent.VK_RIGHT ) arrow = RIGHT;
  145.     else {
  146.       arrow = OTHER;
  147.       keyBuffer.append(event.getKeyChar());
  148.     }
  149.     notifyKeypress();
  150.   }
  151.  
  152.   private synchronized void notifyKeypress () {
  153.     notifyAll();
  154.   }
  155.  
  156.   /**
  157.    * Define the abstract keyPressed method (not used)
  158.    *
  159.    * @param event
  160.    *          the key event object
  161.    */
  162.   public void keyPressed ( KeyEvent event ) {}
  163.  
  164.   /**
  165.    * Define the abstract keyTyped method (not used)
  166.    *
  167.    * @param event
  168.    *          the key event object
  169.    */
  170.   public void keyTyped ( KeyEvent event ) {}
  171.  
  172.   /**
  173.    * Pop the characters that were typed from the buffer. Input is buffered until
  174.    * return is pressed.
  175.    *
  176.    * @return string containing characters that were typed
  177.    */
  178.   private synchronized String popFromBuffer () {
  179.     //      System.out.println("pop from buffer: " + keyBuffer.length());
  180.     // return line of text stopping at a newline
  181.     if ( keyBuffer.length() != 0 ) {
  182.       String line = "";
  183.       int index = keyBuffer.indexOf("\n");
  184.       if ( index < 0 ) {
  185.     return ""; // index = keyBuffer.length();
  186.       }
  187.       line = keyBuffer.substring(0,index);
  188.       keyBuffer = keyBuffer.delete(0,index + 1); // delete includes \n
  189.       return line;
  190.     } else return "";
  191.   }
  192.  
  193.   /**
  194.    * Blocks until some input is given via keyboard and returns this input
  195.    *
  196.    * @return input entered via the keyboard
  197.    */
  198.   private synchronized String waitForText () {
  199.     // line to return
  200.     String line = "";
  201.  
  202.     // loop infinitely until we get a line of text
  203.     while ( true ) {
  204.       line = content.popFromBuffer();
  205.       if ( line.equals("") ) {
  206.     try {
  207.       wait();
  208.     } catch ( InterruptedException ignored ) {}
  209.       } else break;
  210.     }
  211.  
  212.     // return line
  213.     return line;
  214.   }
  215.  
  216.   /**
  217.    * Blocks until a character is entered via keyboard and returns this input
  218.    *
  219.    * @return character entered via the keyboard
  220.    */
  221.   private synchronized char waitForChar () {
  222.     // wait until there is a character
  223.     while ( true ) {
  224.       if ( keyBuffer.length() == 0 ) {
  225.     try {
  226.       wait();
  227.     } catch ( InterruptedException ignored ) {}
  228.       } else {
  229.     break;
  230.       }
  231.     }
  232.     char ch = keyBuffer.charAt(0);
  233.     keyBuffer.delete(0,1);
  234.     return ch;
  235.   }
  236.  
  237.   /**
  238.    * Get a line of typed text Blocks until it gets some input
  239.    *
  240.    * @return line of typed text
  241.    */
  242.   public static String getln () {
  243.     String line = content.waitForText();
  244.     arrow = NONE; // ignore any arrows pressed while waiting
  245.     return line;
  246.   }
  247.  
  248.   /**
  249.    * Get a single characters. Blocks until it gets some input.
  250.    *
  251.    * @return character typed on keyboard
  252.    */
  253.   public static char getChar () {
  254.     char ch = content.waitForChar();
  255.     arrow = NONE; // ignore any arrows pressed while waiting
  256.     return ch;
  257.   }
  258.  
  259.   /**
  260.    * Blocks until an arrow is entered is given via keyboard and returns the
  261.    * direction of the arrow
  262.    *
  263.    * @return input entered via the keyboard
  264.    */
  265.   private synchronized int waitForArrow () {
  266.     int direction;
  267.  
  268.     // loop infinitely until arrow is not NONE
  269.     while ( true ) {
  270.       if ( arrow == NONE ) {
  271.     try {
  272.       wait();
  273.     } catch ( InterruptedException ignored ) {}
  274.       } else break;
  275.     }
  276.  
  277.     direction = arrow;
  278.     arrow = NONE;
  279.  
  280.     // return direction
  281.     return direction;
  282.   }
  283.  
  284.   /**
  285.    * Get an arrow key typed Blocks until an arrow key is typed
  286.    *
  287.    * @return direction of arrow as an int (0-up, 1-down, 2-left, 3-right)
  288.    */
  289.   public static int getArrow () {
  290.     return content.waitForArrow();
  291.   }
  292.  
  293.   /**
  294.    * Build a new window
  295.    *
  296.    * @param title
  297.    *          the title of the window
  298.    * @param x
  299.    *          starting x coordinate of upper lefthand corner of the window on
  300.    *          the desktop (in pixels)
  301.    * @param y
  302.    *          starting y coordinate of upper lefthand corner of the window on
  303.    *          the desktop (in pixels)
  304.    * @param width
  305.    *          width of the drawing area (in pixels)
  306.    * @param height
  307.    *          height of the drawing area (in pixels)
  308.    * @param bgColor
  309.    *          background color of the window
  310.    */
  311.   public static void buildWindow ( String title, int x, int y, int width,
  312.                    int height, Color bgColor ) {
  313.     // if window was already created then hide it until it's redrawn
  314.     if ( window != null ) window.setVisible(false);
  315.  
  316.     // [ssb] made changes to window sizing so that the size of the drawing area
  317.     // is width by height, to avoid having to account for the windoww title bar
  318.  
  319.     // create new window and display it
  320.     window = new JFrame(title);
  321.     window.setLocation(x,y);
  322.     // window.setSize(width,height); // ssb
  323.     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  324.     content = new Paint(bgColor);
  325.     content.setPreferredSize(new Dimension(width,height)); // ssb
  326.     content.setMinimumSize(new Dimension(width,height)); // ssb
  327.     content.setSize(width,height); // ssb
  328.     window.setContentPane(content);
  329.     window.pack(); // ssb
  330.     window.setVisible(true);
  331.   }
  332.  
  333.   /**
  334.    * Add an object to the window
  335.    *
  336.    * @param o
  337.    *          object to draw (e.g., rectangle)
  338.    */
  339.   private static void addObject ( DrawObject o ) {
  340.     // check if more than MAX_OBJECTS and if so throw an exception
  341.     // (usually happens when student has an infinite loop)
  342.     if ( drawObjectsNum >= MAX_OBJECTS )
  343.       throw new RuntimeException("Can create at most " + MAX_OBJECTS
  344.                  + " objects (shapes, strings, etc.) with Paint");
  345.  
  346.     // check if drawObjects array has been initialized, if not then
  347.     // buildWindow was not called
  348.     if ( drawObjects == null )
  349.       throw new RuntimeException("Must call Paint.buildWindow() before "
  350.                  + "drawing any objects (shapes, strings, etc.) " + "with Paint");
  351.  
  352.     // otherwise add object to array
  353.     drawObjects[drawObjectsNum++] = o;
  354.  
  355.     // repaint window
  356.     if ( repaint_ ) {
  357.       content.repaint();
  358.     }
  359.   }
  360.  
  361.   /**
  362.    * Set the current color
  363.    *
  364.    * @param c
  365.    *          current color
  366.    */
  367.   public static void setColor ( Color c ) {
  368.     currentColor = new Color(c.getRed(),c.getGreen(),c.getBlue());
  369.   }
  370.  
  371.   /**
  372.    * Set the current color
  373.    *
  374.    * @param r
  375.    *          red magnitude (0-255)
  376.    * @param g
  377.    *          green magnitude (0-255)
  378.    * @param b
  379.    *          blue magnitude (0-255)
  380.    */
  381.   public static void setColor ( int r, int g, int b ) {
  382.     currentColor = new Color(r,g,b);
  383.   }
  384.  
  385.   /**
  386.    * Set the current font
  387.    *
  388.    * @param name
  389.    *          name of font ("serif" or "sanserif")
  390.    * @param style
  391.    *          style of font (Font.PLAIN, Font.BOLD, Font.ITALIC, or
  392.    *          Font.BOLD+Font.ITALIC)
  393.    * @param size
  394.    *          size of font (e.g., 16)
  395.    */
  396.   public static void setFont ( String name, int style, int size ) {
  397.     currentFont = new Font(name,style,size);
  398.   }
  399.  
  400.   /**
  401.    * Retrieve the font metrics for the current font.
  402.    *
  403.    * @return the font metrics for the current font
  404.    */
  405.   public static FontMetrics getFontMetrics () {
  406.     return content.getGraphics().getFontMetrics();
  407.   }
  408.  
  409.   /**
  410.    * Abstract class for an object on the window
  411.    *
  412.    */
  413.   private static abstract class DrawObject {
  414.     /** Color of the string (initially set using current color) */
  415.     protected Color c;
  416.  
  417.     // must define draw() method in subclasses...
  418.     abstract public void draw ( Graphics g );
  419.   }
  420.  
  421.   /**
  422.    * Class for a string object
  423.    *
  424.    */
  425.   private static class DrawString extends DrawObject {
  426.     /** String to draw */
  427.     protected String str;
  428.  
  429.     /** x coordinate of lower, lefthand portion of the string */
  430.     protected int x;
  431.  
  432.     /** y coordinate of lower, lefthand portion of the string */
  433.     protected int y;
  434.  
  435.     /** Font of the string (initially set using current font) */
  436.     protected Font f;
  437.  
  438.     /**
  439.      * DrawString constructor
  440.      *
  441.      * @param str
  442.      *          string to draw
  443.      * @param x
  444.      *          x coordinate of lower, lefthand portion of the string
  445.      * @param y
  446.      *          y coordinate of lower, lefthand portion of the string
  447.      */
  448.     public DrawString ( String str, int x, int y ) {
  449.       this.str = str;
  450.       this.x = x;
  451.       this.y = y;
  452.       this.c = currentColor;
  453.       this.f = currentFont;
  454.     }
  455.  
  456.     /**
  457.      * Draw the object on the window
  458.      *
  459.      * @param g
  460.      *          graphics object
  461.      */
  462.     public void draw ( Graphics g ) {
  463.       g.setColor(c);
  464.       g.setFont(f);
  465.       g.drawString(str,x,y);
  466.     }
  467.   }
  468.  
  469.   /**
  470.    * Draw a string object onto the window
  471.    *
  472.    * @param str
  473.    *          string to draw
  474.    * @param x
  475.    *          x coordinate of lower, lefthand portion of the string
  476.    * @param y
  477.    *          y coordinate of lower, lefthand portion of the string
  478.    */
  479.   public static void drawString ( String str, int x, int y ) {
  480.     addObject((DrawObject) new DrawString(str,x,y));
  481.   }
  482.  
  483.   /**
  484.    * Class for a line object
  485.    *
  486.    */
  487.   private static class DrawLine extends DrawObject {
  488.     /** x coordinate of first point */
  489.     protected int x1;
  490.  
  491.     /** y coordinate of first point */
  492.     protected int y1;
  493.  
  494.     /** x coordinate of second point */
  495.     protected int x2;
  496.  
  497.     /** y coordinate of second point */
  498.     protected int y2;
  499.  
  500.     /**
  501.      * DrawLine constructor
  502.      *
  503.      * @param x1
  504.      *          x coordinate of first point
  505.      * @param y1
  506.      *          y coordinate of first point
  507.      * @param x2
  508.      *          x coordinate of second point
  509.      * @param y2
  510.      *          y coordinate of second point
  511.      */
  512.     public DrawLine ( int x1, int y1, int x2, int y2 ) {
  513.       this.x1 = x1;
  514.       this.y1 = y1;
  515.       this.x2 = x2;
  516.       this.y2 = y2;
  517.       this.c = currentColor;
  518.     }
  519.  
  520.     /**
  521.      * Draw the object on the window
  522.      *
  523.      * @param g
  524.      *          graphics object
  525.      */
  526.     public void draw ( Graphics g ) {
  527.       g.setColor(c);
  528.       g.drawLine(x1,y1,x2,y2);
  529.     }
  530.   }
  531.  
  532.   /**
  533.    * Draw a line object onto the window
  534.    *
  535.    * @param x1
  536.    *          x coordinate of first point
  537.    * @param y1
  538.    *          y coordinate of first point
  539.    * @param x2
  540.    *          x coordinate of second point
  541.    * @param y2
  542.    *          y coordinate of second point
  543.    */
  544.   public static void drawLine ( int x1, int y1, int x2, int y2 ) {
  545.     addObject((DrawObject) new DrawLine(x1,y1,x2,y2));
  546.   }
  547.  
  548.   /**
  549.    * Class for a rectangle object
  550.    *
  551.    */
  552.   private static class DrawRect extends DrawObject {
  553.     /** x coordinate of upper, lefthand corner of rectangle */
  554.     protected int x;
  555.  
  556.     /** y coordinate of upper, lefthand corner of rectangle */
  557.     protected int y;
  558.  
  559.     /** Width of rectangle */
  560.     protected int width;
  561.  
  562.     /** Height of rectangle */
  563.     protected int height;
  564.  
  565.     /**
  566.      * DrawRect constructor
  567.      *
  568.      * @param x
  569.      *          x coordinate of upper, lefthand corner of rectangle
  570.      * @param y
  571.      *          y coordinate of upper, lefthand corner of rectangle
  572.      * @param width
  573.      *          Width of rectangle
  574.      * @param height
  575.      *          Height of rectangle
  576.      */
  577.     public DrawRect ( int x, int y, int width, int height ) {
  578.       this.x = x;
  579.       this.y = y;
  580.       this.height = height;
  581.       this.width = width;
  582.       this.c = currentColor;
  583.     }
  584.  
  585.     /**
  586.      * Draw the object on the window
  587.      *
  588.      * @param g
  589.      *          graphics object
  590.      */
  591.     public void draw ( Graphics g ) {
  592.       g.setColor(c);
  593.       g.drawRect(x,y,width,height);
  594.     }
  595.   }
  596.  
  597.   /**
  598.    * Draw a rectangle outline to the window
  599.    *
  600.    * @param x
  601.    *          x coordinate of upper, lefthand corner of rectangle
  602.    * @param y
  603.    *          y coordinate of upper, lefthand corner of rectangle
  604.    * @param width
  605.    *          Width of rectangle
  606.    * @param height
  607.    *          Height of rectangle
  608.    */
  609.   public static void drawRect ( int x, int y, int width, int height ) {
  610.     addObject((DrawObject) new DrawRect(x,y,width,height));
  611.   }
  612.  
  613.   /**
  614.    * Abstract class for a filled-in rectangle object
  615.    *
  616.    */
  617.   private static class FillRect extends DrawRect {
  618.     /**
  619.      * FillRect constructor
  620.      *
  621.      * @param x
  622.      *          x coordinate of upper, lefthand corner of rectangle
  623.      * @param y
  624.      *          y coordinate of upper, lefthand corner of rectangle
  625.      * @param width
  626.      *          Width of rectangle
  627.      * @param height
  628.      *          Height of rectangle
  629.      */
  630.     public FillRect ( int x, int y, int width, int height ) {
  631.       super(x,y,width,height);
  632.     }
  633.  
  634.     /**
  635.      * Draw the object on the window
  636.      *
  637.      * @param g
  638.      *          graphics object
  639.      */
  640.     public void draw ( Graphics g ) {
  641.       g.setColor(c);
  642.       g.fillRect(x,y,width,height);
  643.     }
  644.   }
  645.  
  646.   /**
  647.    * Draw a filled-in rectangle to the window
  648.    *
  649.    * @param x
  650.    *          x coordinate of upper, lefthand corner of rectangle
  651.    * @param y
  652.    *          y coordinate of upper, lefthand corner of rectangle
  653.    * @param width
  654.    *          Width of rectangle
  655.    * @param height
  656.    *          Height of rectangle
  657.    */
  658.   public static void fillRect ( int x, int y, int width, int height ) {
  659.     addObject((DrawObject) new FillRect(x,y,width,height));
  660.   }
  661.  
  662.   /**
  663.    * Class for an oval object
  664.    *
  665.    */
  666.   private static class DrawOval extends DrawObject {
  667.     /** x coordinate of upper-lefthand corner of box that bounds oval */
  668.     protected int x;
  669.  
  670.     /** y coordinate of upper-lefthand corner of box that bounds oval */
  671.     protected int y;
  672.  
  673.     /** Width of box that bounds oval */
  674.     protected int width;
  675.  
  676.     /** Height of box that bounds oval */
  677.     protected int height;
  678.  
  679.     /**
  680.      * DrawOval constructor
  681.      *
  682.      * @param x
  683.      *          x coordinate of upper-lefthand corner of box that bounds oval
  684.      * @param y
  685.      *          y coordinate of upper-lefthand corner of box that bounds oval
  686.      * @param width
  687.      *          width of box that bounds oval
  688.      * @param height
  689.      *          height of box that bounds oval
  690.      */
  691.     public DrawOval ( int x, int y, int width, int height ) {
  692.       this.x = x;
  693.       this.y = y;
  694.       this.height = height;
  695.       this.width = width;
  696.       this.c = currentColor;
  697.     }
  698.  
  699.     /**
  700.      * Draw the object on the window
  701.      *
  702.      * @param g
  703.      *          graphics object
  704.      */
  705.     public void draw ( Graphics g ) {
  706.       g.setColor(c);
  707.       g.drawOval(x,y,width,height);
  708.     }
  709.   }
  710.  
  711.   /**
  712.    * Draw an oval outline to the window
  713.    *
  714.    * @param x
  715.    *          x coordinate of upper-lefthand corner of box that bounds oval
  716.    * @param y
  717.    *          y coordinate of upper-lefthand corner of box that bounds oval
  718.    * @param width
  719.    *          width of box that bounds oval
  720.    * @param height
  721.    *          height of box that bounds oval
  722.    */
  723.   public static void drawOval ( int x, int y, int width, int height ) {
  724.     addObject((DrawObject) new DrawOval(x,y,width,height));
  725.   }
  726.  
  727.   /**
  728.    * Class for a filled-in oval object
  729.    *
  730.    */
  731.   private static class FillOval extends DrawOval {
  732.     /**
  733.      * FillOval constructor
  734.      *
  735.      * @param x
  736.      *          x coordinate of upper-lefthand corner of box that bounds oval
  737.      * @param y
  738.      *          y coordinate of upper-lefthand corner of box that bounds oval
  739.      * @param width
  740.      *          width of box that bounds oval
  741.      * @param height
  742.      *          height of box that bounds oval
  743.      */
  744.     public FillOval ( int x, int y, int width, int height ) {
  745.       super(x,y,width,height);
  746.     }
  747.  
  748.     /**
  749.      * Draw the object on the window
  750.      *
  751.      * @param g
  752.      *          graphics object
  753.      */
  754.     public void draw ( Graphics g ) {
  755.       g.setColor(c);
  756.       g.fillOval(x,y,width,height);
  757.     }
  758.   }
  759.  
  760.   /**
  761.    * Draw a filled-in oval to the window
  762.    *
  763.    * @param x
  764.    *          x coordinate of upper-lefthand corner of box that bounds oval
  765.    * @param y
  766.    *          y coordinate of upper-lefthand corner of box that bounds oval
  767.    * @param width
  768.    *          width of box that bounds oval
  769.    * @param height
  770.    *          height of box that bounds oval
  771.    */
  772.   public static void fillOval ( int x, int y, int width, int height ) {
  773.     addObject((DrawObject) new FillOval(x,y,width,height));
  774.   }
  775.  
  776.   /**
  777.    * Class for a rounded rectangle object
  778.    *
  779.    */
  780.   private static class DrawRoundRect extends DrawObject {
  781.     /**
  782.      * x coordinate of upper-lefthand corner of box that bounds rounded
  783.      * rectangle
  784.      */
  785.     protected int x;
  786.  
  787.     /**
  788.      * y coordinate of upper-lefthand corner of box that bounds rounded
  789.      * rectangle
  790.      */
  791.     protected int y;
  792.  
  793.     /** Width of rounded rectangle */
  794.     protected int width;
  795.  
  796.     /** Height of rounded rectangle */
  797.     protected int height;
  798.  
  799.     /** Horizontal diameter of corner arc of elipse */
  800.     protected int xdiam;
  801.  
  802.     /** Vertical diameter of corner arc of elipse */
  803.     protected int ydiam;
  804.  
  805.     /**
  806.      * Constructor DrawRoundRect
  807.      *
  808.      * @param x
  809.      *          x coordinate of upper-lefthand corner of box that bounds rounded
  810.      *          rectangle
  811.      * @param y
  812.      *          y coordinate of upper-lefthand corner of box that bounds rounded
  813.      *          rectangle
  814.      * @param width
  815.      *          width of rounded rectangle
  816.      * @param height
  817.      *          height of rounded rectangle
  818.      * @param xdiam
  819.      *          horizontal diameter of corner arc of elipse
  820.      * @param ydiam
  821.      *          vertical diameter of corner arc of elipse
  822.      */
  823.     public DrawRoundRect ( int x, int y, int width, int height, int xdiam,
  824.                int ydiam ) {
  825.       this.x = x;
  826.       this.y = y;
  827.       this.height = height;
  828.       this.width = width;
  829.       this.xdiam = xdiam;
  830.       this.ydiam = ydiam;
  831.       this.c = currentColor;
  832.     }
  833.  
  834.     /**
  835.      * Draw the object on the window
  836.      *
  837.      * @param g
  838.      *          graphics object
  839.      */
  840.     public void draw ( Graphics g ) {
  841.       g.setColor(c);
  842.       g.drawRoundRect(x,y,width,height,xdiam,ydiam);
  843.     }
  844.   }
  845.  
  846.   /**
  847.    * Draw a rounded rectangle outline to the window
  848.    *
  849.    * @param x
  850.    *          x coordinate of upper-lefthand corner of box that bounds rounded
  851.    *          rectangle
  852.    * @param y
  853.    *          y coordinate of upper-lefthand corner of box that bounds rounded
  854.    *          rectangle
  855.    * @param width
  856.    *          width of rounded rectangle
  857.    * @param height
  858.    *          height of rounded rectangle
  859.    * @param xdiam
  860.    *          horizontal diameter of corner arc of elipse
  861.    * @param ydiam
  862.    *          vertical diameter of corner arc of elipse
  863.    */
  864.   public static void drawRoundRect ( int x, int y, int width, int height,
  865.                      int xdiam, int ydiam ) {
  866.     addObject((DrawObject) new DrawRoundRect(x,y,width,height,xdiam,ydiam));
  867.   }
  868.  
  869.   /**
  870.    * Class for a filled-in rounded rectangle object
  871.    *
  872.    */
  873.   private static class FillRoundRect extends DrawRoundRect {
  874.     /**
  875.      * Constructor FillRoundRect
  876.      *
  877.      * @param x
  878.      *          x coordinate of upper-lefthand corner of box that bounds rounded
  879.      *          rectangle
  880.      * @param y
  881.      *          y coordinate of upper-lefthand corner of box that bounds rounded
  882.      *          rectangle
  883.      * @param width
  884.      *          width of rounded rectangle
  885.      * @param height
  886.      *          height of rounded rectangle
  887.      * @param xdiam
  888.      *          horizontal diameter of corner arc of elipse
  889.      * @param ydiam
  890.      *          vertical diameter of corner arc of elipse
  891.      */
  892.     public FillRoundRect ( int x, int y, int width, int height, int xdiam,
  893.                int ydiam ) {
  894.       super(x,y,width,height,xdiam,ydiam);
  895.     }
  896.  
  897.     /**
  898.      * Draw the object on the window
  899.      *
  900.      * @param g
  901.      *          graphics object
  902.      */
  903.     public void draw ( Graphics g ) {
  904.       g.setColor(c);
  905.       g.fillRoundRect(x,y,width,height,xdiam,ydiam);
  906.     }
  907.   }
  908.  
  909.   /**
  910.    * Draw a filled-in rounded rectangle to the window
  911.    *
  912.    * @param x
  913.    *          x coordinate of upper-lefthand corner of box that bounds rounded
  914.    *          rectangle
  915.    * @param y
  916.    *          y coordinate of upper-lefthand corner of box that bounds rounded
  917.    *          rectangle
  918.    * @param width
  919.    *          width of rounded rectangle
  920.    * @param height
  921.    *          height of rounded rectangle
  922.    * @param xdiam
  923.    *          horizontal diameter of corner arc of elipse
  924.    * @param ydiam
  925.    *          vertical diameter of corner arc of elipse
  926.    */
  927.   public static void fillRoundRect ( int x, int y, int width, int height,
  928.                      int xdiam, int ydiam ) {
  929.     addObject((DrawObject) new FillRoundRect(x,y,width,height,xdiam,ydiam));
  930.   }
  931.  
  932.   /**
  933.    * Class for an arc object
  934.    *
  935.    */
  936.   private static class DrawArc extends DrawObject {
  937.     /**
  938.      * x coordinate of upper-lefthand corner of the box containing the oval,
  939.      * which the arc is contained within
  940.      */
  941.     protected int x;
  942.  
  943.     /**
  944.      * y coordinate of upper-lefthand corner of the box containing the oval,
  945.      * which the arc is contained within
  946.      */
  947.     protected int y;
  948.  
  949.     /** Width of the box containing the oval, which the arc is contained within */
  950.     protected int width;
  951.  
  952.     /** Height of the box containing the oval, which the arc is contained within */
  953.     protected int height;
  954.  
  955.     /** The starting angle of the arc (0 is at 3 o'clock position) */
  956.     protected int startAngle;
  957.  
  958.     /**
  959.      * The degrees to extend the arc from the starting angle (0 is at 3 o'clock
  960.      * position)
  961.      */
  962.     protected int arcAngle;
  963.  
  964.     /**
  965.      * DrawArc constructor
  966.      *
  967.      * @param x
  968.      *          x coordinate of upper-lefthand corner of the box containing the
  969.      *          oval, which the arc is contained within
  970.      * @param y
  971.      *          y coordinate of upper-lefthand corner of the box containing the
  972.      *          oval, which the arc is contained within
  973.      * @param width
  974.      *          width of the box containing the oval, which the arc is contained
  975.      *          within
  976.      * @param height
  977.      *          height of the box containing the oval, which the arc is
  978.      *          contained within
  979.      * @param startAngle
  980.      *          the starting angle of the arc (0 is at 3 o'clock position)
  981.      * @param arcAngle
  982.      *          the degrees to extend the arc from the starting angle (0 is at 3
  983.      *          o'clock position)
  984.      */
  985.     public DrawArc ( int x, int y, int width, int height, int startAngle,
  986.              int arcAngle ) {
  987.       this.x = x;
  988.       this.y = y;
  989.       this.height = height;
  990.       this.width = width;
  991.       this.startAngle = startAngle;
  992.       this.arcAngle = arcAngle;
  993.       this.c = currentColor;
  994.     }
  995.  
  996.     /**
  997.      * Draw the object on the window
  998.      *
  999.      * @param g
  1000.      *          graphics object
  1001.      */
  1002.     public void draw ( Graphics g ) {
  1003.       g.setColor(c);
  1004.       g.drawArc(x,y,width,height,startAngle,arcAngle);
  1005.     }
  1006.   }
  1007.  
  1008.   /**
  1009.    * Draw an arc outline to the window
  1010.    *
  1011.    * @param x
  1012.    *          x coordinate of upper-lefthand corner of the box containing the
  1013.    *          oval, which the arc is contained within
  1014.    * @param y
  1015.    *          y coordinate of upper-lefthand corner of the box containing the
  1016.    *          oval, which the arc is contained within
  1017.    * @param width
  1018.    *          width of the box containing the oval, which the arc is contained
  1019.    *          within
  1020.    * @param height
  1021.    *          height of the box containing the oval, which the arc is contained
  1022.    *          within
  1023.    * @param startAngle
  1024.    *          the starting angle of the arc (0 is at 3 o'clock position)
  1025.    * @param arcAngle
  1026.    *          the degrees to extend the arc from the starting angle (0 is at 3
  1027.    *          o'clock position)
  1028.    */
  1029.   public static void drawArc ( int x, int y, int width, int height,
  1030.                    int startAngle, int arcAngle ) {
  1031.     addObject((DrawObject) new DrawArc(x,y,width,height,startAngle,arcAngle));
  1032.   }
  1033.  
  1034.   /**
  1035.    * Class for a filled-in arc object
  1036.    */
  1037.   private static class FillArc extends DrawArc {
  1038.     /**
  1039.      * FillArc constructor
  1040.      *
  1041.      * @param x
  1042.      *          x coordinate of upper-lefthand corner of the box containing the
  1043.      *          oval, which the arc is contained within
  1044.      * @param y
  1045.      *          y coordinate of upper-lefthand corner of the box containing the
  1046.      *          oval, which the arc is contained within
  1047.      * @param width
  1048.      *          width of the box containing the oval, which the arc is contained
  1049.      *          within
  1050.      * @param height
  1051.      *          height of the box containing the oval, which the arc is
  1052.      *          contained within
  1053.      * @param startAngle
  1054.      *          the starting angle of the arc (0 is at 3 o'clock position)
  1055.      * @param arcAngle
  1056.      *          the degrees to extend the arc from the starting angle (0 is at 3
  1057.      *          o'clock position)
  1058.      */
  1059.     public FillArc ( int x, int y, int width, int height, int startAngle,
  1060.              int arcAngle ) {
  1061.       super(x,y,width,height,startAngle,arcAngle);
  1062.     }
  1063.  
  1064.     /**
  1065.      * Draw the object on the window
  1066.      *
  1067.      * @param g
  1068.      *          graphics object
  1069.      */
  1070.     public void draw ( Graphics g ) {
  1071.       g.setColor(c);
  1072.       g.fillArc(x,y,width,height,startAngle,arcAngle);
  1073.     }
  1074.   }
  1075.  
  1076.   /**
  1077.    * Draw a filled-in arc to the window
  1078.    *
  1079.    * @param x
  1080.    *          x coordinate of upper-lefthand corner of the box containing the
  1081.    *          oval, which the arc is contained within
  1082.    * @param y
  1083.    *          y coordinate of upper-lefthand corner of the box containing the
  1084.    *          oval, which the arc is contained within
  1085.    * @param width
  1086.    *          width of the box containing the oval, which the arc is contained
  1087.    *          within
  1088.    * @param height
  1089.    *          height of the box containing the oval, which the arc is contained
  1090.    *          within
  1091.    * @param startAngle
  1092.    *          the starting angle of the arc (0 is at 3 o'clock position)
  1093.    * @param arcAngle
  1094.    *          the degrees to extend the arc from the starting angle (0 is at 3
  1095.    *          o'clock position)
  1096.    */
  1097.   public static void fillArc ( int x, int y, int width, int height,
  1098.                    int startAngle, int arcAngle ) {
  1099.     addObject((DrawObject) new FillArc(x,y,width,height,startAngle,arcAngle));
  1100.   }
  1101.  
  1102.   /**
  1103.    * Class for a polygon object
  1104.    */
  1105.   private static class DrawPolygon extends DrawObject {
  1106.     /** Array of x coordinates (at least 1 element for each point) */
  1107.     protected int[] xPoints;
  1108.  
  1109.     /** Array of y coordinates (at least 1 element for each point) */
  1110.     protected int[] yPoints;
  1111.  
  1112.     /** Number of points */
  1113.     protected int nPoints;
  1114.  
  1115.     /**
  1116.      * DrawPolygon consructor
  1117.      *
  1118.      * @param xPoints
  1119.      *          array of x coordinates (at least 1 element for each point)
  1120.      * @param yPoints
  1121.      *          array of y coordinates (at least 1 element for each point)
  1122.      * @param nPoints
  1123.      *          number of points
  1124.      */
  1125.     public DrawPolygon ( int[] xPoints, int[] yPoints, int nPoints ) {
  1126.       this.xPoints = xPoints;
  1127.       this.yPoints = yPoints;
  1128.       this.nPoints = nPoints;
  1129.       this.c = currentColor;
  1130.     }
  1131.  
  1132.     /**
  1133.      * Draw the object on the window
  1134.      *
  1135.      * @param g
  1136.      *          graphics object
  1137.      */
  1138.     public void draw ( Graphics g ) {
  1139.       g.setColor(c);
  1140.       g.drawPolygon(xPoints,yPoints,nPoints);
  1141.     }
  1142.   }
  1143.  
  1144.   /**
  1145.    * Draw a polygon outline onto the window
  1146.    *
  1147.    * @param xPoints
  1148.    *          array of x coordinates (at least 1 element for each point)
  1149.    * @param yPoints
  1150.    *          array of y coordinates (at least 1 element for each point)
  1151.    * @param nPoints
  1152.    *          number of points
  1153.    */
  1154.   public static void drawPolygon ( int[] xPoints, int[] yPoints, int nPoints ) {
  1155.     addObject((DrawObject) new DrawPolygon(xPoints,yPoints,nPoints));
  1156.   }
  1157.  
  1158.   /**
  1159.    * Class for a filled-in polygon object
  1160.    *
  1161.    */
  1162.   private static class FillPolygon extends DrawPolygon {
  1163.     /**
  1164.      * FillPolygon consructor
  1165.      *
  1166.      * @param xPoints
  1167.      *          array of x coordinates (at least 1 element for each point)
  1168.      * @param yPoints
  1169.      *          array of y coordinates (at least 1 element for each point)
  1170.      * @param nPoints
  1171.      *          number of points
  1172.      */
  1173.     public FillPolygon ( int[] xPoints, int[] yPoints, int nPoints ) {
  1174.       super(xPoints,yPoints,nPoints);
  1175.     }
  1176.  
  1177.     /**
  1178.      * Draw the object on the window
  1179.      *
  1180.      * @param g
  1181.      *          graphics object
  1182.      */
  1183.     public void draw ( Graphics g ) {
  1184.       g.setColor(c);
  1185.       g.fillPolygon(xPoints,yPoints,nPoints);
  1186.     }
  1187.   }
  1188.  
  1189.   /**
  1190.    * Draw a filled-in polygon onto the window
  1191.    *
  1192.    * @param xPoints
  1193.    *          array of x coordinates (at least 1 element for each point)
  1194.    * @param yPoints
  1195.    *          array of y coordinates (at least 1 element for each point)
  1196.    * @param nPoints
  1197.    *          number of points
  1198.    */
  1199.   public static void fillPolygon ( int[] xPoints, int[] yPoints, int nPoints ) {
  1200.     addObject((DrawObject) new FillPolygon(xPoints,yPoints,nPoints));
  1201.   }
  1202.  
  1203.   /**
  1204.    * Class for a triangle object
  1205.    */
  1206.   private static class DrawTriangle extends DrawObject {
  1207.     /** x coordinate of first point */
  1208.     protected int x1;
  1209.  
  1210.     /** y coordinate of first point */
  1211.     protected int y1;
  1212.  
  1213.     /** x coordinate of second point */
  1214.     protected int x2;
  1215.  
  1216.     /** y coordinate of second point */
  1217.     protected int y2;
  1218.  
  1219.     /** x coordinate of third point */
  1220.     protected int x3;
  1221.  
  1222.     /** y coordinate of third point */
  1223.     protected int y3;
  1224.  
  1225.     /**
  1226.      * DrawTriangle constructor
  1227.      *
  1228.      * @param x1
  1229.      *          x coordinate of first point
  1230.      * @param y1
  1231.      *          y coordinate of first point
  1232.      * @param x2
  1233.      *          x coordinate of second point
  1234.      * @param y2
  1235.      *          y coordinate of second point
  1236.      * @param x3
  1237.      *          x coordinate of third point
  1238.      * @param y3
  1239.      *          y coordinate of third point
  1240.      */
  1241.     public DrawTriangle ( int x1, int y1, int x2, int y2, int x3, int y3 ) {
  1242.       this.x1 = x1;
  1243.       this.y1 = y1;
  1244.       this.x2 = x2;
  1245.       this.y2 = y2;
  1246.       this.x3 = x3;
  1247.       this.y3 = y3;
  1248.       this.c = currentColor;
  1249.     }
  1250.  
  1251.     /**
  1252.      * Draw the object on the window
  1253.      *
  1254.      * @param g
  1255.      *          graphics object
  1256.      */
  1257.     public void draw ( Graphics g ) {
  1258.       g.setColor(c);
  1259.       g.drawPolygon(new int[] { x1, x2, x3 },new int[] { y1, y2, y3 },3);
  1260.     }
  1261.   }
  1262.  
  1263.   /**
  1264.    * Draw a triangle outline onto the window
  1265.    *
  1266.    * @param x1
  1267.    *          x coordinate of first point
  1268.    * @param y1
  1269.    *          y coordinate of first point
  1270.    * @param x2
  1271.    *          x coordinate of second point
  1272.    * @param y2
  1273.    *          y coordinate of second point
  1274.    * @param x3
  1275.    *          x coordinate of third point
  1276.    * @param y3
  1277.    *          y coordinate of third point
  1278.    */
  1279.   public static void drawTriangle ( int x1, int y1, int x2, int y2, int x3,
  1280.                     int y3 ) {
  1281.     addObject((DrawObject) new DrawTriangle(x1,y1,x2,y2,x3,y3));
  1282.   }
  1283.  
  1284.   /**
  1285.    * Class for a filled-in triangle object
  1286.    *
  1287.    */
  1288.   private static class FillTriangle extends DrawTriangle {
  1289.     /**
  1290.      * FillTriangle constructor
  1291.      *
  1292.      * @param x1
  1293.      *          x coordinate of first point
  1294.      * @param y1
  1295.      *          y coordinate of first point
  1296.      * @param x2
  1297.      *          x coordinate of second point
  1298.      * @param y2
  1299.      *          y coordinate of second point
  1300.      * @param x3
  1301.      *          x coordinate of third point
  1302.      * @param y3
  1303.      *          y coordinate of third point
  1304.      */
  1305.     public FillTriangle ( int x1, int y1, int x2, int y2, int x3, int y3 ) {
  1306.       super(x1,y1,x2,y2,x3,y3);
  1307.     }
  1308.  
  1309.     /**
  1310.      * Draw the object on the window
  1311.      *
  1312.      * @param g
  1313.      *          graphics object
  1314.      */
  1315.     public void draw ( Graphics g ) {
  1316.       g.setColor(c);
  1317.       g.fillPolygon(new int[] { x1, x2, x3 },new int[] { y1, y2, y3 },3);
  1318.     }
  1319.   }
  1320.  
  1321.   /**
  1322.    * Draw a filled-in triangle onto the window
  1323.    *
  1324.    * @param x1
  1325.    *          x coordinate of first point
  1326.    * @param y1
  1327.    *          y coordinate of first point
  1328.    * @param x2
  1329.    *          x coordinate of second point
  1330.    * @param y2
  1331.    *          y coordinate of second point
  1332.    * @param x3
  1333.    *          x coordinate of third point
  1334.    * @param y3
  1335.    *          y coordinate of third point
  1336.    */
  1337.   public static void fillTriangle ( int x1, int y1, int x2, int y2, int x3,
  1338.                     int y3 ) {
  1339.     addObject((DrawObject) new FillTriangle(x1,y1,x2,y2,x3,y3));
  1340.   }
  1341.  
  1342.   /**
  1343.    * Clear the drawing window.
  1344.    */
  1345.   public static void clear () {
  1346.     drawObjectsNum = 0;
  1347.   }
  1348.  
  1349.   /**
  1350.    * Set whether or not repaint occurs automatically after a drawRect or similar
  1351.    * drawing command. Triggers a repaint if auto is true.
  1352.    *
  1353.    * @return the previous auto-repaint setting
  1354.    */
  1355.  
  1356.   public static boolean setAutoRepaint ( boolean auto ) {
  1357.     boolean old = repaint_;
  1358.     repaint_ = auto;
  1359.     if ( repaint_ ) {
  1360.       content.repaint();
  1361.     }
  1362.     return old;
  1363.   }
  1364.  
  1365.   /**
  1366.    * Paint the drawing window.
  1367.    */
  1368.   public static void paint () {
  1369.     content.repaint();
  1370.   }
  1371. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top