矩形厚度java

时间:2015-03-25 22:32:37

标签: java shapes rectangles outline

我需要在选中时以蓝色1矩形勾勒出轮廓。代码可以工作,但我不能很好地看到蓝色轮廓。什么是使轮廓更粗的最简单的方法。在它上面绘制另一个形状或其他东西。

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

2 个答案:

答案 0 :(得分:3)

您可以通过创建Graphics2D对象并设置其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

答案 1 :(得分:0)

也许你可以在第一个矩形内立即绘制第二个矩形。或者是其中的多个。

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