hi, im new to java and am trying to create a simple paint program, what i'm having difficulty with at the moment is creating a field that will remember the current shape the user has chosen, i really don't know too much about what i'm doing more trial and error so any help would be appreciated.
import java.util.*;
import comp100.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.*;
public class MiniPaint implements ActionListener, MouseListener{
private JFrame frame = new JFrame("MiniPaint");
private DrawingCanvas canvas = new DrawingCanvas();
// fields to remember the current shape, whether filled or not
// the position the mouse was pressed, and the JTextField
// YOUR CODE HERE
private shape currentShape;
private int mousePosX;
private int mousePosY;
private JTextField textBox;
private Color currentColor;
public MiniPaint(){
frame.setSize(800,600);
frame.getContentPane().add(canvas, BorderLayout.CENTER);
canvas.addMouseListener(this);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
// YOUR CODE HERE
addButton("Line",panel);
addButton("Rect",panel);
addButton("Oval",panel);
addButton("Image",panel);
addButton("Text",panel);
addButton("Colour",panel);
addButton("Fill/NoFill",panel);
addButton("Clear",panel);
addButton("Quit",panel);
frame.setVisible(true);
}
private JButton addButton(String name, JPanel panel){
JButton button = new JButton(name);
button.addActionListener(this);
panel.add(button);
return button;
}
/* Respond to button presses */
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
// YOUR CODE HERE
if (cmd.equals("Line") ){
this.canvas.drawLine(x1,y1,x2,y2);
}
else if (cmd.equals("Rect") ){
this.canvas.fillRect(x1,y1,x2,y2);
}
else if (cmd.equals("Oval") ){
this.canvas.fillOval(x1,y1,x2,y2);
}
else if (cmd.equals("Image") ){
String loadImage = FileChooser.open("Choose image file");
this.canvas.drawImage(loadImage,x1,y1,x2,y2);
}
else if (cmd.equals("Text") ){
this.canvas.drawRect(x1,y1,x2,y2);
}
else if (cmd.equals("Colour") ){
this.currentColor = JColorChooser.showDialog(frame, "Choose Colour", Color.black);
this.canvas.setForeground(this.currentColor);
}
else if (cmd.equals("Fill/NoFill") ){
}
else if (cmd.equals("Clear") ){
canvas.clear();
}
else if (cmd.equals("Quit") ){
frame.dispose();
}
}
int x1;
int y1;
int x2;
int y2;
// Respond to mouse events
public void mousePressed(MouseEvent e) {
// YOUR CODE HERE
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
// YOUR CODE HERE
x2 = e.getX();
y2 = e.getY();
}
public void mouseClicked(MouseEvent e) {}//needed to satisfy interface
public void mouseEntered(MouseEvent e) {} //needed to satisfy interface
public void mouseExited(MouseEvent e) {} //needed to satisfy interface
/* Helper methods for drawing the shapes, if you choose to define them */
// YOUR CODE HERE
// Main: constructs new MiniPaint object
public static void main(String[] arguments){
MiniPaint ob = new MiniPaint();
}
}