Java… not just good coffee anymore (aka my simple java drawing app)

Ok – so I finally created a wordpress account after being frustrated by facebook and twitter’s limitations one too many times.

I present to you… my code for my final homework assignment, to write a simple swing GUI-based drawing app that makes rectangles or ovals.


/* Hw10_rsb.java
 * COP2551
 * Reference 328486
 * Schuyler Bishop
 * 2010-12-06
 *
 *
 * Write a simple, GUI-based drawing program. The program should have two
 * buttons: one allowing the user to draw a rectangle, the other an oval. The
 * user draws either by pressing the mouse botton, dragging it, and releasing
 * it. The top-left (x, y) coordinate of the rectangle (or enclosing rectangle
 * for the oval) is where the user pressed the mouse button; the bottom-right
 * point is where the user released the button.
 *
 *
 */

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

public class Hw10_rsb extends JFrame {

private Container contents;
 private int xStart, yStart, xEnd, yEnd, height, width, xOrigin, yOrigin;
 private JLabel instruLabel;
 private JButton rect, oval;
 private boolean drawRect, drawOval, overwriteDraw, overwriteChanged;
 private JPanel south;
 private JRadioButton red, green, blue;
 private ButtonGroup colorGroup;
 private Color selectedColor = Color.RED;
 private JCheckBox overwrite;

public Hw10_rsb () {
 super("SelectionDemo");
 overwriteDraw = true;
 overwriteChanged = true; // so it draws the first time
 drawRect = true; //Default
 contents = getContentPane( );
 contents.setLayout(new BorderLayout());
 instruLabel = new JLabel("Click inside the window and drag to make a "
 + "shape, and select a color.");
 rect = new JButton("Rectangle");
 rect.isDefaultButton();
 oval = new JButton("Oval");

red = new JRadioButton( "red", true );
 green = new JRadioButton( "green" );
 blue = new JRadioButton( "blue" );
 colorGroup = new ButtonGroup();
 overwrite = new JCheckBox("Do not reset on draw",true);

colorGroup.add(red);
 colorGroup.add(green);
 colorGroup.add(blue);

south = new JPanel();
 south.add(rect);
 south.add(oval);
 south.add(red);
 south.add(green);
 south.add(blue);
 south.add(overwrite);

contents.add (instruLabel,BorderLayout.NORTH);
 contents.add (south,BorderLayout.SOUTH);

MouseHandler mh = new MouseHandler () ;
 ButtonHandler bh = new ButtonHandler () ;
 RadioButtonHandler rbh = new RadioButtonHandler( );
 CheckBoxHandler cbh = new CheckBoxHandler( );

overwrite.addItemListener(cbh);

contents.addMouseListener(mh);

rect.addActionListener(bh);
 oval.addActionListener(bh);

red.addItemListener( rbh );
 green.addItemListener( rbh );
 blue.addItemListener( rbh );

setSize(650,450);
 setResizable(false);
 setVisible(true);
 }

@Override
 public void paint(Graphics g) {
 if (overwriteDraw == false) {
 super.paint(g);
 }
 if (overwriteChanged == true) {
 super.paint(g);
 overwriteChanged = false;
 }
 g.setColor(selectedColor);
 if (drawRect == true) {
 g.fillRect(this.xOrigin, this.yOrigin, this.width, this.height );
 }
 if (drawOval == true) {
 g.fillOval(this.xOrigin, this.yOrigin, this.width, this.height );
 }
 }

private class CheckBoxHandler implements ItemListener
 {
 public void itemStateChanged( ItemEvent ie )
 {
 if (overwriteDraw == false){
 overwriteDraw = true;
 overwriteChanged = true;
 }
 else if (overwriteDraw == true){
 overwriteDraw = false;
 overwriteChanged = true;
 }
 }
 }

private class RadioButtonHandler implements ItemListener
 {
 public void itemStateChanged( ItemEvent ie )
 {
 if ( ie.getSource( ) == red )
 selectedColor = Color.RED;
 else if ( ie.getSource( ) == green )
 selectedColor = Color.GREEN;
 else if ( ie.getSource( ) == blue )
 selectedColor = Color.BLUE;
 }
 }

private class ButtonHandler implements ActionListener
 {
 public void actionPerformed( ActionEvent ae )
 {
 if ( ae.getSource( ) == rect ) {
 drawRect = true;
 drawOval = false;
 }
 else if ( ae.getSource( ) == oval ) {
 drawOval = true;
 drawRect = false;
 }
 }
 }

private class MouseHandler implements MouseListener {

public void mousePressed(MouseEvent e) {
 xStart = e.getX();
 yStart = e.getY();
 }

public void mouseReleased(MouseEvent e) {
 xEnd = e.getX();
 yEnd = e.getY();
 if (yEnd > yStart) {
 yOrigin = yStart;
 height = yEnd - yStart;
 }
 else {
 yOrigin = yEnd;
 height = yStart - yEnd;
 }
 if (xEnd > xStart) {
 xOrigin = xStart;
 width = xEnd - xStart;
 }
 else {
 xOrigin = xEnd;
 width = xStart - xEnd;
 }
 repaint();
 }

public void mouseClicked(MouseEvent e) {
 }

public void mouseEntered(MouseEvent e) {
 }

public void mouseExited(MouseEvent e) {
 }
 }

public static void main(String[] args) {
 Hw10_rsb draw = new Hw10_rsb();
 draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 }

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s