Hi
Im creating a very basic Java applet a college and i need to be able to change the colour of a shape, choose weather the shape is solid or outline and also to draw freehand on the canvas.
Here is my code so far :
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//This class implements the ItemListener so that checkboxes can
//be used
public class Doodle2 extends Applet implements ItemListener
{
//Create components to add to applet
private Canvas Draw = new Canvas();
//Create a group for checkboxes
CheckboxGroup Shape = new CheckboxGroup();
//Create checkbox options and add to group
Checkbox Rectangle = new Checkbox("Rectangle", false, Shape);
Checkbox Circle = new Checkbox ("Circle", false, Shape);
Checkbox Line = new Checkbox("Line", false, Shape);
int X, Y;
//Over-ride applet's init method
public void init()
{
//Set the size of the canvas
Draw.setSize(350,350);
//Change the background colour of the canvas
Draw.setBackground(Color.white);
//Add canvas to the applet
add(Draw);
//Set the background colour of the applet
this.setBackground(Color.blue);
//Add checkboxes to the applet
add(Rectangle);
add(Circle);
add(Line);
//Add Item Listeners to the checkboxes
Rectangle.addItemListener(this);
Circle.addItemListener(this);
Line.addItemListener(this);
}
//Create the method that will be called when
//any of the checkboxes are clicked
public void itemStateChanged (ItemEvent check)
{
//Create a graphics object and associate
//it with the canvas' graphics context
Graphics g = Draw.getGraphics();
//If the checkbox clicked is Rectangle
if (check.getSource()==Rectangle)
{
//Set the foreground colour of the graphics object
g.setColor(Color.blue);
//Draw a rectangle
g.draw3DRect(X,Y,50,50,false);
}
else
{
//If the checkbox clicked is Circle
if (check.getSource()==Circle)
{
//Draw an oval
g.drawOval(X,Y,15,30);
}
else
{
//Draw a line
g.drawLine(X,Y,50,50);
}
}
X=X+20;
Y=Y+5;
}
}
Does anyone know how to get the shapes to change colour and also how to get solid and outline shapes!
The free hand bit is a low priority and is not that important!
If anyone could help me by monday i would much appriciate it!
Cheers
Scottio
Social Networking Bookmarks