Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java applet's

  • 18-12-2001 04:16PM
    #1
    Registered Users, Registered Users 2 Posts: 1,684 ✭✭✭


    Design and implement a Java application that draws shapes on the screen.

    Your application should provide a facility for drawing, resizing and dragging the shape around the screen. The user should be able to select the required shape and the system should provide a facility to calculate the area of the shape.

    i have no idea of java applets to this level any pointers or assistance would be great.


Comments

  • Registered Users, Registered Users 2 Posts: 932 ✭✭✭yossarin


    a quick look at the sun site gives us the following

    'Methods for Drawing and Event Handling':
    http://java.sun.com/docs/books/tutorial/applet/overview/componentMethods.html
    for event/ MouseListener user-applet interaction

    and

    'Moving an Image across a screen':
    http://java.sun.com/docs/books/tutorial/uiswing/painting/movingImage.html
    for simple threaded animation

    look at those for a start

    yoss


  • Registered Users, Registered Users 2 Posts: 1,783 ✭✭✭Puck


    Doesn't your homework ask you to do an application and not an Applet?


  • Registered Users, Registered Users 2 Posts: 1,684 ✭✭✭Kraken


    its not homework me just wants to try it.
    Have tried and failed miserably. Couldnt get it to do anything i wanted it to. going to give up. If anyone wants to throw up some thing like what i am looking for ill take a look it might inspire me to try again and improve on my own. which i think i deleted...


  • Registered Users, Registered Users 2 Posts: 1,684 ✭✭✭Kraken


    [code]
    import java.awt.*;

    class Dot
    {
    int left, top;
    Color color;
    String labl;
    Font fnt;

    Dot(String lbl, int l, int t, Color c, Font f)
    {
    labl = lbl;
    left = l;
    top = t;
    color = c;
    fnt = f;
    }
    public int widthOf() { return 0; }
    public int heightOf() { return 0; }
    public void setWidth(int w) {}
    public void setHeight(int h) {}
    public void setLabel(String s) { labl = s; }
    public void setLeft(int l) { left = l; }
    public void setTop(int t) { top = t; }
    public void setColor(Color c) { color = c; }
    public int leftOf() { return left; }
    public int topOf() { return top; }
    public Color colorOf() { return color; }
    public String labelOf() { return labl; }
    public void draw (Graphics q) {}
    public boolean find (int x, int y) { return false; }
    public void setFont(Font f) { fnt = f; }
    public Font getFont() { return fnt; }
    }

    class RectDot extends Dot
    {
    int width, height;

    RectDot (String s, int l, int t, int w, int h, Color c, Font f)
    {
    super(s,l,t,c,f);
    width = w;
    height = h;
    }
    public int widthOf() { return width; }
    public int heightOf() { return height; }
    public void setWidth(int w) { width = w; }
    public void setHeight(int h) { height = h; }
    public boolean find(int x, int y)
    {
    if (super.leftOf() < x && x < super.leftOf()+width &&
    super.topOf() < y && y < super.topOf()+height)
    return true;
    return false;
    }
    public void draw(Graphics g)
    {
    g.setFont(super.getFont());
    int w = 20;
    int h = 10;
    g.setColor(colorOf());
    g.fillRect(leftOf(), topOf(), width, height);
    g.setColor(Color.black);
    g.drawString(labl, leftOf()-w/2+width/2, topOf()+h/4+height/2);
    }
    }

    class OvalDot extends Dot
    {
    int width, height;

    OvalDot (String s, int l, int t, int w, int h, Color c, Font f)
    {
    super(s,l,t,c,f);
    width = w;
    height = h;
    }

    public int widthOf() { return width; }
    public int heightOf() { return height; }
    public void setWidth(int w) { width = w; }
    public void setHeight(int h) { height = h; }

    public boolean find(int x, int y)
    {
    if (super.leftOf() < x && x < super.leftOf()+width &&
    super.topOf() < y && y < super.topOf()+height)
    return true;
    return false;
    }

    public void draw(Graphics g)
    {
    g.setFont(super.getFont());
    int w = 20;
    int h = 10;
    g.setColor(colorOf());
    g.fillOval(leftOf(), topOf(), width, height);
    g.setColor(Color.black);
    g.drawString(labl, leftOf()-w/2+width/2, topOf()+h/4+height/2);
    }
    }

    class CircDot extends Dot
    {
    int diam;

    CircDot (String s, int l, int t, int d, Color c, Font f)
    {
    super(s,l,t,c,f);
    diam = d;
    }

    public int widthOf() { return diam; }
    public int heightOf() { return diam; }
    public int diameterOf() { return diam; }
    public void setWidth(int w) { diam = w; }
    public void setHeight(int h) { diam = h; }
    public void setDiam(int d) { diam = d; }

    public boolean find (int x, int y)
    {
    if (super.leftOf() < x && x < super.leftOf()+diam &&
    super.topOf() < y && y < super.topOf()+diam)
    return true;
    return false;
    }


  • Registered Users, Registered Users 2 Posts: 1,684 ✭✭✭Kraken


    public void draw(Graphics g)
    {
    g.setFont(super.getFont());
    int w = 20;
    int h = 10;
    g.setColor(colorOf());
    g.fillOval(leftOf(), topOf(), diam, diam);
    g.setColor(new Color(0,0,0));
    g.drawString(labl, leftOf()-w/2+diam/2, topOf()+h/4+diam/2);
    }
    }

    public class ObjectsApplet extends java.applet.Applet
    {
    String fontstylechoice[] = { "Helvetica", "Times Roman", "Courier",
    "Dialog", "DialogInput", "Zapf Dingbats" };
    String fontstyleselect[] = { "Helvetica", "TimesRoman", "Courier",
    "Dialog", "DialogInput", "ZapfDingbats" };
    int nfontstyles = 6;
    String fontsizechoice[] = { "6", "8", "10", "12", "14", "16", "18" };
    int nfontsizes = 7;
    Button redbutton, bluebutton, greenbutton, rectbutton, textbutton,
    circbutton, ovalbutton, objectbutton, bigbutton, smallbutton,
    fontbutton;
    TextField namefield;
    Panel mainpanel;
    Dot dot[] = new Dot[100];
    int ndots = 0, current = -1;
    Color dotcolor = Color.red;
    Dot pick = null;
    int fontstyle = 0;
    int fontsz = 0;
    Font myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
    Integer.parseInt(fontsizechoice[fontsz]));
    int diffx, diffy;
    Choice fonttype, fontsize;

    public void init()
    {
    setLayout(new BorderLayout());
    Panel p = new Panel();
    p.setLayout(new GridLayout(18,1));
    p.add(new Label(""));
    p.add(redbutton = new Button("Red"));
    p.add(bluebutton = new Button("Blue"));
    p.add(greenbutton = new Button("Green"));
    p.add(new Label(""));
    p.add(rectbutton = new Button("Rectangle"));
    p.add(circbutton = new Button("Circle"));
    p.add(ovalbutton = new Button("Oval"));
    p.add(new Label(""));
    p.add(bigbutton = new Button("Bigger"));
    p.add(smallbutton = new Button("Smaller"));
    p.add(new Label(""));
    p.add(textbutton = new Button("Set Text"));
    p.add(new Label(""));
    p.add(fonttype = new Choice());
    for (int i=0 ; i < nfontstyles ; i++)
    fonttype.addItem(fontstylechoice);
    p.add(fontsize = new Choice());
    for (int i=0 ; i < nfontsizes ; i++)
    fontsize.addItem(fontsizechoice);
    p.add(fontbutton = new Button("Set Font"));
    p.add(new Label(""));
    add("East", p);
    Panel q = new Panel();
    q.setLayout(new GridLayout(1,2));
    q.add(objectbutton = new Button("New Object"));
    q.add(namefield = new TextField(24));
    add("South", q);
    mainpanel = new Panel();
    add("Center", mainpanel);
    }

    Image offscreen;
    Dimension offscreensize;
    Graphics offgraphics;

    public void update(Graphics g)
    {
    Dimension d = size();
    if ((offscreen == null) || (d.width != offscreensize.width) ||
    (d.height != offscreensize.height))
    {
    offscreen = createImage(d.width, d.height);
    offscreensize = d;
    offgraphics = offscreen.getGraphics();
    offgraphics.setFont(getFont());
    }

    offgraphics.setColor(getBackground());
    offgraphics.fillRect(0, 0, d.width, d.height);
    for (int i=0 ; i < ndots ; i++) dot.draw(offgraphics);
    g.drawImage(offscreen, 0, 0, null);
    }

    public boolean mouseDown(Event evt, int x, int y)
    {
    for (int i=0 ; i < ndots ; i++)
    {
    if (dot.find(x,y))
    {
    pick = dot;
    current = i;
    diffx = dot.leftOf()-x;
    diffy = dot.topOf()-y;
    break;
    }
    }
    return true;
    }

    public boolean mouseDrag (Event evt, int x, int y)
    {
    if (pick != null)
    {
    pick.setLeft(x+diffx);
    pick.setTop(y+diffy);
    update(mainpanel.getGraphics());
    }
    return true;
    }

    public boolean mouseUp (Event evt, int x, int y)
    {
    pick = null;
    return true;
    }

    public boolean action (Event evt, Object obj)
    {
    if (evt.target == fonttype) {
    fontstyle = fonttype.getSelectedIndex();
    myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
    Integer.parseInt(fontsizechoice[fontsz]));
    } else
    if (evt.target == fontsize) {
    fontsz = fontsize.getSelectedIndex();
    myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
    Integer.parseInt(fontsizechoice[fontsz]));
    } else
    if (evt.target == redbutton) {
    if (current != -1) dot[current].setColor(dotcolor = Color.red);
    } else
    if (evt.target == greenbutton) {
    if (current != -1) dot[current].setColor(dotcolor = Color.green);
    } else
    if (evt.target == bluebutton) {
    if (current != -1) dot[current].setColor(dotcolor = Color.blue);
    } else
    if (evt.target == rectbutton) {
    if (current != -1)
    dot[current] = new RectDot(dot[current].labelOf(),
    dot[current].leftOf(),
    dot[current].topOf(),
    dot[current].widthOf(),
    dot[current].heightOf(),
    dot[current].colorOf(),
    dot[current].getFont());
    } else
    if (evt.target == circbutton) {
    if (current != -1)
    dot[current] = new CircDot(dot[current].labelOf(),
    dot[current].leftOf(),
    dot[current].topOf(),
    dot[current].widthOf(),
    dot[current].colorOf(),
    dot[current].getFont());
    } else
    if (evt.target == ovalbutton) {
    if (current != -1)
    dot[current] = new OvalDot(dot[current].labelOf(),
    dot[current].leftOf(),
    dot[current].topOf(),
    2*dot[current].widthOf(),
    dot[current].heightOf(),
    dot[current].colorOf(),
    dot[current].getFont());
    } else
    if (evt.target == objectbutton) {
    current = ndots;
    dot[ndots++] = new CircDot(namefield.getText(), 100, 100, 30,
    dotcolor, myfont);
    } else
    if (evt.target == smallbutton) {
    if (current != -1)
    {
    dot[current].setWidth(dot[current].widthOf()-1);
    dot[current].setHeight(dot[current].heightOf()-1);
    }
    } else
    if (evt.target == bigbutton) {
    if (current != -1)
    {
    dot[current].setWidth(dot[current].widthOf()+1);
    dot[current].setHeight(dot[current].heightOf()+1);
    }
    } else
    if (evt.target == textbutton) {
    if (current != -1) dot[current].setLabel(namefield.getText());
    } else
    if (evt.target == fontbutton) {
    if (current != -1) dot[current].setFont(myfont);
    }
    update(mainpanel.getGraphics());

    return super.action(evt, obj);
    }
    }
    [/code]
    here is the nearest thing i could find to what i want.... it gave me loads of ideas and helped me greatly just put it here in case any of ye wanna look..............


  • Advertisement
Advertisement