You have to cast the Graphics g to Graphics2D, then you can use Rectangle2D as a Shape. FYI Rectangle2D has two static constructors. Rectangle2D.Float and Rectangle2D.Double.
Shape is an interface, you would g2.fill( r ) or g2.stroke( r ); after you g2.setPaint( Color.red ); g2.setStroke( 2f ); Graphics2D is a whole new way of making graphics, more than I can explain here. In fact, the Graphics2D is a separate tutorial from the Java tuts.
IIWY, stick with the simple .awt Rectagle( x, y, w, h ); using int and Graphics. Graphics2D is fabulous (Adobe wrote the code), but also Graphics2D is a whole different way of composing graphics at type double precision.
You already have Graphics2D.
The easiest way to see Graphics2D... make a JFrame and add this
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class PrettyPanel
extends JPanel {
public PrettyPanel() {
setBorder( BorderFactory
.createBevelBorder(
BevelBorder.RAISED));
setBackground( Color.magenta );
setOpaque( true );
}
protected void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle r1 = getBounds();
Rectangle2D r2 = r1.getBounds2D();
double xLoc = r2.getWidth() / 2;
double yLoc = r2.getHeight() / 2;
double wide = 166;
double tall = 103;
xLoc -= wide / 2;
yLoc -= tall / 2;
r2.setRect(xLoc,yLoc,wide,tall);
g2.setPaint(Color.red);
g2.fill(r2);
g2.setStroke( new BasicStroke(3f));
g2.setColor( Color.cyan );
g2.draw(r2);
}
}