keeping shapes on a canvas with a paint tool


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: keeping shapes on a canvas with a paint tool

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    1

    keeping shapes on a canvas with a paint tool

    Hi,

    I'm trying to write a basic paint tool, and cannot seem to keep the shapes drawn on the canvas. Each time a user clicks on the canvas, it draws a new shape, and deletes the previous shape.

    Any idea how to keep all the shapes drawn on the canvas?

    Help me please!!!

    Code so far 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) {}
    }

  2. #2
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23
    oops!
    Last edited by nescafe; 11-06-2005 at 12:29 PM.

  3. #3
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23
    oops! number 2!
    Last edited by nescafe; 11-06-2005 at 12:30 PM.

  4. #4
    Join Date
    Sep 2005
    Location
    TX
    Posts
    23
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    .....}
    First, comment out the line
    super.paint(g)
    Second, the update method of your canvas: update(g)
    should be overridden to only call the paint method:

    HTML Code:
    public void update(Graphics g){
         paint(g);
    }
    That'll fix your problem of having the shapes disappear w/ each successive click by the user.
    I can't exactly explain the logic of how all this works but if you try out what I say it will indeed work.
    Perhaps another forum user will be able to better explain how the painting works.

  5. #5
    Join Date
    Nov 2005
    Posts
    44

    Re: keeping shapes on a canvas with a paint tool

    Hi,

    Thank you so much for your help. Works like a dream now!!

    Have a great weekend.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Sorry guys, but that doesn't fix the problem, if I minimize/restore this frame
    then only the last shape is visible. I call this method "drawing in the sand".

    But, we just fixed that in another thread:

    http://forums.devx.com/showthread.ph...144#post440144

    Here I have "fixed" your code using double buffering, and the method could
    well be called "tattoing" . Its not a good one, the link above shows the
    preferred way of solving this.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    
    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);
        myCanvas = new DrawToolCanvas(); // member variable
        add("Center", myCanvas);
        square.addActionListener(myCanvas);
        circle.addActionListener(myCanvas);
        setSize(400, 400);
        setLocation(200, 200);
        setVisible(true);
        myCanvas.prepBuffer();
      }
    
      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;
      BufferedImage memImg=null;
      Graphics memG=null;
    
      public DrawToolCanvas() {
        x = -40;
        y = -40;
        drawCircle = true;
        addMouseListener(this);
        prepBuffer();
      }
    
      public void prepBuffer() {
        memImg=new BufferedImage(1024,1024,BufferedImage.TYPE_INT_RGB);
        memG=memImg.getGraphics();
        memG.setColor(Color.white);
        memG.fillRect(0,0,getWidth(), getHeight());
      }
      public void update(Graphics g) {
        paint(g);
      }
      public void paint(Graphics g) {
        if (drawCircle) {
          memG.setColor(Color.blue);
          memG.fillOval(x, y, 40, 40);
        }
        else {
          memG.setColor(Color.red);
          memG.drawRect(x, y, 40, 40);
        }
        g.drawImage(memImg,0,0,this);
      }
    
      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) {}
    }
    eschew obfuscation

Similar Threads

  1. Java Paint Bucket Tool
    By gjab13 in forum Java
    Replies: 4
    Last Post: 09-02-2005, 02:50 PM
  2. Replies: 1
    Last Post: 08-17-2005, 06:42 PM
  3. DockableWindow 2.0 - Adds tool window auto-hide & tabbed MDI
    By Actipro Software in forum dotnet.announcements
    Replies: 0
    Last Post: 08-25-2002, 11:11 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