How to make buttons of various formats in java?

Asked

Viewed 7,146 times

4

I am creating a little game using Swing, and would like to make buttons with different formats, the way I choose. For example: a round button, or an eye-shaped button, for example.

And if you know, I’d also like to know how to put textures on these buttons.

  • 2

    Are you using Swing? is a desktop application?

  • Yes, I’m in the swing library, it’s for desktop normally, a little game!

  • @Isaacreinaldo added a link in my reply to the full project on Github, in case you had difficulties running the code, now it’s easier.

2 answers

6


Create a Jlabel and place an image to your liking, the texture you want and the format you want. To simulate the effect of a button override the methods of mousePressed(), mouseReleased() and mouseMoved().

When passing the mouse over the image one must check whether the current point is transparent or not, not to change the image erroneously, since its Jlabel remains a rectangle however the image does not. You should also check at mouse click, to not allow the Jlabel to be clicked if the mouse is over a transparent area of the image.

To correctly simulate the behavior of a button you must create an image for each state: imagem normal, imagem clicada and imagem com o mouse em cima.

Here are examples I created:

Pawn(regular). png

pawn(regular).png

Pawn(hovering). png

pawn(hovering).png

Pawn(clicking). png

pawn(clicking).png

Put the following code inside your constructor to implement exactly the algorithm I wrote above:

final ImageIcon regular = new ImageIcon(ClassLoader.getSystemResource("pawn(regular).png"));
final ImageIcon hovering = new ImageIcon(ClassLoader.getSystemResource("pawn(hovering).png"));
final ImageIcon clicking = new ImageIcon(ClassLoader.getSystemResource("pawn(clicking).png"));
final BufferedImage img = ImageIO.read(ClassLoader.getSystemResource("pawn(regular).png"));

final JLabel lblNewLabel = new JLabel(regular);
lblNewLabel.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        int pixel = img.getRGB(e.getPoint().x, e.getPoint().y);
        if( (pixel>>24) == 0x00 ) {
            return;
        }
        else { 
            System.out.println("I was clicked! I really look like a button.");
            lblNewLabel.setIcon(clicking);
        }
        super.mousePressed(e);
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        int pixel = img.getRGB(e.getPoint().x, e.getPoint().y);
        if( (pixel>>24) == 0x00 ) {
            lblNewLabel.setIcon(regular);
        }
        else {
            lblNewLabel.setIcon(hovering);                  
        }
        super.mouseReleased(e);
    }
});
lblNewLabel.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
        int pixel = img.getRGB(e.getPoint().x, e.getPoint().y);
        if( (pixel>>24) == 0x00 ) {
            lblNewLabel.setIcon(regular);
            lblNewLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        else {
            lblNewLabel.setIcon(hovering);
            lblNewLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
        super.mouseMoved(e);
    }
});
contentPane.add(lblNewLabel);

To make it cooler I changed the cursor to little hand when I’m over a non-transparent Jlabel area.

I recently created a repository to make complete project on my Github, just download it and import it into Eclipse. All images are already in the repository for ease.

2

In my opinion, you are asking three different questions. I will answer only one of them.

Round button

The class Border Swing offers the possibility of using a radio:

private static class RoundedBorder implements Border {

    private int radius;

    RoundedBorder(int radius) {
        this.radius = radius;
    }
    public Insets getBorderInsets(Component c) {
        return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
    }


    public boolean isBorderOpaque() {
        return true;
    }


    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        g.drawRoundRect(x,y,width-1,height-1,radius,radius);
    }
}

Then all you have to do is set this class to the button:

jButton.setBorder(new RoundedBorder(50));

Results:

screenshoot


Source: SO, by Lalchand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.