I am making a slider puzzle applet. And i have this problem when i display it as an applet my buttons do not seem to act the same was as they did before i made it into an applet by using a JApplet rather than a JFrame as my extension of the class (i'm sure there is terminology for that, im just new at java)

What i want is my images on my buttons to take up the whole button rather than just part of it. Have a look at the attached image for reference.

This is my code:

package Puzzle;

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

import java.awt.BorderLayout;
import javax.swing.JApplet;
import java.awt.Dimension;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JPanel;

import java.util.Random;

 * @author Owner
public class Main extends JApplet implements ActionListener{

    private JPanel centerPanel;
    private JButton button;
    private JLabel label;
    private Image source;
    private Image image;
    private Image[] imlist;
    private JButton[] buttons;
    private Random generator;

    int[][] pos;
    int width, height;

    public Main(){
        generator = new Random();
        imlist = new Image[11];
        buttons = new JButton[11];
        pos = new int[][]{

        centerPanel = new JPanel();
        centerPanel.setLayout((new GridLayout(4,4,0,0)));

        ImageIcon sid = new ImageIcon(Main.class.getResource("Sid.jpg"));
        source = sid.getImage();

        width = sid.getIconWidth();
        height = sid.getIconHeight();

        add(Box.createRigidArea(new Dimension(0,5)), BorderLayout.NORTH);

        int count = -1;
        for(int i = 0; i<4 ; i++ ){
            for(int j = 0; j<3; j++){
                count ++;
                if(j==2 && i == 3){
                    label = new JLabel("");
                    button = new JButton();
                    imlist[count] = createImage(new FilteredImageSource(source.getSource(),
                            new CropImageFilter(j*width/3, i*height/4, width/3+1, height/4)));
                    buttons[count]  = button;
        Image im;
        for(int i = 0; i< buttons.length; i++){
            try{ im = imlist[generator.nextInt(imlist.length-1)];}
            catch(Exception e){
                im = imlist[0];
            imlist = remove(im, imlist);

            buttons[i].setIcon(new ImageIcon(im));

        setSize(325, 275);


     * @param args the command line arguments
    public void init() {
        new Main();

    public void actionPerformed(ActionEvent e) {
        JButton but = (JButton) e.getSource();

        Dimension size = but.getSize();

        int LabelX = label.getX();
        int LabelY = label.getY();
        int ButtonX = but.getX();
        int ButtonY = but.getY();
        int ButtonPosX = ButtonX/size.width;
        int ButtonPosY = ButtonY / size.height;
        int buttonIndex = pos[ButtonPosY][ButtonPosX];

        if (LabelX == ButtonX && (LabelY - ButtonY) == size.height ) {

             int labelIndex = buttonIndex + 3;

             centerPanel.add(label, buttonIndex);

        if(LabelY == ButtonY && (LabelX - ButtonX) == size.width){
            int labelIndex = buttonIndex +1;

            centerPanel.add(label, buttonIndex);

        if(LabelY == ButtonY && (LabelX - ButtonX) == -size.width){
            int labelIndex = buttonIndex -1;

            centerPanel.add(label, labelIndex);
            centerPanel.add(but, labelIndex);


        if (LabelX == ButtonX && (LabelY - ButtonY) == -size.height){
            int labelIndex = buttonIndex - 3;

            centerPanel.add(but, labelIndex);
            centerPanel.add(label, buttonIndex);

    public Image[] remove(Image im, Image[] list){
        Image[] ret = new Image[list.length-1];
        int count = 0;
        for(int i = 0; i < list.length; i++){
            if(list[i]!= im){
                ret[count] = list[i];
                count ++;

        return ret;


try setting your button to a certain width and scale the imageIcons by using getScaledInstance() method of the Image class.

You can set the margin on the button with the following

button.setMargin(new Insets(0,0,0,0));

I put that line of code in just before where i set the icon on the button and nothing happened. Is this because i put it in the wrong place? Or do i need a different solution?

I just noticed that when i had this as just a JFrame swing application it worked perfectly and just how i wanted it. So, is there perhaps something about the way that a JApplet displays it that could change how it works?

Well, the look and feel could be different between the two perhaps.

Is there any way that i could change that? Or is that just set for the JApplet and JFrame?