0

I used this code to try and make the JButtons in my project (chess game) transparent so you can only see the piece and not the background. It works but the buttons copy themselves when I move my mouse over the board. E.g. If I have my mouse on a piece with a pawn and then move to a piece with a knight on it the pawn will appear behind the knight. Repainting the frame gets rid of them, but even if I repaint on mouse move it still looks really odd when I move around the board.

public class Pawn extends JButton(){

Pawn(x, y){
   this.setIcon(new ImageIcon("pawn"));
   this.setBounds(x, y, 100, 100);
   this.makeClear();
}

public void makeClear(){
    this.setOpaque(false);
    this.setContentAreaFilled(false);
    this.setBorderPainted(false);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

-1

Make sure you setLayout(null) on the panel which contains the buttons. Then, when you setBounds() on a button, call repaint() on the panel.

Andrei Vajna II
  • 4,642
  • 5
  • 35
  • 38
  • *"Make sure you `setLayout(null)`"* Make sure you ***don't*** call `setLayout(null)`! Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 04 '15 at 11:26