mohit kumar
mohit kumar

Reputation: 35

How to draw a rectangle in Java?

I am working on an application that needs to draw shapes (rectangle etc) by searching an array like:

while(array!=null)
{
    if(array.equals("x"))
    then 
    drawRect(100,100,50,20);
}

Every rectangle must be drawn on a single Frame and with different coordinates.

Upvotes: 0

Views: 2165

Answers (2)

Andrew
Andrew

Reputation: 49656

There is an error in your code. The word then doesn't exist in Java.

while(array!=null) {
    if(array.equals("x")) {
         drawRect(100,100,50,20);
    }
}

There are so many example in Google. The best of all is Drawing Geometric Primitives by Oracle Tutorials.

public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    if (yourCondition) {
        g2.draw(new Rectangle2D.Double(x, y, rectwidth, rectheight));
    }
}

enter image description here

Upvotes: 1

Vamsi Ravi
Vamsi Ravi

Reputation: 1236

// Define an array
String[] array = {"a","b","x"};

for(int i=0; i < array.length; i++)
{
    if(array[i] == "x")
    {
        drawRect(100,100,50,20);
    }
}

Upvotes: 1

Related Questions