一般来说,JavaFX中的绘画机制已经转向更基于事件的方法。要遵循JavaFX方式,您应该查看Timeline或AnimationTimer,并且只在实际图像数据更改时更新显示。
但是,你可以使用旧的方式摆在JavaFX中,如果你喜欢:
public class MyPane extends Pane {
private final Canvas canvas;
public MyPane() {
canvas = new Canvas(getWidth(), getHeight());
getChildren().add(canvas);
widthProperty().addListener(e -> canvas.setWidth(getWidth()));
heightProperty().addListener(e -> canvas.setHeight(getHeight()));
}
@Override
protected void layoutChildren() {
super.layoutChildren();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());
gc.setFill(Color.RED);
gc.fillRect(10, 10, getWidth() - 20, getHeight() - 20);
// Paint your custom image here:
gc.drawImage(someImage, 0, 0);
}
}
上面的代码将是这个Swing代码等价:
public class MyPanel extends JPanel {
private static final long serialVersionUID = -969772195113348076L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(java.awt.Color.RED);
g.fillRect(10, 10, getWidth() - 20, getHeight() - 20);
// Paint your custom image here:
g.drawImage(someImage, 0, 0, null);
}
}
我不知道你的意思是“'ImageView'正在使用css”。您可以使用['WritableImage'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/WritableImage.html)作为提供给“ImageView”的图像,或者使用['Canvas'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/Canvas.html)及其[[GraphicsContext]](http:// docs。 oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html)。 'GraphicsContext'有很多用于绘制图像的公共API。 –
什么是图像格式? – Mansueli
@James_D @Kyllopardiun我没有读得太深,但似乎'ImageView'通过'-fx-image' CSS属性显示自己的图像,但实际的绘画是在其他地方完成的。它可能在'impl_'方法之一中,但没有文档我无法理解它们。我有一个代表图像数据的“int”数组。这是我正在编写的图像编辑器;我可以更新图像视图以反映图像数据更改的速度是一个问题,这就是为什么我不希望在两种格式之间转换的原因。 – sorbet