1

This may be a weird question, but interesting for me. When we create an image using java, we use:

BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );

But it uses new Rectangle as the image shape, Is there any way to change the image shape? I didn't see any option for that in my eclipse editor. If there is, please give me an example.

  • 1
    Surely you can get it as a Rectangle first, then turn it into a funkier shape later. – byxor Dec 25 '16 at 22:08
  • You can do all sorts of magic by overriding onDraw() of the label/panel or whatever is used to display your image – vlatkozelka Dec 25 '16 at 22:13
  • You may be interested in this answer: [How to take a custom shaped Screenshot?](http://stackoverflow.com/a/17766188/7274990) – Calculator Dec 25 '16 at 22:15

2 Answers2

-1

I don't see any possible way to create different shaped image

Here is a list of the available classes in awt(where we get the image shape). In this list, there aren't any other available shapes.

The method createScreenCapture() takes a java.awt.Rectangle as its sole parameter - so we can only use a Rectangle. There are no overloads to choose from either.

Images themselves are also a rectangle, the computer is unlikely to be able to read any other shape (if it's even possible to create a different shaped image).

Luke Melaia
  • 1,470
  • 14
  • 22
-1

It all comes down to how to how image sampling works

When images are taken from the real world (or rendered) they are sampled in pixels. The number of those pixels is called spacial resolution: width*height. So obviously images are always represented as a rectangles, so to make the math easier. Just imagine how much extra work would go into rendering images if they were in radial coordinates for example ;)

But, if its the displaying of the image that you are asking about. Then of course you could display them in whatever shape you like. Look at how whats app displays avatars in a circle for example.

How to realize that in Java?

You will have to create your own JComponent, most likely a subclass of JLabel and override the paint method to suit your needs. Something like

public class MyCircularLabel extends JLabel{

BufferedImage image;
//constructors, setters , getters ...

@Override
paint(Graphics g){
super.paint(g);
//initialize some polygon
//draw the image in the shape you prefer here
}

}

I honestly have never tried something like this. But in general that's what you do when you want the GUI to be drawn in a way that's different than the default : you override paint

Also you might want to look at JavaFx, since its more suited for "stylish" UI than Swing is. Look at this example Border-Radius and Shadow on ImageView

Community
  • 1
  • 1
vlatkozelka
  • 909
  • 1
  • 12
  • 27
  • (1-) `most likely a subclass of JLabel and override the paint method to suit your needs.` - custom painting in Swing is done by overriding the paintComponent() method. If you did want to do custom painting you would not extend JLabel. You would extend JComponent (or maybe JPanel). – camickr Dec 26 '16 at 03:47
  • @camickr haven't done that stuff in a while, I actually wrote the answer as JComponent at first but I think it may be easier to subclass JLabel as it has functionality to setIcon – vlatkozelka Dec 26 '16 at 22:36
  • If you are going to set the Icon then there is no need to do custom painting, you just let the JLabel paint the Icon. – camickr Dec 26 '16 at 23:40