Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

canvas problems 1

Status
Not open for further replies.

welshspoon

Technical User
Dec 14, 2002
34
GB
I'm having some problems with my code. I want the user to be able to draw on my canvas (i.e. over the two shapes) but i cannot see why my code wont let me. My code is written below.

Thank you

-----------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sketcher extends Frame
{
public Sketcher()
{
setSize(500, 500);
setTitle("My Dodgy Game");
Canvas ExampleCanvas = new ExampleCanvas();
add(ExampleCanvas);
}
class ExampleCanvas extends Canvas
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Rectangle d = new Rectangle(50, 50, 400, 400);
Rectangle j = new Rectangle(100, 90, 300, 320);
g2.draw(d);
g2.draw(j);
g.drawString("Start where ever takes your fancy, and try to stay between the lines! Good Luck!",20,20);
}
}

public static void main(String[] args)
{
Sketcher f = new Sketcher();
MouseWatcher mw = new MouseWatcher();
f.addMouseListener(mw);
f.addMouseMotionListener(mw);
f.show();
}
}

 
Sorry i didn't leave the other class. I've pasted it below.

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

//Class MouseWatcher listens out for Mouse Events and Mouse Motion Events

public class MouseWatcher extends MouseAdapter implements MouseMotionListener {

private Point p, oldp; //Current, Previous position of mouse

public void mousePressed(MouseEvent e){

oldp = e.getPoint(); //Find initial mouse position

}

public void mouseDragged(MouseEvent e)
{
p = e.getPoint(); //Find mouse position
Graphics g = ((Canvas)e.getSource()).getGraphics(); //Get graphics context
g.drawLine(oldp.x, oldp.y, p.x, p.y); //Draw line from old position to here
oldp = p; //Set current position to old.
g.dispose();
}

public void mouseMoved(MouseEvent e){} //Do nothing for mouse moves

}

// if mouse coordinates outside area do something
// else
// do nothing
 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sketcher extends Frame
{
    public Sketcher()
    {
        setSize(500, 500);
        setTitle("My Dodgy Game");
    }

    public static void main(String[] args)
    {
        Sketcher f = new Sketcher();
        ExampleCanvas ExampleCanvasObj = new ExampleCanvas();
        f.add(ExampleCanvasObj);
        MouseWatcher mw = new MouseWatcher(ExampleCanvasObj);
        ExampleCanvasObj.addMouseListener(mw);
        ExampleCanvasObj.addMouseMotionListener(mw);
        f.show();
    }
}
        class ExampleCanvas extends Canvas
        {
        final static int max=1000;
        Point[] arrayPoints= new Point[max];
        int lastElement = -1;
            public ExampleCanvas()
            {
             super();
            }        
            public void addPoint(Point tempP)
            {
             lastElement++;
             if (lastElement<=max-1)
                arrayPoints[lastElement] = tempP;
            }
            public void paint(Graphics g)
            {
                Graphics2D g2 = (Graphics2D)g;
                Rectangle d = new Rectangle(50, 50, 400, 400);
                Rectangle j = new Rectangle(100, 90, 300, 320);
                g2.draw(d);
                g2.draw(j);
                g.drawString("Start where ever takes your fancy, and try to stay between the lines! Good Luck!",20,20);
                if (lastElement>-1)
                   {
                    g.drawLine(arrayPoints[0].x, arrayPoints[0].y, arrayPoints[0].x, arrayPoints[0].y);
                    if (lastElement>0)
                       {
                        for (int i=1;i<lastElement;i++)
                             g.drawLine(arrayPoints[i-1].x, arrayPoints[i-1].y, arrayPoints[i].x, arrayPoints[i].y);   
                       }
                   }
            }
        }
Code:
import java.awt.*;
import java.awt.event.*;

//Class MouseWatcher listens out for Mouse Events and Mouse Motion Events

public class MouseWatcher extends MouseAdapter implements MouseMotionListener {
ExampleCanvas pointToExampleCanvas;
    public MouseWatcher(ExampleCanvas tempCanvas)
           {
            pointToExampleCanvas = tempCanvas;
           }

    private Point p, oldp;                                        //Current, Previous position of mouse

    public void mousePressed(MouseEvent e){

        oldp = e.getPoint();                                    //Find initial mouse position

    }

    public void mouseDragged(MouseEvent e)
    {
        p = e.getPoint();     
System.out.println(p);
        pointToExampleCanvas.addPoint(p);
        pointToExampleCanvas.repaint();      
    }

    public void mouseMoved(MouseEvent e){}                        //Do nothing for mouse moves

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top