i want to create 2 items on screen. each item will be different color rect.
item1 = green rect
item2 = blue rect

if user get the 1st item. than it will print bullet1 1st image.
if user get the 2nd item. than it will print bullet2 2nd image.

bullet1 = 1st image
bullet2 = 2nd image

public void Item
{
   int x, y;
}

public void Bullet extends Item
{
  int dx = 1;
}

i am not sure how to create 2 different color rect. bc les say in item class

public void paint(...)
{
 g.setColor(Color.green);
 g.fillRect(x,y,width,height);
}

but this will only create one type of item, green. how can i do this without making another item class?

Recommended Answers

All 9 Replies

by instantiating that class two times.

Bullet bullet1 = new Bullet(Color.RED);
Bullet bullet2 = new Bullet(Color.GREEN);

but I do hope you understand why this part of your code makes no sense at all:

public void Bullet extends Item
{
  int dx = 1;
}

i should move the dx to Item class?

no. you are trying to let a method extend a class.

Do you inherit x, y, dx, dy from the Item class? If so you can just set dx in the default constructor for Bullet.

You haven't given enough info for a proper answer, but my instinct is that if you have two similar items then you should create two instances of a class.

ah ok i get it. thank just one last question.

item class

public class Item
{
}

for hp item i have two different types of hp. one is rect other is oval. oval will add 1 hp. and rect will full the jp bar.

class hp extends Item
{

  public void playerOvalCollision()
  {
  }

  public void playerRectCollision()
  {
  }
  ...
 pubic void paint(...)
 {
   if(...)//randomly draw shapes
     r.drawOval(x,y,);
   else
      g.drawRect(x,y,width,height);
 }
}

Should i have just one hp.java class or should i have two different classes, hp1, hp2.java. and they both extends Item.java?

different approaches.
you can have either an instance variable type, which's value determines what to do:

public void playerCollision(){
  if ( this.type == Types.OVAL){
    // perform oval
  }
  else if ( this.type == Types.RECTANGLE){
    // perform rectangle
  }
}

you can also turn hp in an interface, and have two implementing classes, Rectangle and Oval, ...

Once again - it depends on the complete requirements, which we don't have.
But every time I see code like stultiske's first approach above, or code like

   public void playerOvalCollision()
    {
    }
    public void playerRectCollision()
    {
    }
    ...
    pubic void paint(...)
    {
    if(...)//randomly draw shapes
    r.drawOval(x,y,);
    else
    g.drawRect(x,y,width,height);
    }

it just screams at me "TWO CLASSES"!

just a summarization of possibilities, didn't mean to claim that it's a better one :)
personally, I 'd prefer the interface with two implementing classes, but considering previous examples posted by the OP, I doubt he is very experienced with Java, and I have no idea whether or not he is familiar with interfaces.

Yes.
gamo06 has been working on stuff like this for a while now, with a number of related topics in this forum. I think he already has a superclass for things that move and need to be painted, so these may just be a couple more sublasses (?)

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.