使用g.drawRect
时如何增加矩形的厚度?我希望蓝色矩形有一条粗线。
这是我的代码 -
if(selectedApartment == 0)
{
g.setColor(Color.red);
g.fillRect (150, 120,aptWidth, aptHeight);
**g.setColor(Color.blue);
g.drawRect(150, 120, aptWidth, aptHeight);**
g.setColor(Color.black);
g.setFont(new Font("BoldFont",Font.BOLD,14));
g.drawString("1", 150+15, 120+25);
}
else if (selectedApartment != 0)
{
g.setColor(Color.red);
g.fillRect (150, 120,aptWidth, aptHeight);
g.setColor(Color.black);
g.drawRect(150, 120, aptWidth, aptHeight);
g.setColor(Color.black);
g.setFont(new Font("BoldFont",Font.BOLD,14));
g.drawString("1", 150+15, 120+25);
}
答案 0 :(得分:0)
您应该使用setStroke设置Graphics2D对象的笔划。
以下是程序中此代码段的示例:
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
cp.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.draw(new Line2D.Float(30, 20, 80, 90));
}
});
jf.setSize(300, 200);
jf.setVisible(true);
}
}
(请注意,setStroke方法在Graphics对象中不可用。您必须将其强制转换为Graphics2D对象。)