JavaFx自定义网格绘制问题
我使用下面的代码在JavaFX画布上绘制垂直线。不知何故,最后一行(最后10%)的不透明度较低。我没有更改任何选项(gc上的转换/效果)。我附上一张截图供参考,你知道吗JavaFx自定义网格绘制问题,javafx,javafx-8,graphicscontext,Javafx,Javafx 8,Graphicscontext,我使用下面的代码在JavaFX画布上绘制垂直线。不知何故,最后一行(最后10%)的不透明度较低。我没有更改任何选项(gc上的转换/效果)。我附上一张截图供参考,你知道吗 public class ChartPane extends StackPane { Canvas canvas; public ChartPane() { setStyle("-fx-background-color: white;"); canvas = new Canv
public class ChartPane extends StackPane {
Canvas canvas;
public ChartPane() {
setStyle("-fx-background-color: white;");
canvas = new Canvas(getWidth(), getHeight());
getChildren().add(canvas);
widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
canvas.setWidth(newValue.intValue());
}
});
heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
canvas.setHeight(newValue.intValue());
}
});
}
@Override
protected void layoutChildren() {
super.layoutChildren();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.save();
gc.clearRect(0, 0, getWidth(), getHeight());
System.out.println(getWidth() + ", " + getHeight());
// vertical lines
gc.setStroke(Color.BLUE);
gc.setLineWidth(0.1);
gc.beginPath();
for(int i = 0 ; i < getWidth() ; i+=30){
gc.moveTo(i, 0);
gc.lineTo(i, getHeight() - (getHeight()%30));
gc.stroke();
}
// horizontal lines
gc.beginPath();
gc.setStroke(Color.RED);
for(int i = 30 ; i < getHeight() ; i+=30){
gc.moveTo(30, i);
gc.lineTo(getWidth(), i);
gc.stroke();
}
//gc.restore();
}
}
公共类图表窗格扩展了StackPane{
帆布;
公共图表窗格(){
setStyle(“-fx背景色:白色;”);
画布=新画布(getWidth(),getHeight());
getChildren().add(画布);
widthProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(observeValue我已将代码改写为strokeLine
,似乎有效:
@Override
protected void layoutChildren() {
super.layoutChildren();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());
// vertical lines
gc.setStroke(Color.BLUE);
for(int i = 0 ; i < getWidth() ; i+=30){
gc.strokeLine(i, 0, i, getHeight() - (getHeight()%30));
}
// horizontal lines
gc.setStroke(Color.RED);
for(int i = 30 ; i < getHeight() ; i+=30){
gc.strokeLine(30, i, getWidth(), i);
}
}
@覆盖
受保护的void layoutChildren(){
super.layoutChildren();
GraphicsContext gc=canvas.getGraphicsContext2D();
clearRect(0,0,getWidth(),getHeight());
//垂直线
gc.设定行程(颜色为蓝色);
对于(int i=0;i
仅供参考,我已经使用画布编写了一个可调整大小的网格:
更新 我从我的链接示例中复制,以使用锐线绘制,并显示在何处编辑线宽:
@Override
protected void layoutChildren() {
super.layoutChildren();
final int top = (int) snappedTopInset();
final int right = (int) snappedRightInset();
final int bottom = (int) snappedBottomInset();
final int left = (int) snappedLeftInset();
final int width = (int) getWidth() - left - right;
final int height = (int) getHeight() - top - bottom;
final double spacing = 30;
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());
gc.setLineWidth(1); // change the line width
final int hLineCount = (int) Math.floor((height + 1) / spacing);
final int vLineCount = (int) Math.floor((width + 1) / spacing);
gc.setStroke(Color.RED);
for (int i = 0; i < hLineCount; i++) {
gc.strokeLine(0, snap((i + 1) * spacing), width, snap((i + 1) * spacing));
}
gc.setStroke(Color.BLUE);
for (int i = 0; i < vLineCount; i++) {
gc.strokeLine(snap((i + 1) * spacing), 0, snap((i + 1) * spacing), height);
}
}
private double snap(double y) {
return ((int) y) + 0.5;
}
@覆盖
受保护的void layoutChildren(){
super.layoutChildren();
final int top=(int)snapdtopinset();
final int right=(int)SnapDrightInset();
final int bottom=(int)snapedbottominset();
final int left=(int)snapdleftinset();
最终整数宽度=(int)getWidth()-左-右;
最终整数高度=(int)getHeight()-顶部-底部;
最终双间距=30;
GraphicsContext gc=canvas.getGraphicsContext2D();
clearRect(0,0,getWidth(),getHeight());
gc.setLineWidth(1);//更改线宽
最终整数hLineCount=(整数)数学楼层((高度+1)/间距);
最终整数vLineCount=(整数)数学楼层((宽度+1)/间距);
gc.设定行程(颜色为红色);
对于(int i=0;i
查看将画布作为子元素添加到何处会很有帮助。画布的任何父元素都可能引入我们在屏幕截图上看到的效果。编辑问题,请检查。是否可以将gc.stroke()放置在笔划之外,以便我可以准备路径并调用stroke()只有一次?现在它确实绘制,但不是用setStroke()方法指定的颜色。嘿,这画的线很好…谢谢。但我似乎无法设置线宽,我的意思是,如果我在strokeLine之前添加gc.setLineWidth(),它就不会绘制颜色!你能帮我吗?既然你用strokeLine画的是一条直线(x1,y1,x2,y2)
,线条的宽度由坐标中的差值构成(x2-x1=水平线条宽度,y2-y1=垂直线条宽度)对于线宽,我指的是笔划宽度,如何使用笔划式方法绘制更细的线条?有什么线索吗?在这里找到了线条锐度的解决方案:线条实际上是1px,但看起来模糊。答案仍然与我之前的评论相同。但为了澄清这一点,我将编辑我的答案。。