This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Please help!!!!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Pardner. I have some observations.
1.import java.lang.*; is unnecessary.
2. The Graphics class does not contain these methods:
g.fillSquare(25,25,25,25);
g.fillTriangle(10,10);
g.fillRect(25,25,25,25); will draw a square.
fillPolygon(Polygon p) may help with drawing a triangle.
3. Your method names are confusing. Naming conventions make programs more understandable by making them easier to read. "white" for instance is not a good name for a method.
4. How to ask questions the smart way.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto,
The methods fillSquare and fillTriangle do not exist. What JDK are you using for compling this code?
Are you getting any specific error while executing this code?
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic