Painting and redrawing and creating a JFrame / JPanel

Before I begin to understand this: I am extremely new to Java and programming. How do I draw "grass.jpg" on the screen correctly?

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 understand that this program is mostly not implemented, but I thought, what's the point of starting to implement things if I could never display any photos?

My problem is that I cannot figure out why the .jpg are not showing. Shouldn't the method be called paint();

on creation JFrame

and JPanel

? The code is pretty messy, but I figured it would be best to include it all.

In case that matters, in the end it will be a game similar to Pokémon, where the launch window is made up of many 16x16 pixel squares that the player can move around. Before starting any of this, I wanted to experiment with displaying some images at random locations. I read similar questions and looked at examples, I just read the Java text section on graphics, but could only find information about loading images, not displaying them through paint. If anyone could help even pointing me to the correct path, that would be greatly appreciated. (I realize that I will most likely completely reboot and everything goes completely wrong, but anything you could do would help.)

+3


source to share


2 answers


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

For a Pokémon-style game, I don't think using JLabel

for every icon / image will provide any benefit. Instead of this:

  • Create the BufferedImage

    desired size for the play area.
  • Get instances Image

    for each of the smaller icons you might need to paint (symbols, elements in the landscape, etc.) and paint them onto an instance of the Graphics

    main BufferedImage

    1 .
  • To display the main image of the game, there are two good options:
    • Add the main image in JLabel

      and add it in JPanel

      , call label.repaint()

      .
    • Draw the game image directly to the instance Graphics

      specified in the paintComponent()

      method JComponent

      .

In the last part, I recommend option 1.



1. For example.

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

      

Examples of working with BufferedImage

andGraphics

+6


source


Don't reinvent the wheel. Use JLabel

with ImageIcon

to paint your image for you. Just add it to JPanel

and everything will be installed.



+4


source







All Articles