I tried to put JIcon into my JButtons but with one particular it doesnt want to fit. I tried different window and icon sizes but nothing helped, Ball's icon outsmarts me. The whole plan is made by 13x16 buttons in grid layout with empty Border. Im not sending whole code but probably only relevant part of it. I tried to make a grid layout of button on new project and same results, the ball doesnt fit
I tried ball image 40x40, 50x50, 60x60, its jpg file. The ball should look full and nice but its cut on one edge
EDIT: I change the code to show exactly whats going on, still cant find mistake.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class Line extends JFrame {
private JPanel jPBoard;
private JButton jBFill[] = new JButton[209];
private Border empty;
private int fillCounter;
private int position;
private Icon iconBall;
private Icon iconSand;
public static void main(String[] args)
{
Line frame = new Line();
frame.setSize(520, 640);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
//Icons
try
{
iconBall = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/ball.png")));
iconSand = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/sand.jpg")));
}
catch (Exception e)
{
System.err.println("Couldn't process"+e);
}
//Main pannel settings
jPBoard = new JPanel();
jPBoard.setPreferredSize(new Dimension(520, 640));
jPBoard.setBackground(Color.GRAY);
window.add(jPBoard);
jPBoard.setLayout(new GridLayout(13, 16));
empty = BorderFactory.createEmptyBorder();
//Icon to Image, resizing
Image img = ((ImageIcon)iconBall).getImage();
Image imageBall = img.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
iconBall = new ImageIcon(imageBall);
for (fillCounter = 1; fillCounter < 209; fillCounter++) {
// Filling the field
jBFill[fillCounter] = new JButton(""+fillCounter);
jBFill[fillCounter].setBorder(empty);
position = 24;
jPBoard.add(jBFill[fillCounter]);
jBFill[fillCounter].setIcon(iconSand);
if (fillCounter == 15)
{
jBFill[fillCounter].setBackground(Color.PINK);
}
if (position == fillCounter)
{
jBFill[fillCounter].setIcon(iconBall);
}
}
}
}