Close

Results 1 to 1 of 1

Thread: Java Help!

  1. #1
    DF VIP Member Scottio200's Avatar
    Join Date
    Feb 2001
    Location
    Tha Toon
    Posts
    1,814
    Thanks
    42
    Thanked:        96
    Karma Level
    398

    Java, Drawing program, help!

    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
    Last edited by Scottio200; 20th June 2003 at 02:20 PM.

    http://scottio-productions.co.uk/
    XBL : Scottio200 :: PS3 PSN-ID : Scottio200 :: Twitter ID : Scottio200

Similar Threads

  1. J2ME / JBlend Java on Sharp GX10 ???
    By hummmm in forum Programming
    Replies: 0
    Last Post: 25th November 2002, 01:45 AM
  2. Java Popup's :()
    By Dragoncity in forum The Dog and Duck
    Replies: 0
    Last Post: 7th October 2002, 04:16 PM
  3. Java console??
    By Jaffa in forum PC Problems
    Replies: 3
    Last Post: 26th September 2002, 02:14 AM
  4. Java Idea?
    By Roo in forum Unlocking Questions & Solutions
    Replies: 0
    Last Post: 8th September 2002, 06:55 PM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •