Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:53:27+00:00 2026-06-01T07:53:27+00:00

Before I start know this: I am extremely new to Java and programming. How

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:53:28+00:00Added an answer on June 1, 2026 at 7:53 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I start I should say I know this seems like a long shot,
Before I start, I know there is this post and it doesn't answer my
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
Before I start: I'm programming for Iphone, using objective C. I have already implemented
Before you start marking this as a duplicate , read me out. The other
     Before we start I know a fair few people consider tests that hit the
How would you evaluate project size? Part A: Before you start a project. Part
I know this might have been asked a thousand times before, but I can't
I'm trying to get some better logic behind this before I start writing code.
Before I start the question, I know that some of you won't read the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.