//:::::::::::::::::::::::::::::::::::::::::::: /** @author Casey Bowman * @version 1.1 * @date Mon Mar 09 15:15:38 EDT 2015 * @see (License) MIT style license */ import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.BasicStroke; import java.awt.Toolkit; import java.awt.Dimension; //::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** A simple java swing application to display a rectangle * in a window. * * @const dim the dimensions of the screen * @const frameWidth the width of the JFrame * @const frameHeight the height of the JFrame * @const frameX the x-coordinate of the top left corner of the JFrame * @const frameY the y-coordinate of the top left corner of the JFrame * @const rectWidth the width of the rectangle * @const rectHeight the height of the rectangle * @const rectX the x-coordinate of the top left corner of the rectangle * @const rectY the y-coordinate of the top left corner of the rectangle * @const pane a JPanel to hold the Graphics */ public class SwingRectangle extends JFrame { private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); private int frameWidth = 600; private int frameHeight = 600; private int frameX = dim.width / 2 - frameWidth / 2; private int frameY = dim.height / 2 - frameHeight / 2; private int rectWidth = 50; private int rectHeight = 50; private int rectX = 100; private int rectY = 100; private JPanel panel = makePanel (); //::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** Constructor for class swingRectangle. It sets the * characteristics for the JFrame: * * 1. sets the location of the frame. * 2. sets the default close operation. The program stops if the window is closed. * 3. sets the size of the frame. * 4. sets the background color. * 5. adds the panel to the frame's content pane. * 6. makes it visible. */ public SwingRectangle () { setLocation (frameX, frameY); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setSize (frameWidth, frameHeight); setBackground (Color.decode ("0x33CC99")); getContentPane ().add (panel); setVisible (true); } // constructor //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** This method creates a JPanel, overrides the panel's paint * method with a custom paint method, and then returns * the panel. */ public JPanel makePanel () { JPanel p = new JPanel () { //:::::::::::::::::::::::::::::::::::::::::::::::::: /** Override Override JPanel's paint method with * custom graphics. * * @param g Graphics object that does the drawing. */ @Override public void paint (Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor (Color.decode ("0xEE3344")); g2d.fillRoundRect (rectX, rectY, rectWidth, rectHeight, 10, 10); g2d.setColor (Color.BLACK); g2d.setStroke (new BasicStroke (3)); g2d.drawRoundRect (rectX, rectY, rectWidth, rectHeight, 10, 10); } // paint }; return p; } // makePanel //::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /** the main method creates an instance of class * swingRectangle. the internal code of swingRectangle * create the JFrame and draw the graphics. */ public static void main (String[] args) { SwingRectangle sr = new SwingRectangle (); } // main } // SwingRectangle