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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:59:03+00:00 2026-06-05T01:59:03+00:00

i have a class that draws a shape from a png image, so that

  • 0

i have a class that draws a shape from a png image, so that i can use the shape to draw the border of custom buttons that i need for my project. here’s the code for the class to draw the shape of an image:

public class CreateShapeClass {
    public static Area createArea(BufferedImage image, int maxTransparency) {
        Area area = new Area();
        Rectangle rectangle = new Rectangle();
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                int rgb = image.getRGB(x, y);
                rgb = rgb >>> 24;
                if (rgb >= maxTransparency) {
                    rectangle.setBounds(x, y, 1, 1);
                    area.add(new Area(rectangle));
                }
            }
        }
        return area;
    }
}

however, this takes a very long time to process, and I thought that by pre-drawing the shapes in my main application and then storing them into an array and passing to other classes, it will reduce the rendering time. however, the time taken for the paintBorder() method to draw out the border of the button takes a pretty long time too (although shorter than the time required to draw the shape), because the shape generated by the class above is filled and not empty. I’ve tried to draw shapes using java2d, for example, Ellipse2D, and the rendering of the button only takes a very short time. anyone experienced in this field can teach me how do i generate a shape that is the border of a BufferedImage? i use the class above to create the shape from PNG image with a transparent background.

  • 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-05T01:59:04+00:00Added an answer on June 5, 2026 at 1:59 am

    For tips, look at Smoothing a jagged path. The algorithm to obtain the (crude) outline was relatively quick in the final versions. Creating a GeneralPath is astonishingly faster than appending Area objects.

    The important part is this method:

    public Area getOutline(Color target, BufferedImage bi) {
        // construct the GeneralPath
        GeneralPath gp = new GeneralPath();
    
        boolean cont = false;
        int targetRGB = target.getRGB();
        for (int xx=0; xx<bi.getWidth(); xx++) {
            for (int yy=0; yy<bi.getHeight(); yy++) {
                if (bi.getRGB(xx,yy)==targetRGB) {
                    if (cont) {
                        gp.lineTo(xx,yy);
                        gp.lineTo(xx,yy+1);
                        gp.lineTo(xx+1,yy+1);
                        gp.lineTo(xx+1,yy);
                        gp.lineTo(xx,yy);
                    } else {
                        gp.moveTo(xx,yy);
                    }
                    cont = true;
                } else {
                    cont = false;
                }
            }
            cont = false;
        }
        gp.closePath();
    
        // construct the Area from the GP & return it
        return new Area(gp);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm missing something fundamental here. i have a very simple custom class that draws
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
Let's say I have an abstract parent class called shape, and that there are
Suppose I have the following: class Shape a where draw a :: a ->
I have a class that draws a GUI, using gtk. Clicking a button will
I have a class that is called ShapeBase. It is abstract and draw method
I have a MyCanvas class that extends JComponent. On this canvas I have drawn
I have class that represents users. Users are divided into two groups with different
I have class A such that: class A { static int i; A(); f1();
I have a class that implements the IDisposable interface. I am using a webclient

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.