0

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

enter image description here

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);
            }
        }
    }
}
  • 2
    Welcome to Stack Overflow! That looks like more code than strictly needed. Can you reduce the code until you have a [mcve] that demonstrates your problem? This will increase your chances of getting good answers. – Neuron Apr 22 '18 at 06:28
  • Welcome to SO. Please do some research before asking. A simple search yields many links which may be relevant: [1](https://stackoverflow.com/questions/2856480/resizing-a-imageicon-in-a-jbutton) [2](https://stackoverflow.com/questions/36957450/fit-size-of-an-imageicon-to-a-jbutton) [3](https://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java) – c0der Apr 22 '18 at 06:32
  • To get an image for the example suggested by @LonelyNeuron, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Apr 22 '18 at 09:28
  • I tried code from "1", which is same as in the "2". It didn't change anything, still looks the same. – zanstaszek9 Apr 22 '18 at 09:36
  • Tip: Add @c0der (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 22 '18 at 10:27

1 Answers1

1

A mcve for the problem you describe could be:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class Line extends JFrame {

    private static int rows = 3, cols = 3;
    private JButton jBFill[] = new JButton[rows*cols];

    Line(){
        createGUI();
        pack();//apply preferred dimensions 
        setVisible(true);
    }

    public static void main(String[] args)  {

        SwingUtilities.invokeLater(() -> new Line());
    }

    private void createGUI(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //For posts it is more convenient to use generated images
        Image ballImage = createImage(Color.BLUE);
        Icon  ballIcon = new ImageIcon(ballImage);

        Image sandImage = createImage(Color.YELLOW);
        Icon  sandIcon = new ImageIcon(sandImage);

        JPanel jPBoard = new JPanel();
        jPBoard.setBackground(Color.GRAY);
        add(jPBoard);
        jPBoard.setLayout(new GridLayout(rows, cols));
        Border empty = BorderFactory.createEmptyBorder();

        for (int fillCounter = 0; fillCounter < (rows * cols); fillCounter++) {
            JButton button = new JButton(""+fillCounter);
            button.setBorder(empty);
            button.setIcon(((fillCounter%2) ==0) ?  sandIcon : ballIcon);
            button.setPreferredSize(new Dimension(125,40)); //set preferred dimension to button 
            jBFill[fillCounter] = button;
            jPBoard.add(button);
        }
    }

    //creates 100x100 image
    private BufferedImage createImage(Color color) {

        int iconWidth =100;
        BufferedImage img = new BufferedImage(iconWidth , iconWidth,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setColor(color);
        g2.fillOval(1, 1, iconWidth - 2, iconWidth - 2);
        g2.dispose();
        return img;
    }
}

The out put shows the problem clearly: the 100x100 ball-shaped icon used, is cropped :

enter image description here

One remedy, as suggested in one of the links in my comment, is to re scale the image, before applying it to the buttons. Replace these lines :

    Image ballImage = createImage(Color.BLUE);
    Icon  ballIcon = new ImageIcon(ballImage);
    Image sandImage = createImage(Color.YELLOW);
    Icon  sandIcon = new ImageIcon(sandImage);

With:

    Image ballImage = createImage(Color.BLUE);
    Image scaledBall = ballImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH ) ;
    Icon ballIcon = new ImageIcon(scaledBall);
    Image sandImage = createImage(Color.YELLOW);
    Image scaledSand = sandImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH ) ;
    Icon sandIcon = new ImageIcon(scaledSand);

And see output with the scaled images:

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65