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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:50:10+00:00 2026-05-22T20:50:10+00:00

i want to write a code for the Tanks Game and I have a

  • 0

i want to write a code for the Tanks Game and I have a problem setting a tank.jpg on an existing ImageIcon , casue i want the both images to be visible and showed to the user , it’s like :

JButton block = new JButton () ;
block.setIcon(new ImageIcon("ground.png")) ;// sets the first image 
block.setIcon(new ImageIcon("tank.png")) ;// sets the second image

but if i write the code like this , the second setIcon will replace the first one , which is what i dont want , any Ideas how to have 2 icons on a JButton at once ? thanks

  • 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-22T20:50:11+00:00Added an answer on May 22, 2026 at 8:50 pm

    The easiest way is to combine the two icons into one. You can do this either manually (if you have only few combinations) or write an Icon implementation on your own.

    You can e.g. align the two icons side-by-side with the following implementation:

    import java.awt.Component;
    import java.awt.Graphics;
    
    import javax.swing.Icon;
    
    
    public class DoubleIcon implements Icon {
    
        private static final int ICONSPACING = 4;
    
        private final Icon i1;
        private final Icon i2;
    
        public DoubleIcon(Icon i1, Icon i2) {
            this.i1 = i1;
            this.i2 = i2;
        }
    
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            i1.paintIcon(c, g, x, y + (getIconHeight() - i1.getIconHeight()) / 2);
            i2.paintIcon(c, g, x + ICONSPACING + i1.getIconWidth(), y + (getIconHeight() - i2.getIconHeight()) / 2);
        }
    
        @Override
        public int getIconWidth() {
            return i1.getIconWidth() + ICONSPACING + i2.getIconWidth();
        }
    
        @Override
        public int getIconHeight() {
            return Math.max(i1.getIconHeight(), i2.getIconHeight());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find I want to write code something like this in C#, but
I am a AS3 novice learning PureMVC and want to write code following best
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)
How do I write code to connect through Windows VPN client? I want this
The code that I want to write is like this: void MethodOnThreadA() { for
I want to write an NSOutputStream to a server with apple's sample code: NSURL
I want to write a simple task which will update and commit source code
I want to write a .xml file using the following code into the App_Data/posts.
I want to write a simple web proxy, for exercise. Here's the code I
Is there a better way to write this code? I want to show a

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.