java - 矩形厚度java

标签 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

最佳答案

您可以通过创建 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

关于java - 矩形厚度java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267790/

相关文章:

java - 通过构造函数传递对象引用

java - Guava 依赖的 Jar 大小 - 有小版本吗?

java - 旋转多个形状并保持关系

Javafx:如何使用源文件的驱动程序文件来显示创建的形状

javafx - 可调整大小和可移动的矩形

java - 矩形坐标未更新

java - 是否会在编译时优化局部变量的一次性使用?

java - 如何使用 Apache POI 制作垂直文本单元格样式?

python-2.7 - 如何用python在pdf中绘制形状?

java - Android 阶段性进展