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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:05:00+00:00 2026-05-26T04:05:00+00:00

Please excuse me if this is a very simple solution or stupid mistake –

  • 0

Please excuse me if this is a very simple solution or stupid mistake – this is my first time attempting to implement graphics in Java! 🙂

I’m attempting to make a board of tiles, each is a Tile object, and all positions of the tiles are stored in a triple array of Tiles called ‘content’ (content[][][]).

In order to make each tile “clickable”, I’m basically creating a label with each tile icon and positioning that tile on the board based on the Tile object’s x,y coordinates. I do this for each of the non-null Tile objects in the content array.

This works fine when I use the graphics.drawImage function, but when I position each label using the setBorders() function it:

  1. Creates the layout of tiles, but not perfectly – it seems some are missing or below others.

and

  1. It creates a duplicate unpositioned layer above the other tiles that are in their sort-of correct positions.

The code for the function I’m calling is:

    public void paintComponent(Graphics graphics) {
    // let superclass paint to fill in background
    super.paintComponent(graphics);
    Tile[][][] content = b.getContent();

    if (content==null || tileImages==null) {
        return;
    }

    /* Set dummy previous label */ 
    prevT.setBounds(-1,-1,1,1);

    // draw tiles back to front

    for (int i = 0; i<content.length; i++) {
        for (int y = 0; y<content[i].length; y++) {
            for (int x = 0; x<content[i][y].length; x++) {
                final Tile t = content[i][y][x];
                if (t!=null) {
                    if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
                        continue;
                    }
                    if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
                        continue;
                    }
                    Image image = tileImages[t.getValue()][t.getSubindex()];

                    if (b.free(t)) {
                        image = tileImagesHL[t.getValue()][t.getSubindex()];

                    }

                    /* Mouse event magic */

                    graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);

                    /* Create icon to be displayed */
                    ImageIcon icon = new ImageIcon(image);
                    /* Label that acts as the clickable tile */
                    final JLabel label = new JLabel();

                    /* Associate image with label */
                    label.setIcon(icon);

                    /* Allow mouse events to interact with label */
                    label.addMouseListener(this);

                    /* Position labels according to tile coordinates */
                    label.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));

                    /* Associate label with specified tile */
                    t.setLabel(label);

                    /* Add label to list*/
                    labels.add(label);

                    this.setVisible(true);
                    this.add(label);

                }
            }
        }
    }
}

Any explanation for why this is happening would be greatly greatly appreciated! I’ve tried to re-write this function SO many times and am out of ideas!

Thank you! 🙂

  • 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-26T04:05:00+00:00Added an answer on May 26, 2026 at 4:05 am

    1) don’t create Object inside paintComponent(Graphics graphics), prepare these Object before

    my view, simple comtainer with Mouse event magic without MouseListener

    2) create JPanel and put there JButton#setBackground(someColor), and add JButton#setRolloverIcon(someIcon), no longer any MouseListener needed

    3) if you create Tiles then look for GridLayout

    4) prepare Icons and add these Icon to the JButton, nothing else, any Array of Objects, no code more than I described

    enter image description here enter code here

    import java.awt.*;
    import javax.swing.*;
    
    public class TilesWithButton {
    
        private Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
        private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
        private Icon errorIconRoll = UIManager.getIcon("OptionPane.errorIcon");
        private JPanel myPanel = new JPanel();
    
        public TilesWithButton() {
            myPanel.setLayout(new GridLayout(1, 2, 1, 1));
    
            JButton btn = new JButton();
            btn.setBackground(Color.white);
            btn.setIcon(infoIcon);
            btn.setRolloverIcon(errorIconRoll);
            btn.setFocusPainted(false);
            myPanel.add(btn);
    
            JButton btn1 = new JButton();
            btn1.setBackground(Color.white);
            btn1.setIcon(warningIcon);
            btn1.setRolloverIcon(errorIconRoll);
            btn1.setFocusPainted(false);
            myPanel.add(btn1);
    
            JFrame frame = new JFrame("Tiles");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(myPanel);
            frame.pack();
            frame.setLocation(150, 100);
            frame.setVisible(true);
        }
    
        public static void main(final String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TilesWithButton tilesWithButton = new TilesWithButton();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am rather new to wx/python so please excuse if this is stupid or
this was my first dojo build so please excuse my ignorance in this matter.
Now please excuse me if this is a stupid question - BUT... In my
Please excuse me if this is a very novice question, but I am little
Please excuse my complex GUI structure, I am very new to this: URL of
Please excuse the verbosity of this question, as in writing it I am attempting
First of all, please excuse me for any incoherence in the tile of this
I am very new to Jquery so please excuse this noobie question. I am
I am relatively inexperienced with java & generics, so please excuse me if this
I am a patterns newbie so please excuse this question if it sounds too

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.