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

Recommended Answers

All 8 Replies

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

I am assuming that the above code is part of a Robot class? If not, I suggest you create one. I think your idea of separating the robot into several different body parts is a good one. Before coding, I sugggest you figure out exactly what you want to store in the Robot class, which it looks like you have already started. I imagine you are displaying this using in a JPanel or a JFrame or something? I'd have Robot object have a pair of coordinates, say the upper left corner of the invisible rectangle you refer to. You'd also want to store the size of that rectangle in your Robot class. Knowing the size and location of the the rectangular box will allow you to calculate the coordinates of the Robot parts, which seem like classes that you have already written like Triangle and Rectangle. I imagine those classes store sizes and coordinates as well. You'll determine those sizes and coordinates relative to the Robot's size and coordinates. When you get all of the individual sizes and coordinates figured out and stored, I imagine your Robot class will have a paintComponent function, as will your Triangle and Rectangle classes. You'll probably want to call the paintComponent functions of Triangle, Rectangle, etc.

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.

This is the triangle class:

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) {



   }

}
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.

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

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

Making the data members private is fine. But before coding, declare the data members, private or not. Then code.

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:

private Triangle head;
private Rectangle body;
private Rectangle rightArm;
// etc.

It might be a little more complicated. Head may not be a Rectangle, but instead an assortment of Rectangles and Triangles. If so, define rightEye, leftEye, etc. as Triangles, Rectangles and make THEM private data members of Suspect and don't have Head be a data member. Or make a Head class which contains eyes, nose, etc. Looks to me like you are defining the mouth, eyes, etc. in Suspect, but not as data members. There are ways to do that, for sure, but I'm not following what you are trying to accomplish. I suppose it depends on how complicated this robot is going to get. The long and short of it is you are going to end up with a bunch of Triangles and Rectangles, each with sizes/coordinates, and those will make up your robot. You'll need to calculate the coordinates and sizes of these Triangles and Rectangles. You either need to store them somewhere or calculate them when you paint. My preference would be to store them all as data members of one class or the other, then call paint. paint would do just that, paint the JFrame or whatever. All calculations would have already been done and stored ahead of time. paint wouldn't have to do any of it.

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.

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.
.

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

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.
.

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

Glad you got it working. I can't help you with the HashSet since I've never used them. Even if I had used them before, I wouldn't be able to help you because all you've posted is the loop code. I assume win is the HashSet, What you've posted looks fine to me, but it's hard to say since you haven't posted the related code, so we'd have no idea how everything fits together. Keep in mind that while you are intimately familiar with your project, all anyone here knows is what you have posted. You also haven't mentioned what is working and what isn't regarding the HashSet and what your specific question is regarding HashSet. Anyway, good luck with the project. Like I said, I can't help with the HashSet.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.