I'm supposed to allow the user to draw shapes based on their combobox choices (eg. line, rectangle, circle), colour, fill state, etc. However, nothing is showing up.
Model class:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class DrawPanel extends JPanel
{
// Declare variables:
private MyShape shapeObjects[]; // stores all of the shape objects the user draws
private int numOfShapes; // counts the number of shapes in the array
private int currentShapeType; // determines the type of shape to draw
private MyShape currentShapeObject; // represents the current shape the user is drawing
private Color currentShapeColor; // represents the current drawing currentShapeColor
private boolean currentShapeFilled; // determines whether to draw a currentShapeFilled shape
private JLabel statusLabel; // represents the status bar, displays the coordinates of the current mouse position
// Constructor with a single JLabel parameter
public DrawPanel( JLabel statusLabel ) {
// Initialize variables:
this.statusLabel = statusLabel;
shapeObjects = new MyShape[100]; // 100 storage for user-drawn shapes
numOfShapes = 0; // Defult number of shapes is 0
currentShapeType = 1; // Default shape is a line
currentShapeColor = Color.BLACK; // Default colour is black
// Set background colour of the window to white
setBackground( Color.WHITE );
// Create and register listener for mouse and mouse motion events
MouseHandler handler = new MouseHandler();
addMouseListener( handler );
addMouseMotionListener( handler );
}
// Mutators
public void setCurrentShapeType( int currentShapeType ) {
this.currentShapeType = currentShapeType;
}
public void setCurrentShapeColor( Color currentShapeColor ) {
this.currentShapeColor = currentShapeColor;
}
public void setCurrentShapeFilled( boolean currentShapeFilled ) {
this.currentShapeFilled = currentShapeFilled;
}
// Clears the last shape drawn by decrementing the instance variable numOfShapes.
// Ensures that numOfShapes is never less than zero.
public void clearLastShape() {
// Do nothing if numOfShapes is less than or equal to zero.
if ( numOfShapes <= 0) {
return;
}
// Clear the current shape
currentShapeObject = null;
// Decrement the number of existing shapes
numOfShapes--;
repaint();
}
// Remove all the shapes in the current drawing by setting numOfShapes to zero
public void clearDrawing() {
numOfShapes = 0;
repaint();
}
// Draws the shapes in the shapeObjects array.
// Uses instance variable numOfShapes to determine how many shapes to draw.
// Method paintComponent() should also call currentShapeObject’s draw() method,
// provided that currentShapeObject is not null.
public void paintComponent( Graphics g ) {
// Inherit the paintComponent method from the superclass
super.paintComponent( g );
// Determine how many shapes to draw
numOfShapes++;
// If currentShapeObject is null, do nothing
if ( currentShapeObject == null ) {
return;
} else {
// Otherwise, draw the shapes
currentShapeObject.draw( g );
}
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener {
// Handles event when mouse is pressed
public void mousePressed( MouseEvent event ) {
// Determine current shape type
if ( currentShapeType == 1 ) {
currentShapeObject = new MyLine();
} else if ( currentShapeType == 2 ) {
currentShapeObject = new MyRectangle();
} else {
currentShapeObject = new MyOval();
}
// Get current mouse position
int x1 = event.getX();
int y1 = event.getY();
// Create shape of the type specified by currentShapeType
// Initializes both points to the mouse position
currentShapeObject.setX1( x1 );
currentShapeObject.setY1( y1 );
}
}
// Handles event when mouse is released after dragging
public void mouseReleased( MouseEvent event ) {
// Get current mouse position
int x2 = event.getX();
int y2 = event.getY();
// Finish drawing the shape and initialize current coordinates to currentShapeObject
currentShapeObject.setX2( x2 );
currentShapeObject.setY2( y2 );
// Place it into the shapeObjects array
shapeObjects[numOfShapes] = currentShapeObject;
// Clear currentShapeObject's information
currentShapeObject = null;
repaint();
}
// Handles event when user moves the mouse
// Sets the text of the statusLabel so that it displays (constantly updated) mouse coordinates
public void mouseMoved( MouseEvent event )
{
statusLabel.setText( String.format( "(%d, %d)", event.getX(), event.getY() ) );
}
// Handles event when user drags mouse with button pressed
// Calls method repaint() to allow the user to see the shape while dragging the mouse
public void mouseDragged( MouseEvent event )
{
// Get current mouse position
int x2 = event.getX();
int y2 = event.getY();
// Set second point of currentShapeObject to current mouse position
currentShapeObject.setX2( x2 );
currentShapeObject.setY2( y2 );
// Update statusLabel with the current mouse position
statusLabel.setText( String.format( "(%d, %d)", event.getX(), event.getY() ) );
}
}
View:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DrawFrame extends JFrame
{
// Declare variables:
private JButton undo; // undoes the last shape drawn
private JButton clear; // clears all shapes from the drawing
private JComboBox colour; // selects the colour from the 13 predefined colours
private JComboBox shape; // selects the shape to draw
private JCheckBox fill; // specifies whether a shape should be filled or unfilled
private JLabel status; // Status bar text
private String colList[] = { "Black", "White", "Red", "Green", "Blue", "Yellow", "Orange", "Magenta", "Cyan", "Pink", "Dark Gray", "Gray", "Light Gray" }; // List for the colour JComboBox
private String shapeList[] = { "Line", "Rectangle", "Oval" }; // List for the shape JComboBox
// New DrawPanel object
DrawPanel panel = new DrawPanel( status );
// Constructor
public DrawFrame() {
// Main frame properties
super( "SuperPaint Application" ); // Title bar text
setLayout( new BorderLayout() ); // Set frame layout
// Initialize variables:
status = new JLabel();
undo = new JButton( "Undo" );
clear = new JButton( "Clear" );
colour = new JComboBox( colList );
shape = new JComboBox( shapeList );
fill = new JCheckBox( "Filled" );
// Add DrawPanel to the DrawFrame
add( panel );
// Panel in the north region of the border layout for components
JPanel northPanel = new JPanel();
northPanel.setLayout( new FlowLayout() );
add( northPanel, BorderLayout.NORTH );
// Add components into the north panel
northPanel.add( undo );
northPanel.add( clear );
northPanel.add( colour );
northPanel.add( shape );
northPanel.add( fill );
// Panel in the center region of the border layout for main drawings
add( panel, BorderLayout.CENTER );
// Status bar in the south region of border layout
add( status, BorderLayout.SOUTH );
// Create and register listeners for events
ButtonHandlerU bHandlerU = new ButtonHandlerU();
undo.addActionListener( bHandlerU );
ButtonHandlerC bHandlerC = new ButtonHandlerC();
clear.addActionListener( bHandlerC );
ComboBoxHandlerC cmbHandlerC = new ComboBoxHandlerC();
colour.addItemListener( cmbHandlerC );
ComboBoxHandlerS cmbHandlerS = new ComboBoxHandlerS();
shape.addItemListener( cmbHandlerS );
CheckBoxHandler cbHandler = new CheckBoxHandler();
fill.addItemListener( cbHandler );
}
// Inner class for button event handling for the undo button
private class ButtonHandlerU implements ActionListener
{
public void actionPerformed( ActionEvent event ) {
panel.clearLastShape();
}
}
// Inner class for button event handling for the clear button
private class ButtonHandlerC implements ActionListener {
public void actionPerformed( ActionEvent event ) {
panel.clearDrawing();
}
}
// Inner class for the colour combobox
private class ComboBoxHandlerC implements ItemListener {
public void itemStateChanged( ItemEvent event ) {
// Determines whether check box is selected
if ( event.getStateChange() == ItemEvent.SELECTED ) {
// Set colour selections to actual colours
if ( colour.getSelectedIndex() == 1 ) {
panel.setCurrentShapeColor( Color.BLACK );
} else if ( colour.getSelectedIndex() == 2 ) {
panel.setCurrentShapeColor( Color.WHITE );
} else if ( colour.getSelectedIndex() == 3 ) {
panel.setCurrentShapeColor( Color.RED );
} else if ( colour.getSelectedIndex() == 4 ) {
panel.setCurrentShapeColor( Color.GREEN );
} else if ( colour.getSelectedIndex() == 5 ) {
panel.setCurrentShapeColor( Color.BLUE );
} else if ( colour.getSelectedIndex() == 6 ) {
panel.setCurrentShapeColor( Color.YELLOW );
} else if ( colour.getSelectedIndex() == 7 ) {
panel.setCurrentShapeColor( Color.ORANGE );
} else if ( colour.getSelectedIndex() == 8 ) {
panel.setCurrentShapeColor( Color.MAGENTA );
} else if ( colour.getSelectedIndex() == 9 ) {
panel.setCurrentShapeColor( Color.CYAN );
} else if ( colour.getSelectedIndex() == 10 ) {
panel.setCurrentShapeColor( Color.PINK );
} else if ( colour.getSelectedIndex() == 11 ) {
panel.setCurrentShapeColor( Color.darkGray );
} else if ( colour.getSelectedIndex() == 12 ) {
panel.setCurrentShapeColor( Color.GRAY );
} else {
panel.setCurrentShapeColor( Color.lightGray );
}
}
}
}
// Inner class for the shape combobox
private class ComboBoxHandlerS implements ItemListener {
public void itemStateChanged( ItemEvent event ) {
// Determines whether check box is selected
if ( event.getStateChange() == ItemEvent.SELECTED ) {
if ( shape.getSelectedIndex() == 1 ) {
panel.setCurrentShapeType( 1 );
} else if ( shape.getSelectedIndex() == 2 ) {
panel.setCurrentShapeType( 2 );
} else {
panel.setCurrentShapeType( 3 );
}
}
}
}
// Inner class for checkbox event handling for the fill state
private class CheckBoxHandler implements ItemListener {
public void itemStateChanged( ItemEvent event ) {
if ( event.getSource() == fill ) {
panel.setCurrentShapeFilled( true );
} else {
panel.setCurrentShapeFilled( false );
}
}
}
}
A shape class for reference:
import java.awt.Color;
import java.awt.Graphics;
public class MyLine extends MyShape
{
// No-parameter constructor
public MyLine () {
super.setX1(0);
super.setY1(0);
super.setX2(0);
super.setY2(0);
super.setColor(Color.BLACK);
}
// Constructor with parameters for the coordinates and colour
public MyLine( int x1, int y1, int x2, int y2, Color colour ) {
super.setX1(x1); // set x coordinate of first endpoint
super.setY1(y1); // set y coordinate of first endpoint
super.setX2(x2); // set x coordinate of second endpoint
super.setY2(y2); // set y coordinate of second endpoint
super.setColor(colour); // set the colour
} // end MyLine constructor
// Actually draws the line
public void draw( Graphics g ) {
g.setColor( super.getColor() );
g.drawLine( super.getX1(), super.getY1(), super.getX2(), super.getY2() );
}
}
I'm pretty sure it's something in setting the numOfShapes variable, or just a faulty draw method. Thanks if anyone can help!