我正在尝试绘制一个笔触为1像素的多边形。因为整个多边形都按100缩放,所以我将线宽设置为0.01。但是由于某种原因,多边形的绘制在屏幕上的线宽看起来是100像素而不是1。
我使用的GeneralPath是多边形形状。如果我使用相同的方法绘制Line2D形状,则会绘制细线。
g2d.scale(100, 100);
g2d.setStroke(new BasicStroke(0.01f));
g2d.draw(theShape);Run Code Online (Sandbox Code Playgroud)
新信息:如果删除setStroke行,我会正确地得到一条2像素的行,因为之前在Graphics2D对象上设置了0.02f的BasicStroke。
这是真正的setStroke行
g.setStroke(new BasicStroke((float) (1f / getRoot().scaleX)));
Run Code Online (Sandbox Code Playgroud)
以下代码产生如下所示的输出。您的代码中的其他地方必须有一个错误。也许scale您在问题中已省略的另一个呼叫:
import java.awt.*;
public class FrameTest {
public static void main(String[] args) throws InterruptedException {
JFrame f = new JFrame("Demo");
f.getContentPane().setLayout(new BorderLayout());
f.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
GeneralPath theShape = new GeneralPath();
theShape.moveTo(0, 0);
theShape.lineTo(2, 1);
theShape.lineTo(1, 0);
theShape.closePath();
g2d.scale(100, 100);
g2d.setStroke(new BasicStroke(0.01f));
g2d.draw(theShape);
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2821 次 |
| 最近记录: |