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 8964453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:35:37+00:00 2026-06-15T16:35:37+00:00

I’m trying to make a game in Java (school project) and I have the

  • 0

I’m trying to make a game in Java (school project) and I have the following setup:

A main class, extended with JFrame, a ‘Game’ class, extended with JPanel.

Now from within this main class, I make calls to a class ‘Player’ and a class ‘Map’.. The class ‘Map’ exists of two subclasses ‘Blocks’ and ‘Bombs’.

But I’m wondering.. How do I let the paint methods of all this classes paint to the same JPanel (of the class Game)?

I gave every class the method ‘public void paint(Graphics g)’ and do the painting.. But only the painting of the class ‘Game’ shows up when i run the program, not the painting from the subclasses.

How do I implement this?

By example, I reduced my code to this:

Main class:

    BomberGame game = new BomberGame();
        add(game);
        setSize(400, 400);
        setTitle("Bomber");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.show();

    }

    public static void main(String[] args) {
        BomberB1 main = new BomberB1();
    }
}

Game class:

    package bomberb1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


import java.util.ArrayList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BomberGame extends JPanel {
    public BomberGame() {;
        BomberMap map = new BomberMap(this);
    }

    public void paint(Graphics g) {
        g.drawRect(10, 10, 10, 10);
        g.setColor(Color.red);
        g.fillRect(10, 10, 10, 10);
    }
}

Map class:

    package bomberb1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.SwingUtilities;
public class BomberMap{
    BomberGame game;
    public BomberMap(BomberGame game) {
        this.game = game;
    }
    public void paint(Graphics g) {
        g.drawRect(30, 30, 20, 20);
    }
}
  • 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-15T16:35:38+00:00Added an answer on June 15, 2026 at 4:35 pm

    In the Entity class (which could be Map player etc) to be drawn have a draw method that accepts a Graphics object thus allowing it to access the Graphics object of the JPanel and draw to it, something like:

    class GamePanel extends JPanel {
    
        Entity e=new Entity;
    
        @Override
        protected paintComponent(Graphics g) {
            super.paintComponent(g);
    
            e.draw(g);//call draw method for entity and pass graphics object
        }
    
    }
    
    class Entity {
    
       //will draw whats necessary to Graphics object
        public void draw(Graphics g) {
            //draw to the graphics object here
        }
    }
    

    Other suggestions:

    • Do not extend JFrame class unnecessarily
    • override JPanel paintComponent() and not paint() (+1 to trashGod comment)
    • Swing components should be created and manipulated on Event Dispatch Thread via SwingUtilities.invokeLater(..) block.

    UPDATE:

    As @GuillaumePolet stated a better game design would be implementing JPanels as parent class for most of the game entities see this similar answer for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.