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

  • SEARCH
  • Home
  • 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 3356710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:32:44+00:00 2026-05-18T02:32:44+00:00

-> Short Introduction, you can SKIP this part I’m glad to be able to

  • 0

->Short Introduction, you can SKIP this part

I’m glad to be able to finally post on this platform, because I for myself have gained so much knowledge through this community; just by reading. So I wanted to say “Hello everybody, and thank you!

Actual Content(prologue):

Though I’m developing in Objective-C in my company, I am utterly interested in JAVA development.
I am quite comfortable with the syntax but have some major troubles with awt/swing/Graphics2D.

The Concept:

JAVA Application with a 800*600 Frame. On this Frame you can see 16*12 tiles (i.e. gras.jpg or tree.jpg) with a size of 50px². Above this Frame a .png “player” is moving. The field is generated with a 2 dimensional int array[16][12] where a 0 symbolizes “gras” and 1 means “tree”.

-> This is actually “working”.

First Try:

Adding (frame.add(…)) 16*12 JLabels with imageIcon “gras” or “tree” to a JLayeredPane
Adding (frame.add(…)) class “entityLayer” wich is updated at 5ms with paint(Graphics g)
{
g.drawImage(imageIcon.getImage());
}
This Version “works” if I add either the field with tiles or the entity layer. In every case else either the entityLayer overlays the field with a grey background or the “player” is not visible under the field;

Second Try:

Making all the drawings in the entityLayer paint(Graphics g) method. But thats not what I intended. I want the field to be drawn once and the “player” with, like a translucent background, drawn above the field.

Question:

How can I get myself in a position to actually have two seperate layers, that are both equally independent with out having one overlapping the other? Im nearly 99% shure my attempt is wrong in some way.

Thank you right away.

  • 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-05-18T02:32:45+00:00Added an answer on May 18, 2026 at 2:32 am

    Instead of adding/drawing 16*12 JLabels, just draw the tile image 16*12 times?

    For things like this, I usually add a single JPanel to a JFrame, and override the JPanel’s paint() method.
    Actually, I don’t add a JPanel, I add a subclass of JPanel that I have created, you should do the same, as I will show you will need to add a field to the JPanel and override the paint method

    Your background tile drawing code might look similar to:

    int tileWidth = 50;
    int tileHeight = 50;
    for ( int x = 0; x < 16; x++ )
    {
        for ( int y = 0; y < 12; y++ )
        {
            Image tileImage;
            int tileType = fieldArray[x][y];
    
            switch ( tileType )
            {
                case 0:
                {
                    tileImage = myGrassImage;
                    break;
                }
                case 2:
                {
                    tileImage = myTreeImage;
                    break;
                }
            }
    
            Graphics2D g;
            g.drawImage(tileImage, x * tileWidth, y * tileHeight, null);
        }
    }
    

    A technique that works well for me is to separate the drawing logic of each layer into a separate class that implements the Painter interface (or a similar interface you define).

    public class TilePainter implements Painter
    {
    
        @Override
        public void paint( Graphics2D g, Object thePanel, int width, int height )
        {
            //The tile painting code I posted above would go here
            //Note you may need to add a constructor to this class
            //that provides references to the needed resources
            //Eg: images/arrays/etc
            //Or provides a reference to a object where those resources can be accessed
        }
    
    }
    

    Then for the player painter:

    public class EntityPainter implements Painter
    {
    
        @Override
        public void paint( Graphics2D g, Object thePanel, int width, int height )
        {
            g.drawImage(player.getImage(), player.getX(), player.getY(), null);
        }
    
    }
    

    If you are wondering why I am passing NULLs into the g.drawImage() function, its because for that overloaded function call the last parameter is an ImageObserver, which is something we do not need for this purpose.

    Ok, so once you have your painters separated into different classes we need to make the JPanel capable of using them!

    This is the field that you need to add to your JPanel:

    List<Painter> layerPainters;
    

    Your constructor should look something like this:

    public MyExtendedJPanel()
        {
            //I use an ArrayList because it will keep the Painters in order
            List<Painter> layerPainters = new ArrayList<Painter>();
    
            TilePainter tilePainter = new TilePainter(Does,This,Need,Args);
            EntityPainter entityPainter = new EntityPainter(Does,This,Need,Args);
    
            //Layers will be painted IN THE ORDER THEY ARE ADDED
            layerPainters.add(tilePainter);
            layerPainters.add(entityPainter);
    
        }
    

    Now for the last but most important part:

    @Override
    public void paint( Graphics g )
    {
        int width = getWidth();
        int height = getHeight();
        //Loops through all the layers in the order they were added
        //And tells them to paint onto this panels graphic context
        for(Painter painter : layerPainters)
        {
            painter.paint(g,this,width,height);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my question is: I have a short introduction movie for my application and i
Can someone give me a short introduction to doing DB migrations in Rails using
Introduction I have some sort of values that I might want to access several
Introduction : Hello Everyone, I have been looking for days for a way to
Short Introduction I am kind of new in the field of app development using
Short version of what I want to accomplish : I have a foot pedal
I am working through the Introduction to Algorithms book by Cormen, and I have
I have been reading parts of Introduction to Algorithms by Cormen et al, and
Short introduction. There is a production system where some independent clients invent some objects
Short introduction Working on my first commercial iOS app, XCode 3.2 has proven to

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.