English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method to redraw Swing buttons into any shape pattern

The metalButton that comes with Swing is very ugly and does not meet our actual needs, so we need to customize our own buttons, such as an image button, etc. As shown in the following figure.

Next, I will explain how to make it.

(1Find some nice button images, but the button may be inside the image, so we need to extract the button using Meitu Xiu Xiu or Photoshop. As shown in the following figure:

(2Save it with a transparent background.

(3Then write a class for my button:

import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
public class newButton extends JButton{ 
 ImageIcon img; 
 public newButton(String icon){ 
  super(); 
  this.img = new ImageIcon(Demo.class.getResource(icon)); 
  setBorderPainted(false); 
  setContentAreaFilled(false); 
  setOpaque(false); 
  setSize(img.getIconWidth(),img.getIconHeight()); 
  try{ 
   bi = ImageIO.read(Demo.class.getResource(icon)); 
  } 
   JOptionPane.showMessageDialog(this,"The image file may not exist","ImageIO Exception",JOptionPane.ERROR_MESSAGE); 
   System.exit(0); 
  } 
 } 
 @Override 
 public void paintComponent(Graphics g){ 
  if(this.getModel().isPressed()){ 
   g.drawImage(img.getImage(),1,1,this); 
  } 
   g.drawImage(img.getImage(),0,0,this); 
  } 
  super.paintComponent(g); 
 } 
 BufferedImage bi ; 
 int rgb,alpha; 
 /** 
  * Set the button click range to only the non-transparent area of the image. 
  */ 
 @Override 
 public boolean contains(int x,int y){ 
  try{ 
   rgb = bi.getRGB(x,y); 
   alpha = (rgb>>24)&0xFF; 
   if(alpha==0){ 
    return false; 
   } 
    return true; 
   } 
  } 
   //An ArrayIndexOutOfBoundsException is thrown when searching for a transparent area. 
   return false; 
  } 
 } 
} 

The above program overrides the contains function to ensure that the mouse click area is limited to the valid area of the image.

(4Write a Demo class for testing:

import javax.swing.*; 
import java.awt.*; 
import java.net.URL; 
public class Demo { 
 public Demo(){ 
  JFrame jf=new JFrame("Shape Image Button Test"); 
  jf.setBounds(500,200,700,500); 
  myJPanel jp = new myJPanel(Demo.class.getResource("bg.jpg")); 
  jp.setLayout(null); 
  newButton jb1 = new newButton("bt1.png"); 
  1.setLocation(44,44); 
  jp.add(jb1); 
  1 = new newButton("snowFlower.png"); 
  1.setLocation(200,44); 
  jp.add(jb1); 
  1 = new newButton("bt2.png"); 
  1.setLocation(350,64); 
  jp.add(jb1); 
  1 = new newButton("bt3.png"); 
  1.setLocation(450,64); 
  jp.add(jb1); 
  jf.add(jp); 
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  jf.setVisible(true); 
 } 
 public static void main(String[] args){ 
  new Demo(); 
 } 
 private class myJPanel extends JPanel{ 
  ImageIcon bg; 
  public myJPanel(URL bg) { 
   this.setOpaque(false);//To be transparent. 
   this.bg = new ImageIcon(bg); 
  } 
  //Used to set the background image 
  @Override 
  public void paintComponent(Graphics g){ 
   g.drawImage(bg.getImage(),0,0,this.getWidth(),this.getHeight(),this); 
   super.paintComponent(g); 
  } 
 } 
} 

That's all for this article. Hope it will be helpful to your study, and also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You may also like