Dcurrie1
Dcurrie1

Reputation: 93

Rectangle thickness java

I need to outline in blue 1 rectangle at a time when selected. The code works but I cant see the blue outline very well. what is the easiest way to make the outline thicker. Wither that be drawing another shape over it or something else.

private void displayBlock(Graphics g)
{
    for(int i = 0; i < 10; i++)
    {

        if (occupant[i].equals("Empty"))
        {
            //Sets the rectangle red to show the room is occupied
            g.setColor(emptyColor);
            g.fillRect(150+(i*50), 120, aptWidth, aptHeight);

        }
        else
        {
            //Sets the rectangle green to show the room is free
            g.setColor(Color.red);
            g.fillRect(150+(i*50), 120, aptWidth, aptHeight);               
        }
        if (i == selectedApartment)
        {

            g.setColor(Color.blue);
            g.drawRect(150+(i*50), 120, aptWidth, aptHeight);

        }
        else
        {
            g.setColor(Color.black);
            g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
        }

        // Draws the numbers in the Corresponding boxes
        g.setColor(Color.black);
        g.drawString(Integer.toString(i+1), 150+(i*50)+15, 120+25);
    }

    //Drawing the key allowing the user to see what the colours refer to
    g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    g.drawString("Key", 20,120);
    g.drawString("Occupied",60,150);
    g.drawString("Empty", 60, 170);

    // Drawing the boxes to show what the colours mean in the key             
    g.setColor(Color.red);
    g.fillRect(20, 130, 25, 25);
    g.setColor(Color.black);
    g.drawRect(20, 130, 25, 25);
    g.setColor(Color.green);
    g.fillRect(20,150,25,25);
    g.setColor(Color.black);
    g.drawRect(20,150,25,25);

} // End of displayBlock

Upvotes: 1

Views: 3837

Answers (2)

TNT
TNT

Reputation: 2920

You can set the thickness of the rectangle by creating a Graphics2D object and setting its stroke.

java.awt.Graphics2D g2 = (java.awt.Graphics2D) g.create();
g2.setStroke(new java.awt.BasicStroke(3)); // thickness of 3.0f
g2.setColor(Color.blue);
g2.drawRect(10,10,50,100); // for example

Upvotes: 4

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79867

Maybe you could draw a second rectangle immediately inside the first one. Or multiple of them.

public static void drawRectangle(Graphics g, int x, int y, int width, int height, int thickness) {
    g.drawRect(x, y, width, height);
    if (thickness > 1) {
        drawRectangle(g, x + 1, y + 1, width - 2. height - 2, thickness - 1);
    }
}

Upvotes: 0

Related Questions