简体   繁体   中英

Paint and repaint and JFrame/JPanel creation

Before I start know this: I am extremely new to Java and programming. How would I properly draw the "grass.jpg" to the screen?

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.util.Random;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Game extends Canvas {
private static int Height = 300, Width = 600;  //25x50
private Random generator = new Random();

private String[][] TerrainList = new String[12][12];



public void GameSetup() {
    JFrame container = new JFrame("CARSSémon");

// get hold the content of the frame and set up the resolution of the game
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(Width,Height));
//panel.setLayout(null);

    //setBounds(0,0,800,600);
//panel.add(this);

// finally make the window visible
container.pack();
container.setResizable(false);
container.setVisible(true);
    container.setLocationRelativeTo(null);  //Centers screen

    container.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
    });


    PrepareTerrain();
    PreparePlayer();

}

public void PrepareTerrain() {

  for (int a=0; a<=11; a++){
   for (int b=0; b<=11; b++){
      TerrainList[a][b]="grass";   //Sets defult terrain of grass
   }
  }

  int BushLocationx = generator.nextInt(12);
  int BushLocationy = generator.nextInt(12);

  BushCheck(BushLocationx,BushLocationy);  //Checks to see if it can make bushs at the location

}

@Override
public void paint(Graphics g) {
super.paint(g);
// Draw an Image object
Image grass = new ImageIcon("grass.jpg").getImage();
Image bushes = Toolkit.getDefaultToolkit().getImage("bushes.jpg");
g.setColor(Color.BLACK);
g.drawImage(grass, 0, 0, null);
g.drawImage(grass, 0, 50, null);
g.drawImage(grass, 50, 0, null);
g.drawImage(grass, 200, 200, null);
}

public void DrawTerrain() {

  for (int r=0; r<=11; r++){
   for (int c=0; c<=11; c++){

   }
  }
}

private void BushCheck(int x, int y){

}

public void PreparePlayer() {

}

public static void main(String[] args) {

    Game G =new Game();
    G.GameSetup();

}

}

Now I obviously realize that this program has basically nothing implemented but I figured what's the point of starting to implement things if I could never even display any pictures?

My problem is that I can not figure out why the.jpgs aren't being displayed. Shouldn't the paint(); method be called when the JFrame and JPanel are created? The code is pretty messy, but I figured it would be best to include all of it.

In case this matters, this is eventually going to be a Pokemon like game, where the the run window is made up of many 16x16 pixel squares, that a player could move around on. Before starting any of that I wanted to experiment with outputting some images at random places. I've been reading similar questions and looking at examples, I just read a section of Java text on graphics but could only find information on loading images, not displaying through paint. If anyone could help by even pointing me in the right way, it would be greatly appreciated. (I realize that I will most likely completely need to restart, and are going about things completely wrong but anything you could do would help.)

I just read a huge section of Java text on graphics but could only find information on loading images, not displaying through paint.

For a Pokemon style game, I don't think using JLabel for each icon/image would provide any benefit. Instead:

  • Create a BufferedImage of the desired size of the game area.
  • Get Image instances for each of the smaller icons that you might need to paint (characters, elements in the terrain etc.) & paint them to the Graphics instance of the main BufferedImage 1 .
  • To display the main 'game' image there are two good options:
    1. Add the main image to a JLabel and add it to a JPanel , call label.repaint() .
    2. Paint the game image directly to the Graphics instance supplied to the paintComponent() method of a JComponent .

For the last part, I would recommend option 1.

1. EG

public void gameRenderLoop() {
    Graphics2D g2 = gameImage.createGraphics();
    g2.drawImage(playerImage, 22, 35, this);
    ...
    g2.dispose();
}

Examples of dealing with BufferedImage & Graphics

  1. Very simple example of painting an image .
  2. More complicated example dealing with text & clips .
  3. A slightly different tack (extending a JLabel ) showing image painting with transparency .

Don't reinvent the wheel. Use a JLabel with an ImageIcon to paint your image for you. Just add it to a JPanel and you're all set.

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