0

私のプロジェクトには2つのクラスがあります。Main.javaVillage.java。このクラスは、 のアプリケーション フレームに画像を描画するために、 にオブジェクトMain.javaを渡すことになっています。しかし、何も描画していないため、私のコードにはバグがあるようです..これが私のコードです...GraphicsContextVillage.javaMain.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);
       
    }
}

下に空のウィンドウが表示されます。助けてくださいここに画像の説明を入力

4

1 に答える 1

1

コードを調べたところ、誤解を受けていることに気付きました。キャンバスに描画できるようにするには、paintメソッドをオーバーライドしてそこで描画操作を行う必要があります。ヒントがさらに役立つことを願っています。

于 2021-03-27T15:56:56.637 に答える