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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:22:07+00:00 2026-06-09T02:22:07+00:00

So, I have been modding Minecraft for a while, and I saw how it

  • 0

So, I have been modding Minecraft for a while, and I saw how it generates its buttons. It takes a very wide image of a button, takes part of the left, takes part of the right side, and puts the two back together to form a smaller image.

I’m sorry, but I can’t explain this well in words, so let me just show you with bad Paint.Net skills:

explanation

However, I cannot get it to work, as it turns out like this:
turnout

Here is part of my code:

public class ComponentToolbarButton extends JComponent implements MouseListener {

    private static final int HEIGHT = 40;

    // ... other methods here

    private BufferedImage getImageBasedOnWidth(Graphics g) {
        BufferedImage finalImage = null;
        BufferedImage rawImage = null;


         // Try/catch block to initialize rawImage

         // Setting font and things here        

        int compWidth = determineComponentWidth(g); // Returns the (should-be) width of the component

        if (compWidth != getWidth()) { 
            setPreferredSize(new Dimension(compWidth, HEIGHT)); // Just making sure :)
        }

        finalImage = new BufferedImage(compWidth, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics finalImageGraphics = finalImage.getGraphics();


        // Draw left side:
        finalImageGraphics.drawImage(rawImage, 0, 0, 0, compWidth / 2, 0, 0, compWidth, HEIGHT, null);

        // Draw right side:
        finalImageGraphics.drawImage(rawImage, compWidth / 2, 0, compWidth, HEIGHT, rawImage.getWidth() / 2 - compWidth,
            0, rawImage.getWidth(), rawImage.getHeight(), null);


        return finalImage;
    }
}

Thanks in advance! (I tried to shorten the code as much as I could BTW)

  • 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-09T02:22:09+00:00Added an answer on June 9, 2026 at 2:22 am

    Okay, after some thinking, I came up with this little hack to demonstrate how I believe it should work 😛

    public class TestMenu extends JComponent {
    
        protected static final int MAX_HEIGHT = 40;
        protected static final int LEFT_MARGIN = 5;
        protected static final int RIGHT_MARGIN = 5;
    
        protected static final ImageIcon BACKGROUND = new ImageIcon(TestMenu.class.getResource("/Menu.png"));
    
        private String text;
    
        public TestMenu() {
    
            setOpaque(false);
            text  = "This is some text";
            setFont(UIManager.getFont("Label.font"));
    
        }
    
        @Override
        public Dimension getPreferredSize() {
    
            FontMetrics fm = getFontMetrics(getFont());
            int width = fm.stringWidth(text) + LEFT_MARGIN + RIGHT_MARGIN;
    
            Dimension size = new Dimension(width, MAX_HEIGHT);
    
            return size;
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            int width = getWidth() - 1;
            int height = getHeight() - 1;
    
            BufferedImage imgLeft = new BufferedImage(LEFT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
            BufferedImage imgRight = new BufferedImage(RIGHT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
            BufferedImage imgBody = new BufferedImage(width - LEFT_MARGIN - RIGHT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
    
            Graphics2D g2d = imgLeft.createGraphics();
            g2d.drawImage(BACKGROUND.getImage(), 0, 0, this);
            g2d.dispose();
    
            g2d = imgRight.createGraphics();
            g2d.drawImage(BACKGROUND.getImage(), RIGHT_MARGIN - BACKGROUND.getIconWidth(), 0, this);
            g2d.dispose();
    
            g2d = imgBody.createGraphics();
            g2d.drawImage(BACKGROUND.getImage(), -LEFT_MARGIN, 0, this);
            g2d.dispose();
    
            g2d = (Graphics2D) g;
    
            g2d.drawImage(imgLeft, 0, 0, this);
            g2d.drawImage(imgBody, LEFT_MARGIN, 0, this);
            g2d.drawImage(imgRight, width - RIGHT_MARGIN, 0, this);
    
            FontMetrics fm = g2d.getFontMetrics();
    
            int x = (width - fm.stringWidth(text)) / 2;
            int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
    
            g2d.setColor(Color.WHITE);
            g2d.drawString(text, x, y);
    
    
        }
    
    }
    

    First, one should never try and build all the image segments in the paint method, it will slow down the paint routine and consume to much memory, but for this example, I just wanted to demonstrate the idea. Use a backing buffer instead (which I think is what you were trying to do anyway).

    This will produce this:

    enter image description here

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

Sidebar

Related Questions

I have been modding Valve games for quite a while, but one that I
I have been tracking emails for years using a "beacon" image and for those
I have been working with SQL Server as a Developer a while. One thing
Have been fighting this for two days and am very frustrated but feel like
I have been looking for a solution to this problem for a while now
I have been trying for a while now, but I can't figure out how
Have been googling for a while, but I don't find any page which explains
Have been working in Ruby for a while, but usually in the context of
Have been using it for a while with CodeIgniter and I can't remember if
Have been programming with VS studio 2005/2008 making ASP.NET applications for a while. I

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.