Results 1 to 6 of 6
Thread: Classes Problem
- 11-05-2011, 02:49 PM #1
Member
- Join Date
- Nov 2011
- Location
- Bolzano, Italy
- Posts
- 3
- Rep Power
- 0
Classes Problem
Hi, i'm new to java...
i have an applet assignment to do, and i got stuck..
"• Write a class Diner with the following features:
• attributes name (String), gender (Color),
locationX, locationY (int)
• A constructor
Diner(String dinerName, Color genderColor, int x, int y)
that sets up this diner with the specified characteristics.
• A method draw(Graphics page) that draws the diner.
Then write a class DinnerTable
extending Japplet that:
• creates 4 diners;
• draw the table and the diners
as shown in the picture.
"
so i created this program:
so i have to create the 4 diners with the second class, from the first one...Java Code:package diner; import java.awt.Graphics; import java.awt.Color; public class Diner { public enum DinerGender {male, female} private DinerGender gender; private String name; private int locationX, locationY; public Diner(String dinerName, DinerGender genderInit, int x, int y) { name = dinerName; gender = genderInit; locationX = x; locationY = y; } public void draw (Graphics page, int leftedge) { final int OVAL_WIDTH = 75; final int OVAL_HEIGHT = 75; page.setColor(Color.black); page.drawString(name, locationX, locationY); page.setColor(Color.cyan); page.fillOval(100, 25, OVAL_WIDTH, OVAL_HEIGHT); } } package diner; import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Color; public class DinnerTable extends JApplet { private final int APPLET_HEIGHT = 300; private final int APPLET_WIDTH = 400; private Diner d1,d2,d3,d4; public void init() { d1 = new Diner ("Mike", Diner.DinerGender.male, 100, 60); d2 = new Diner ("Sarah", Diner.DinerGender.female, 240, 60); d3 = new Diner ("John", Diner.DinerGender.male, 100, 260); d4 = new Diner ("Anna", Diner.DinerGender.female, 240, 260); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); } public void paint (Graphics page) { page.setColor (Color.gray); page.fillRect (100, 110, 175, 125); d1.draw(page); d2.draw(page); d3.draw(page); d4.draw(page); } }
i have to specify if it's a girl or a boy..and so the program should create a pink or a cyan oval...with a name or it
and i can't understand how to do that...
and also about the position of the ovals, don't know how to specify that..
- 11-05-2011, 03:00 PM #2
Re: Classes Problem
When you create the class object, you should specify: boy or girl so the drawing method knows what color to use.have to specify if it's a girl or a boy..and so the program should create a pink or a cyan oval...with a name or it
and i can't understand how to do that...
I see that your code already does that in the constructor call. Can you explain what your problem is?
Read the API doc for the Graphics class's drawing methods. The doc will tell you how to set the position of the drawing.also about the position of the ovals
- 11-05-2011, 03:20 PM #3
Member
- Join Date
- Nov 2011
- Location
- Bolzano, Italy
- Posts
- 3
- Rep Power
- 0
Re: Classes Problem
i know how to create the constructor, but then i don't know how to create the pink or the cyan oval...
and for these pink or cyan to set the position...
- 11-05-2011, 03:23 PM #4
Re: Classes Problem
Look at your code. It currently (lines 33-34) draws one cyan oval. You need to add logic there to decide on color and location for the oval being drawn.i don't know how to create the pink or the cyan oval..
- 11-05-2011, 03:35 PM #5
Member
- Join Date
- Nov 2011
- Location
- Bolzano, Italy
- Posts
- 3
- Rep Power
- 0
Re: Classes Problem
...and how do i do that?
- 11-05-2011, 03:37 PM #6
Similar Threads
-
Complicated problem between classes
By uhertz in forum New To JavaReplies: 3Last Post: 06-20-2011, 01:58 AM -
Problem with classes
By liakos in forum NetBeansReplies: 14Last Post: 03-10-2011, 07:34 PM -
inner classes problem
By smallmos1 in forum New To JavaReplies: 7Last Post: 11-18-2010, 03:07 PM -
problem with using classes and output
By sjaakie in forum New To JavaReplies: 3Last Post: 10-10-2010, 01:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks