DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: Please Help!!!

  1. #1
    Join Date
    Mar 2004
    Posts
    3

    Please Help!!!

    Im trying to build my first applet but im totally lost. I cant understand what ive done wrong. I want to be able to select a choice(circle,square,etc and click on a button which draws that object on my canvas but imlost.Can some1 please help. Here is my code in full...
    //import classes
    import java.awt.*;
    import java.io.*;
    import java.lang.*;
    import java.net.URL;
    import java.util.*;
    import java.awt.Color;
    import java.applet.*;

    //Java Applet of Objects
    public class Objects extends Applet
    {
    ObjectsControls controls;
    ObjectsCanvas canvas;
    int c;

    //initialise border,controls(add to Pane)
    public void init()
    {
    setLayout(new BorderLayout());
    ObjectsCanvas c = new ObjectsCanvas();
    add("Center", c);
    add("North", controls = new ObjectsControls(c));
    }

    //start controls
    public void start()
    {
    controls.start();
    }

    //stop controls
    public void stop()
    {
    controls.stop();
    }

    //initialise frame with objects
    public static void main(String args[])
    {
    Frame f = new Frame("Objects");
    Objects objects = new Objects();

    objects.init();
    objects.start();

    f.add("Center", objects);
    f.resize(550,600 );
    f.show();
    }
    }

    /********************************
    * - Applet Canvas - *
    * paint method *
    * (objects and canvas) *
    ********************************/


    class ObjectsCanvas extends Canvas
    {
    boolean submit = false;
    int noob = 0;
    int obj = 0;
    Objects objects;
    ObjectsControls controls;

    public void add(Objects object){}
    //paint method
    public void paint(Graphics g)
    {
    this.white(g);
    }


    public void update(Graphics g)
    {
    if (!submit)this.white(g);
    else
    {
    if (noob == 0)
    {
    this.oval(g,obj);
    }
    else if (noob == 1)
    {
    this.rect(g,obj);
    }
    else if (noob == 2)
    {
    this.square(g,obj);
    }
    else
    {
    this.triangle(g,obj);
    }
    }
    }

    //canvas(draw objects)
    public void white(Graphics g)
    {
    g.setColor(Color.white);
    g.fillRect(10,10,500,280);
    g.setColor(Color.black);
    g.drawRect(10,10,500,280);
    }

    //object oval
    public void oval(Graphics g,int obj)
    {
    g.setColor(Color.red);
    g.fillOval(30,30,20,20);
    }

    //object rectangle
    public void rect(Graphics g,int obj)
    {
    g.setColor(Color.blue);
    g.fillRect(50,50,400,220);
    }

    //object square
    public void square(Graphics g,int obj)
    {
    g.setColor(Color.green);
    g.fillSquare(25,25,25,25);
    }

    //object triangle
    public void triangle(Graphics g,int obj)
    {
    g.setColor(Color.yellow);
    g.fillTriangle(10,10);
    }

    public void getdata(int noob)
    {
    this.noob = noob;
    }

    public void redraw(boolean submit)
    {
    this.submit = submit;
    repaint();
    }
    }

    /********************************
    * - Applet Controls - *
    * creating controls *
    * action method *
    ********************************/

    class ObjectsControls extends Panel
    {
    int o,obj;
    ObjectsCanvas canvas;

    //add object controls
    public ObjectsControls(ObjectsCanvas canvas)
    {
    this.canvas = canvas;

    Choice o = new Choice();
    o.addItem("Circle");
    o.addItem("Rectangle");
    o.addItem("Square");
    o.addItem("Triangle");
    add(o);

    add(new Button("submit"));
    add(new Button("clear"));
    }

    //action method
    public boolean action(Event e, Object arg)
    {
    if (e.target instanceof Choice)
    {
    String label = (String)arg;

    if (label.equals("Circle"))
    o=0;
    else if (label.equals("Rectangle"))
    o=1;
    else if (label.equals("Square"))
    o=2;
    else
    o=3;

    canvas.getdata(o);
    return true;
    }
    else if(e.target instanceof Button)
    {
    String label = (String)arg;
    canvas.redraw(label.equals("submit"));
    return true;
    }
    return false;
    }

    //start actions
    public void start()
    {
    this.enable();
    }

    //stop actions
    public void stop()
    {
    this.disable();
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    read the discussion in my signature, regarding Inherited SHapes
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links