Paint and repaint and JFrame/JPanel creation

Go To StackoverFlow.com

3

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.)

2012-04-04 02:35
by Pyropylon
Suggestions: You don't want to mix AWT with Swing, so get rid of all "Canvas"-related code. Never open a file from with a paint or paintComponent method. It slows the method down needlessly and makes your program much less responsive. Don't draw in a paint method but rather the the paintComponent(...) method of a JComponent or one of its children (such as a JPanel). Easier still is to put the Image into an ImageIcon and the icon into a JLabel as has already been recommended below. And most important -- you'll want to read the graphis related tutorials. They will help immensely - Hovercraft Full Of Eels 2012-04-04 03:22
Thanks a ton, and really sorry about the quality of my code. I'm still learning but I realize that's no excuse. So I should definitely use JLabels to output a 25x50 grid of images? It's not that I'm listening, just that although I'm 100% positive it doesn't look like I spent a large amount of time getting here. I just want to make sure I'm going in the right direction with JLabels for many images - Pyropylon 2012-04-04 03:37
If your 25x50 grid of images contains many of the same images, then yeah I'd go with ImageIcons. You can add one ImageIcon to many JLabels so you may only be dealing with a few Images/ImageIcons - Hovercraft Full Of Eels 2012-04-04 03:38
There would only be about 5 different images all the same size, so it looks like your method could be very promising. Thanks again Hovercraft, this has really helped me out - Pyropylon 2012-04-04 03:55


6

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 BufferedImage1.
  • 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. E.G.

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.
2012-04-04 03:33
by Andrew Thompson
I was originally planning on outputting a grid 25x50 of icons with only about 5 total different icons throughout the entire game area. Your method of creating a BufferedImage and painting to it seems more flexible and would provide an easier way to implement more features in the future, but I don't think I fully understand. Could you link me a good source on painting to, and using BufferedImage - Pyropylon 2012-04-04 03:52
Thanks a ton! This should help me to get a good grasp of BufferedImage. You've been a life saver Andrew - Pyropylon 2012-04-04 04:09
Glad you got a strategy sorted. : - Andrew Thompson 2012-04-04 04:46


4

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.

2012-04-04 02:39
by Jeffrey
Would labels be the most effective way to output 50x25 imagines? How would I deal with a quantity like that? Could you link something to get me started? (Sorry I should have mentioned that In my original post. - Pyropylon 2012-04-04 02:49
@Pyropylon Label tutorial - Jeffrey 2012-04-04 03:04
Thanks a lot Jeffrey, I'm going over the Labels now. Really sorry about the quality of my work. This should help a ton. Thanks again - Pyropylon 2012-04-04 03:45
Ads