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