Problem with simple drawing tool


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem with simple drawing tool

  1. #1
    Join Date
    Nov 2005
    Posts
    44

    Problem with simple drawing tool

    Hi,

    I've only just started using Java and would appreciate some help with a drawing tool application I'm currently trying to write.

    The problem is that when the user clicks on a button at the bottom of the frame, they are then supposed to be able to produce a square or circle by simply clicking on the canvas.

    Unfortunately, this is not currently happening.

    Please help!

    The code for both classes is as follows:

    1. DrawToolFrame Class:

    import java.awt.*;
    import java.awt.event.*;

    public class DrawToolFrame extends Frame
    implements WindowListener
    {
    DrawToolCanvas myCanvas;

    public DrawToolFrame()
    {
    setTitle("Draw Tool Frame");
    addWindowListener(this);
    Button square, circle;
    Panel myPanel = new Panel();
    square = new Button("square"); square.setActionCommand("square");
    circle = new Button("circle"); circle.setActionCommand("circle");
    myPanel.add(square); myPanel.add(circle);
    add("South", myPanel);
    DrawToolCanvas myCanvas = new DrawToolCanvas();
    add("Center", myCanvas);
    square.addMouseListener(myCanvas);
    circle.addMouseListener(myCanvas);
    }


    public void windowClosing(WindowEvent event) { System.exit(0); }
    public void windowOpened(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowClosed(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}

    }

    2. DrawToolCanvas Class:

    import java.awt.*;
    import java.awt.event.*;

    public class DrawToolCanvas
    extends Canvas
    implements MouseListener, ActionListener {

    String drawType="square";
    Point lastClickPoint=null;

    public void drawComponent(Graphics g) {
    if (lastClickPoint==null) {
    g.drawString("Click canvas first",20,20);
    } else {
    if (drawType.equals("square")) {
    square(g);
    }
    else if (drawType.equals("circle")) {
    circle(g);
    }
    }
    }

    public void actionPerformed(ActionEvent event) {
    if (event.getActionCommand().equals("square"))
    drawType="square";
    else if (event.getActionCommand().equals("circle"))
    drawType="circle";
    repaint();
    }
    public void mouseReleased(MouseEvent e) {
    lastClickPoint=e.getPoint();
    }
    public void square(Graphics g) {

    g.setColor(Color.red);
    g.fillRect(lastClickPoint.x, lastClickPoint.y, 40, 40);
    g.setColor(Color.black);
    }

    public void circle(Graphics g) {
    g.setColor(Color.blue);
    g.fillOval(lastClickPoint.x, lastClickPoint.y, 40, 40);
    g.setColor(Color.black);

    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {}

    }

    Any help would be appreciated!

  2. #2
    Join Date
    Nov 2005
    Posts
    44

    New problem with simple drawing tool

    HI,

    I know this will be a simple problem for all you Java developers out there, but how do I keep the shapes drawn in the following program on the canvas when a user clicks the mouse to draw another shape.

    At the moment, each new mouse click on the canvas is simply drawing a new shape.

    Also, does anyone know how to add buttons to allow a user to increase the size of the shapes before they add a new shape?

    The code for this class is as follows:


    import java.awt.*;
    import java.awt.event.*;

    public class DTF extends Frame implements WindowListener
    {
    DrawToolCanvas myCanvas; // member variable

    public DTF()
    {
    setTitle("Draw Tool Frame");
    addWindowListener(this);
    Button square, circle;
    Panel myPanel = new Panel();
    square = new Button("square"); square.setActionCommand("square");
    circle = new Button("circle"); circle.setActionCommand("circle");
    myPanel.add(square); myPanel.add(circle);
    add("South", myPanel);
    DrawToolCanvas myCanvas = new DrawToolCanvas(); // local variable
    // myCanvas = new DrawToolCanvas(); // member variable
    add("Center", myCanvas);
    square.addActionListener(myCanvas);
    circle.addActionListener(myCanvas);
    setSize(400,400);
    setLocation(200,200);
    setVisible(true);
    }

    public void windowClosing(WindowEvent event) { System.exit(0); }
    public void windowOpened(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowClosed(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}

    public static void main(String[] args)
    {
    new DTF();
    }
    }

    class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
    {
    int x, y;
    boolean drawCircle;

    public DrawToolCanvas()
    {
    x = -40;
    y = -40;
    drawCircle = true;
    addMouseListener(this);
    }

    public void paint(Graphics g)
    {
    super.paint(g);
    if(drawCircle)
    {
    g.setColor(Color.blue);
    g.fillOval( x, y, 40, 40 );
    }
    else
    {
    g.setColor(Color.red);
    g.drawRect(x, y, 40, 40);
    }
    }

    public void actionPerformed(ActionEvent event)
    {
    if (event.getActionCommand().equals("square"))
    drawCircle = false;
    else if (event.getActionCommand().equals("circle"))
    drawCircle = true;
    }

    public void mousePressed(MouseEvent e)
    {
    x = e.getX();
    y = e.getY();
    repaint();
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    }

    Help would really be appreciated!

Similar Threads

  1. Problem with double quotes in xsl
    By lit in forum XML
    Replies: 4
    Last Post: 02-23-2005, 03:37 AM
  2. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  3. simple XML APPENDING problem
    By scopee in forum XML
    Replies: 4
    Last Post: 04-13-2001, 03:31 AM
  4. Simple SQL Parameter problem
    By Eric in forum Database
    Replies: 1
    Last Post: 11-10-2000, 02:05 AM
  5. Simple report problem
    By Gary Thompson in forum authorevents.kurata
    Replies: 1
    Last Post: 04-20-2000, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center