How to create custom jbutton in java with image base?

I recently read this topic ( Creating a custom button in Java ) when creating custom buttons in java, extending the JButton class, however, all solutions to this topic use graphics drawn in java.

I wanted my button to be based on the button image that I painted in Photoshop. So I tried to apply what I read in this thread with this result:

import javax.swing.*; import java.awt.*; public class nextButton extends JButton { @Override protected void paintComponent(Graphics g) { Image image = new ImageIcon("nextButton.png").getImage(); g.drawImage(image,0,0,this); } @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); size.setSize(75, 150); return size; } } 

When I launch the main program by adding this button to JPanel, it is not displayed. I suggest this could be one of several reasons:

a) Does the JButton size not match the image? b) I did not upload the image properly. In the notes that my lecturer gave me, he writes out the image code with the image only "imageName.png" without the path to the file, so I have no idea whether to do this or how the program will know how to load the image, c) What something else that is still known to me.

If anyone knows how to solve this, I would be very grateful.

Many thanks!

+6
source share
3 answers

I asked this question before. The solution that I found best was actually doing it, not drawing.

 ImageIcon icon = new ImageIcon("pathOfImageHere.png"); JButton button = new JButton(icon); 

Thus, the button sets the image. Now I decided to make the button invisible and delete all borders. So I did the following:

 button.setOpaque(false); button.setContentAreaFilled(false); button.setBorderPainted(false); button.setFocusPainted(false); 
+5
source

For one, you should use ImageIO.read(new File("somefile.png")) to load Image . Please note that if an absolute path is not specified, by default it is relative to the working directory . If you are running out of eclipse, this is the project folder. From the jar is the folder in which the jar is located (unless otherwise indicated).

See http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html for an explanation of how to properly load an image (it also says how to do this from an applet).

In addition, you must download the image once, and then reuse it for each iteration of colors:

 BufferedImage image; public nextButton() { try { image = ImageIO.read(new File("nextButton.png")); } catch (IOException e) { e.printStackTrace(); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, null); } @Override public Dimension getPreferredSize() { return new Dimension(image.getWidth(), image.getHeight()); } 

Let me know if this works for you (be sure to put your png in the working directory!).

+4
source

Why don't you just use the JButton constructor that accepts the image?

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

+1
source

Source: https://habr.com/ru/post/902168/


All Articles