9

I need to create a rectangle object and then paint it to the applet using paint(). I tried

Rectangle r = new Rectangle(arg,arg1,arg2,arg3);

Then tried to paint it to the applet using

g.draw(r);

It didn't work. Is there a way to do this in java? I have scoured google to within an inch of its life for an answer, but I haven't been able to find an answer. Please help!

imulsion
  • 8,820
  • 20
  • 54
  • 84

3 Answers3

16

Try this:

public void paint (Graphics g) {    
    Rectangle r = new Rectangle(xPos,yPos,width,height);
    g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());  
}

[edit]

// With explicit casting
public void paint (Graphics g) {    
        Rectangle r = new Rectangle(xPos, yPos, width, height);
        g.fillRect(
           (int)r.getX(),
           (int)r.getY(),
           (int)r.getWidth(),
           (int)r.getHeight()
        );  
    }
Zac
  • 2,201
  • 24
  • 48
  • succinct and accurate – Dave Alperovich Oct 09 '14 at 15:40
  • 1
    Actually, that's wrong. The `get...` methods return `double`, but the `filRect` method requires `int`.... – Marco13 Apr 03 '18 at 21:30
  • @Marco13 Java uses auto-boxing to automatically convert between these types. Furthermore, in this example no loss of precision will occur since the Rectangle constructor accepts `int`s so `r.getX` and `r.getY` will always give you a double with no decimal part. – Zac Apr 04 '18 at 18:29
  • 1
    Assuming that this is a `java.awt.Rectangle`, the code does not compile, it's as simple as that. Boxing and precision are unrelated to that. The method signature simply does not match. Try it out. – Marco13 Apr 04 '18 at 18:48
  • @Marco13 Looks like over the last 6 years this answer has existed it no longer works. I will update my answer with explicit casting. – Zac Apr 04 '18 at 19:37
  • 1
    I'm pretty sure that it never worked like this. They neither changed the `fillRect` method nor the `Rectangle` class in this regard. In the best case, this may have been a copy+paste accident, but find it utterly confusing where the 14 upvotes came from...?! However, the version with the `(int)` casts will work, so this is fixed now. (I generally prefer `Graphics2D` and `g.draw(Shape)`, but that's not the point here) – Marco13 Apr 04 '18 at 21:20
5

You may try like this:

import java.applet.Applet;
import java.awt.*;

public class Rect1 extends Applet {

  public void paint (Graphics g) {
    g.drawRect (x, y, width, height);    //can use either of the two//
    g.fillRect (x, y, width, height);
    g.setColor(color);
  }

}

where x is x co-ordinate y is y cordinate color=the color you want to use eg Color.blue

if you want to use rectangle object you could do it like this:

import java.applet.Applet;
import java.awt.*;

public class Rect1 extends Applet {

  public void paint (Graphics g) {    
    Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
    g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    g.setColor(color);
  }
}       
heretolearn
  • 6,387
  • 4
  • 30
  • 53
0

Note:drawRect and fillRect are different.

Draws the outline of the specified rectangle:

public void drawRect(int x,
        int y,
        int width,
        int height)

Fills the specified rectangle. The rectangle is filled using the graphics context's current color:

public abstract void fillRect(int x,
        int y,
        int width,
        int height)
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39