0

I have two classes in my project. Main.java and Village.java. The class Main.java is supposed to pass a GraphicsContext object to Village.java so that it draws some image on the Application frame of Main.java. But it seems like my code has a bug because its not drawing anything..Here is my code...

//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();
    }
}

Now the other class with target draw(GraphicsContext)method is this one..The image defined in there is in the same directory as these class files.

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);
       
    }
}

Am getting the empty window below, please help meenter image description here

1 Answers1

0

When looking through your code, I noticed that you are subject to a misunderstanding. In order to be able to draw on a canvas, you have to override the paint method and do your drawing operations there. I hope the hint helps you further.

  • Am gonna need more information on that, to which class is the method going? And whose interface is being overriden? –  Mar 27 '21 at 17:29
  • There are several ways to override the `paint` method in a `Canvas`. You could do this inline while creating your `Canvas`. You could write yourself a derivative of `Canvas` that overwrites `paint`. Your Village class could also be derived from `Canvas`, so that you can overwrite the paint method there. I don't know enough about your project to give you final advice. The documentation and the examples for `Canvas` are freely available online. Here at SO, too, there are a lot of questions and solutions about the `Canvas class`. Just have a look around there! –  Mar 27 '21 at 18:34