如何解决ContextGraphics 绘制图像,在 Java 中什么都不绘制
我的项目中有两个类。 Village.java
和 Main.java
。类GraphicsContext
应该将Village.java
对象传递给Main.java
,以便它在//This is the main class and it extends Application class for drawing a frame object
public class Main extends Application{
//Define the main method
public static void main(String args[]){
//Draw the frame
launch(args);
}
//This method override and passes a GraphicsContext object to a method in another class for drawing image
@Override
public void start(Stage stage) throws Exception {
//Set the title of the application window
stage.setTitle("Village");
Group root= new Group();
//Define a new canvas object
Canvas newcanvas= new Canvas();
//Get GraphicsContext object from the canvas
GraphicsContext gc=newcanvas.getGraphicsContext2D();
//Get an object of the class whose draw() method we wish to access
Village myvillage=Village.create();
//Call the method and pass GraphicsContext object as a parameter
myvillage.draw(gc);
root.getChildren().add(newcanvas);
stage.setScene(new Scene(root));
stage.show();
}
}
的应用程序框架上绘制一些图像。但似乎我的代码有一个错误,因为它没有绘制任何东西......这是我的代码......
draw(GraphicsContext)
现在另一个具有目标 same directory
方法的类是这个。在那里定义的图像作为这些类文件在 public class Village{
// Y co ordinate of where to draw the image
static final double Y_VILLAGE=30;
public void draw(GraphicsContext r){
Image house=new Image("file:House.png",100,250,false,false);
//Define the xPosition for this House object
MyBuilding y=(MyBuilding)MyBuilding.create();
//Use the GraphicsContext to draw on the Canvas,we get an X position on where to draw from user dialog
//Draw the image
r.drawImage(house,y.getXposition(),Village.Y_VILLAGE);
}
}
中。
{{1}}
解决方法
在查看您的代码时,我注意到您存在误解。为了能够在画布上绘图,您必须覆盖 paint
方法并在那里进行绘图操作。
我希望提示可以帮助您进一步。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。