简体   繁体   中英

How do I implement a Graphics class in my main loop?

I've written a simulation in Java, and, with help, got a class that extends JPanel. A summary of the implementation is as follows:

public class Map extends JPanel {
BufferedImage truck1;

public Map() throws IOException {
    truck1 = ImageIO.read(getClass().getResource("Truck.png"));
}

protected void paintcomponent (Graphics g) {
    super.paintComponent(g);
    g.drawImage(truck1, 50, 100, 300, 300, this);
}

How do I go about initializing or calling this object in my main function, to make it draw the objects on screen?

How about using google: Swing tutorial

  1. Create a JFrame
  2. add your JPanel to it
  3. make the frame visible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM