Hi
This app is supposed to let a person draw depending on their choice of Shapes and color. I cant figure out what is going wrong. The colours and shapes barely work and the mouse position is not showing up at all. Mouse listener that tracks mouse position seems to just give errors.
Please help.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DrawPanel extends JPanel{
private MyShape shapeObjects[]; // stores all of the shape objects the user draws
private int shapeCount; // 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
public DrawPanel(JLabel label){
statusLabel = label;
shapeObjects = new MyShape[100];
shapeCount = 0;
currentShapeType = 1; // Line
currentShapeColor = Color.BLACK;
setBackground(Color.WHITE);
MouseHandler handler = new MouseHandler();
addMouseListener( handler );
addMouseMotionListener( handler );
}
@Override
public void paintComponent(Graphics g){
// draw the shapes
for (int i = 0; i < shapeCount; i++) {
shapeObjects[i].draw( g );
}
// If currentShapeObject is null, do nothing
if ( currentShapeObject != null ) {
currentShapeObject.draw( g );
}
}
// Set Methods
public void setCurrentShapeType(int currentShapeType) {
this.currentShapeType = currentShapeType;
}
public void setCurrentShapeColor(Color currentShapeColor) {
this.currentShapeColor = currentShapeColor;
}
public void setCurrentShapeFilled(boolean currentShapeFilled) {
this.currentShapeFilled = currentShapeFilled;
}
public void clearLastShape(){
if(shapeCount > 0){
shapeCount = shapeCount - 1;
}
repaint();
}
public void clearDrawing(){
shapeCount = 0;
repaint();
}
class MouseHandler extends MouseAdapter implements MouseMotionListener{
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[shapeCount] = currentShapeObject;
shapeCount++;
// 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() ) );
repaint();
}
}
}
#
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 );
}
}
}
}
#
import java.awt.Color;
import java.awt.Graphics;
public abstract class MyShape {
private int x1 = 0; // x-coordinate of first endpoint
private int y1 = 0; // y-coordinate of first endpoint
private int x2 = 0; // x-coordinate of second endpoint
private int y2 = 0; // y-coordinate of second endpoint
private Color myColor; // color of this shape
// Constructors
public MyShape(){
// a)A no-argument constructor that sets all the coordinates of the shape to 0 and the
// color to Color.BLACK.
setX1(0);
setY1(0);
setX2(0);
setY2(0);
setMyColor(Color.BLACK);
}
public MyShape(int x1, int y1, int x2, int y2, Color myColor, boolean filled) {
// b) A constructor that initializes the coordinates and color to the values of the
// arguments supplied.
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.myColor = myColor;
}
// Getters and Setters
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public int getX2() {
return x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getY2() {
return y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public Color getMyColor() {
return myColor;
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
}
// Abstract method
public abstract void draw( Graphics g );
}