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();
  } 


}

Recommended Answers

All 2 Replies

i'm having difficulty with at the moment is creating a field that will remember the current shape the user has chosen

To remember a value, you need a variable in your class to contain the value for future reference. What type of data is the current shape held in? Is it a String or some other type of object.
Your code references a class named shape (Note coding conventions are that class names begin with uppercase letters) Since java already has a class named Shape you could name you class: MyShape or whatever makes sense.
Define the class to hold the shape information with the values you need to define a shape.

Member Avatar for coil

You've already created a currentShape variable - use it. Change its reference every time a shape is selected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.