| | |
object for an array
![]() |
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
I would like to draw an array of robots. How do i complete this class to draw the head, body, legs of a robot given that the robot is to be drawn within an invisible rectangular Container. The position and dimensions of the
Container are given as parameters to the constructor. The width and height of the
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes. This is what i have so far but i odnt know how to get the head, body, legs of the robot.
private Triangle head;
private Rectangle body, leg1, leg 2;
public Suspect(int xpos, int ypos, int width, int height) {
super();
this.setBounds(xpos, ypos, width, height);
int head_width = width;
int head_height = head_width;
int xc = width/2;
int yc = width/2;
head = new Triangle(xc,yc, , width, width);
body = new Rectangle();
leg1=new Rectangle();
leg2=new Triangle();
kindly help me
Container are given as parameters to the constructor. The width and height of the
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes. This is what i have so far but i odnt know how to get the head, body, legs of the robot.
private Triangle head;
private Rectangle body, leg1, leg 2;
public Suspect(int xpos, int ypos, int width, int height) {
super();
this.setBounds(xpos, ypos, width, height);
int head_width = width;
int head_height = head_width;
int xc = width/2;
int yc = width/2;
head = new Triangle(xc,yc, , width, width);
body = new Rectangle();
leg1=new Rectangle();
leg2=new Triangle();
kindly help me
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
I would like to draw an array of robots. How do i complete this class to draw the head, body, legs of a robot given that the robot is to be drawn within an invisible rectangular Container. The position and dimensions of the
Container are given as parameters to the constructor. The width and height of the
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes. This is what i have so far but i odnt know how to get the head, body, legs of the robot.
private Triangle head;
private Rectangle body, leg1, leg 2;
public Suspect(int xpos, int ypos, int width, int height) {
super();
this.setBounds(xpos, ypos, width, height);
int head_width = width;
int head_height = head_width;
int xc = width/2;
int yc = width/2;
head = new Triangle(xc,yc, , width, width);
body = new Rectangle();
leg1=new Rectangle();
leg2=new Triangle();
kindly help me
Can't really give you more specific advice unless you post your full Robot class and at least part of your Triangle, Rectangle classes, etc.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
This is the triangle class:
Java Syntax (Toggle Plain Text)
public class Triangle extends JComponent { private int[] xPoints, yPoints; public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) { super(); int minX = (int) Math.min(Math.min(x1,x2),x3); int maxX = (int) Math.max(Math.max(x1,x2),x3); int minY = (int) Math.min(Math.min(y1,y2),y3); int maxY = (int) Math.max(Math.max(y1,y2),y3); int twidth = maxX - minX + 1; int theight = maxY - minY + 1; xPoints = new int[] {x1-minX, x2-minX, x3-minX}; yPoints = new int[] {y1-minY, y2-minY, y3-minY}; this.setBounds(minX, minY, twidth, theight); this.setBackground(Color.black); } public void paint(Graphics g) { g.setColor(this.getBackground()); g.fillPolygon(xPoints, yPoints, 3); this.paintChildren(g); } } This s the rectangle class public class Rectangle extends JComponent { public Rectangle(int x, int y, int w, int h) { super(); setBounds(x, y, w, h); setBackground(Color.black); } public void paint(Graphics g) { g.setColor( getBackground() ); g.fillRect(0, 0, getWidth(), getHeight()); paintChildren(g); } } This is the robot class: public class Suspect extends Container { // INCOMPLETE - define appropriate private variables here public Suspect(int xpos, int ypos, int width, int height) { super(); this.setBounds(xpos, ypos, width, height); int head_width = width; int head_height = head_width; int xc = width/2; int yc = width/2; // INCOMPLETE - add the Suspect's head, body, and legs here } private void makeFace(Triangle head, int width, int height) { int xc = width / 2; int yc = height / 4; int nose_diameter = width / 5; int mouth_width = width/3; int mouth_height = mouth_width/4; int eye_sep = width/2; int eye_diam = mouth_height; // INCOMPLETE - add the suspect's nose, mouth, and eyes here } public void setColor(Color eyeCol, Color mouthCol, Color noseCol) { } }
Last edited by ~s.o.s~; May 4th, 2008 at 3:05 pm. Reason: Added code tags, learn to use them. First and last warning.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
JAVA Syntax (Toggle Plain Text)
public class Triangle extends JComponent { private int[] xPoints, yPoints; public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) { super(); int minX = (int) Math.min(Math.min(x1,x2),x3); int maxX = (int) Math.max(Math.max(x1,x2),x3); int minY = (int) Math.min(Math.min(y1,y2),y3); int maxY = (int) Math.max(Math.max(y1,y2),y3); int twidth = maxX - minX + 1; int theight = maxY - minY + 1; xPoints = new int[] {x1-minX, x2-minX, x3-minX}; yPoints = new int[] {y1-minY, y2-minY, y3-minY}; this.setBounds(minX, minY, twidth, theight); this.setBackground(Color.black); } public void paint(Graphics g) { g.setColor(this.getBackground()); g.fillPolygon(xPoints, yPoints, 3); this.paintChildren(g); } } // This s the rectangle class public class Rectangle extends JComponent { public Rectangle(int x, int y, int w, int h) { super(); setBounds(x, y, w, h); setBackground(Color.black); } public void paint(Graphics g) { g.setColor( getBackground() ); g.fillRect(0, 0, getWidth(), getHeight()); paintChildren(g); } } // This is the robot class: public class Suspect extends Container { // INCOMPLETE - define appropriate private variables here public Suspect(int xpos, int ypos, int width, int height) { super(); this.setBounds(xpos, ypos, width, height); int head_width = width; int head_height = head_width; int xc = width/2; int yc = width/2; // INCOMPLETE - add the Suspect's head, body, and legs here } private void makeFace(Triangle head, int width, int height) { int xc = width / 2; int yc = height / 4; int nose_diameter = width / 5; int mouth_width = width/3; int mouth_height = mouth_width/4; int eye_sep = width/2; int eye_diam = mouth_height; // INCOMPLETE - add the suspect's nose, mouth, and eyes here } public void setColor(Color eyeCol, Color mouthCol, Color noseCol) { } }
Code tags, use them and formatting and your code will be much easier to read:
[code=JAVA]
// paste code here
[/code]
I see you have paint functions in Rectangle and Triangle. You need a paint function in your Robot class as well. This paint function in your Robot class will call the paint functions from Rectangle and Triangle. I would fill in the data member section of your Suspect class now rather than try to code functions. This will force you to decide exactly what data types go with what. Head, Arm, Leg, etc. should be data members of Suspect. I saw "legs" in your earlier code and assumed that was part of the Robot/Suspect class, and it should be, but it isn't yet, so if you code using legs, you are going to get errors. I think if you spend time making a good design of where everything should be, THEN do the coding, you'll have an easier time coding.
Don't try to create an array of Robots yet. Just create one and paint it to the screen and get it working the way you want. Before trying that, make sure that you can paint rectangles and triangles correctly to the screen. Your robot is simply a collection of triangles and rectangles.
Consider making your data members public until you get it working. That'll prevent you from having to write a bunch of "get" and "set" functions. A lot of people might disagree with me on this one and their point would probably be that it's better coding practice to assign data members to be private and use "get" and "set", so you should get in the habit, and you'll probably have to change them to that anyway, so why not just do it from the beginning? My feeling is that if you make them public, that's one less set of potential errors you'll have to debug as you are going, and you can concentrate faster on getting the robot to do what you want rather than public/private issues, etc.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
Thanks for your response. I find it much easier to declare objects as private. What I find difficult at this time is actually creating the robot itself. Giving the following parameters "The width and height of the
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes". the head of the robot is a triangle with the broadest side on top, the body is a rectangle, the legs are 2 rectangles. Kindly reply
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes". the head of the robot is a triangle with the broadest side on top, the body is a rectangle, the legs are 2 rectangles. Kindly reply
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Thanks for your response. I find it much easier to declare objects as private. What I find difficult at this time is actually creating the robot itself. Giving the following parameters "The width and height of the
robot’s triangular “head” = the width of the Container. The width of the robot’s “body”
is the same as the width of the head at its widest point. The height of the head, body, and
“legs” sum to the given height of the Container,with the height of the body being equal
to the height of the legs. The width of the base of each leg is 1/4 of the width of the body.
Using the given Triangle and Rectangle classes". the head of the robot is a triangle with the broadest side on top, the body is a rectangle, the legs are 2 rectangles. Kindly reply
I'm confused about the classes. I see a Suspect class, but no Robot class. I see words above referring to a Robot class, and I see a comment above the Suspect class saying that it is the Robot class. I'm assuming that you renamed the Robot class to call it the Suspect class. I see paintChildren functions in Rectangle and Triangle, but I don't know what they are for. That might not be important.
I don't think you can go any further until you define your Suspect class data members. All or most of these data members will be of type Triangle or type Rectangle. Define them as data members of your Suspect class. Something like this:
JAVA Syntax (Toggle Plain Text)
private Triangle head; private Rectangle body; private Rectangle rightArm; // etc.
There are a lot of ways to do this. Pick one and try to stick with it. like I said, I generally take the approach of declaring all the body parts as data members of the Suspect class. I really don't think you can code much further until you make a real layout of what all the data members are and what classes they belong too. All of the math involved in calculating the correct coordinates and how to do that should come after you have decided on your class structure.
Last edited by VernonDozier; May 4th, 2008 at 4:23 pm.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
Thank you Vernon for your help,
I have been able to get the program running. I created the robots which I named suspect and have an array of them with different color of noses. I now want to use a HashSet to save a refernce to each suspect(robot) as it is drawn and add instructions to the leftAction method on my 3button window in order to iterate over all suspects stored in the HashSet and to remove from the display those that have yellow nose
I implemented a boolean method which returns true if the nose is yellow and false otherwise
but i don't really know how to use the HashSet method.
. .
This is what i hav done so far. Pardon me, I'm very new in writing programs in java.
Thanks
I have been able to get the program running. I created the robots which I named suspect and have an array of them with different color of noses. I now want to use a HashSet to save a refernce to each suspect(robot) as it is drawn and add instructions to the leftAction method on my 3button window in order to iterate over all suspects stored in the HashSet and to remove from the display those that have yellow nose
I implemented a boolean method which returns true if the nose is yellow and false otherwise
but i don't really know how to use the HashSet method.
.
Java Syntax (Toggle Plain Text)
for (int col=0; col<nr; col++) { for (int row=0; row<nc; row++) { if(suspect[col][row].nose(Color.yellow)){ win.remove(suspect[col][row]);
This is what i hav done so far. Pardon me, I'm very new in writing programs in java.
Thanks
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Thank you Vernon for your help,
I have been able to get the program running. I created the robots which I named suspect and have an array of them with different color of noses. I now want to use a HashSet to save a refernce to each suspect(robot) as it is drawn and add instructions to the leftAction method on my 3button window in order to iterate over all suspects stored in the HashSet and to remove from the display those that have yellow nose
I implemented a boolean method which returns true if the nose is yellow and false otherwise
but i don't really know how to use the HashSet method.
..Java Syntax (Toggle Plain Text)
for (int col=0; col<nr; col++) { for (int row=0; row<nc; row++) { if(suspect[col][row].nose(Color.yellow)){ win.remove(suspect[col][row]);
This is what i hav done so far. Pardon me, I'm very new in writing programs in java.
Thanks
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
Thanks a lot Vernon,
I was able to create an instance of a suspent, then i drew a nested loop to add an array of suspects. and randomly added color to the nose, eyes of the suspect.I now want to make the suspects with yellow noses be removed when i click on the left button and to be added when i click again.
I was able to create an instance of a suspent, then i drew a nested loop to add an array of suspects. and randomly added color to the nose, eyes of the suspect.I now want to make the suspects with yellow noses be removed when i click on the left button and to be added when i click again.
![]() |
Similar Threads
- Problem declaring class object array (C++)
- Maximum Array (C#)
- encapsulating an array in a class? (C#)
- Array["name"] instead of Array[int] (C#)
- single array objects (Java)
- Array of Class Objects? (C++)
- Output 2D array? (C++)
- Help : Pointers to array of class objects . (C++)
- Help with copying an array into a struct (C)
Other Threads in the Java Forum
- Previous Thread: JAVA is good with databases or NOT...?
- Next Thread: JAXB
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows






