Be a Supporter!

Java applet problem

  • 1,004 Views
  • 2 Replies
New Topic Respond to this Topic
Rammer
Rammer
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 33
Programmer
Java applet problem 2008-05-03 22:00:05 Reply

ok, so, let me get this out of the way: it is, indeed, homework. however, i've done most of the work and i'm just having one small problem with it, so please don't give me that "we don't do your homework" bullshit.

this is my assignment, as quoted from my book: "Write an applet that displays a graphical seating chart for a dinner party. Create a class called Diner (as in one who dines) that stores the person's name, gender, and location at teh dinner table. A diner is graphically represented as a circle, color-coded by gender, with the person's name printed in the circle."

i have 3 classes: Main, Diner, and Table. here's all the code.

Main.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;

public class Main
        extends Applet {

    Color col;
    Diner[] diners =new Diner[12];
    int tx = 100, ty = 100, length = Diner.getRadius() * 3, cw = Diner.getRadius(), sbtac = 7, persons = 0;
    Random rand = new Random();
    Table tbl = new Table(length, tx, ty, new Color(8404992));
    String g;

    public void init() {
        setBackground(Color.GRAY);
        setSize(600, 600);
        for (int i = 0; i < 12; i++) {
            int reee = rand.nextInt(9);
            g = (rand.nextInt(9) < 4) ? "male" : "female";
            diners[i] = new Diner(g, reee);
        }
    }

    public void paint(Graphics page) {
        int x, y;
        tbl.paint(page);
        //top
        for (int i = 0; i < diners.length / 4; i++) {
            page.setColor(diners[persons].getColor());
            y = ty - cw - sbtac;
            x = tx + cw * i;
            diners[i].paint(page, x, y);
            persons++;

        }
        //left
        for (int i = 0; i < diners.length / 4; i++) {
            page.setColor(diners[persons].getColor());
            x = tx - cw - sbtac;
            y = ty + cw * i;
            persons++;
            diners[i].paint(page, x, y);
        }
        //right
        for (int i = 0; i < diners.length / 4; i++) {
            page.setColor(diners[persons].getColor());
            x = tx + length + sbtac;
            y = ty + cw * i;
            persons++;
            diners[i].paint(page, x, y);
        }
        //bottom
        for (int i = 0; i < diners.length / 4; i++) {
            page.setColor(diners[persons].getColor());
            y = ty + length + sbtac;
            x = tx + cw * i;
            diners[i].paint(page, x, y);
            persons++;
        }
    }
}

Diner.java

import java.awt.*;

public class Diner {

    private String g,  n;
    private static int r = 75;
    private String[] names = {"Face", "Bo-Peep", "Laurence", "Billy", "Phil", "Alex",
        "Quincy", "Phil 2", "Gilfred", "Milfred"
    };
    Color c;

    public Diner(String gender, int randnum) {
        g = gender;
        n = names[randnum];
        System.out.println(n);
        c = (g.equals("male") == true) ? Color.BLUE : Color.PINK;
    }

    public void paint(Graphics page, int lx, int ly) {
        page.fillOval(lx, ly, r, r);
        page.setColor(Color.black);
        page.drawString(n, lx + r / 4, ly + r / 2);
    }

    public Color getColor() {
        return c;
    }

    public static int getRadius() {
        return r;
    }
}

Table.java

import java.awt.*;
public class Table {
  int x, y, l;
  Color c;
  public Table(int length, int ex, int wy, Color col) {
    x = ex;
    y = wy;
    l = length;
    c = col;
  }
  public void paint(Graphics page) {
    page.setColor(c);
    page.fillRect(x, y, l, l);
    
  }

}

i'm sure my code isn't as efficient as it could be, but i'm stumped.

my problem here is that i get 2 or 3 names recycled inside the circles. in the output, though, it shows that all the names are being used (or at least, more than 3). so why is it outputting something different than what is being displayed in the circle?

thanks for any help. i'd show a picture, but i can't upload anything anywhere, for some reason.


snyggys

Rammer
Rammer
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 33
Programmer
Response to Java applet problem 2008-05-03 22:03:39 Reply

this ALWAYS happens <_> i fixed it. stupid small stuff.

in my paint() function in Main.java, i put diners[i].paint(page, x, y);, when i should have put diners[persons].paint(page, x, y);

sigh

snyggys

helloNeighbour
helloNeighbour
  • Member since: Mar. 7, 2013
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Java applet problem 2013-03-07 22:51:51 Reply

At 5/3/08 10:03 PM, Rammer wrote: this ALWAYS happens <_> i fixed it. stupid small stuff.

in my paint() function in Main.java, i put diners[i].paint(page, x, y);, when i should have put diners[persons].paint(page, x, y);

sigh

ummm.. can i see the edited bit of the program and wen i try to run it.. it say <no main class found>