[java] How to create a Rectangle object in Java using g.fillRect method

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!

This question is related to java graphics applet drawing drawrectangle

The answer is


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)

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);
  }
}       

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to graphics

How to play or open *.mp3 or *.wav sound file in c++ program? How to use graphics.h in codeblocks? How do I get the height and width of the Android Navigation Bar programmatically? How to Change Font Size in drawString Java How can I produce an effect similar to the iOS 7 blur view? Creating a blurring overlay view Saving images in Python at a very high quality Android: Background Image Size (in Pixel) which Support All Devices How to create a Rectangle object in Java using g.fillRect method Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio

Examples related to applet

Java unsupported major minor version 52.0 Unsupported major.minor version 52.0 "application blocked by security settings" prevent applets running using oracle SE 7 update 51 on firefox on Linux mint Can not run Java Applets in Internet Explorer 11 using JRE 7u51 Run local java applet in browser (chrome/firefox) "Your security settings have blocked a local application from running" Java Error: "Your security settings have blocked a local application from running" How to create a Rectangle object in Java using g.fillRect method socket programming multiple client to one server java.io.IOException: Invalid Keystore format How can I open Java .class files in a human-readable way?

Examples related to drawing

How to draw a checkmark / tick using CSS? Draw in Canvas by finger, Android How to create a Rectangle object in Java using g.fillRect method c# write text on bitmap Circle drawing with SVG's arc path Creating an empty bitmap and drawing though canvas in Android Drawing circles with System.Drawing Android: How to overlay a bitmap and draw over a bitmap? How To: Best way to draw table in console app (C#) Draw radius around a point in Google map

Examples related to drawrectangle

How to create a Rectangle object in Java using g.fillRect method