Load image in a JPanel? - java

I can't figure out why this code wont work. Any ideas?
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.swing.JPanel;
public class ImageTool extends JPanel {
private static final long serialVersionUID = 1L;
private static Image image;
public ImageTool(URL url) {
image = Toolkit.getDefaultToolkit().getImage(url);
rightSize();
}
private void rightSize() {
int width = image.getWidth(this);
int height = image.getHeight(this);
if (width == -1 || height == -1)
return;
addNotify();
System.out.println("Image width: "+width);
System.out.println("Image height"+height);
}
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
if ((infoflags & ImageObserver.ERROR) != 0) {
System.out.println("Error loading image!");
System.exit(-1);
}
if ((infoflags & ImageObserver.WIDTH) != 0
&& (infoflags & ImageObserver.HEIGHT) != 0) {
rightSize();
System.out.println("1");
}
if ((infoflags & ImageObserver.SOMEBITS) != 0)
repaint();
if ((infoflags & ImageObserver.ALLBITS) != 0) {
System.out.println("2");
rightSize();
repaint();
return false;
}
return true;
}
public void update(Graphics g) {
paint(g);
}
public void paintComponent(Graphics g) {
Insets insets = getInsets();
g.drawImage(image, insets.left, insets.top, this);
}
public static void main(String[] args) throws Exception {
String url = "http://www.java2s.com/style/logo.png";
new ImageTool(new URL(url));
}
}

In your code you're missing a JFrame or JDialog to contain your JPanel in. Here's an example that I believe does what you're asking for. It loads the same image into a visible window and outputs the dimensions to the console.
public class ImageTool extends JPanel {
public ImageTool(URL url) {
ImageIcon icon = new ImageIcon(url);
JLabel label = new JLabel(icon, JLabel.CENTER);
add(label);
System.out.println("Image width: " + icon.getIconWidth());
System.out.println("Image height: " + icon.getIconHeight());
}
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://www.java2s.com/style/logo.png");
JPanel panel = new ImageTool(url);
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}

I'm not sure what you are trying to do either, but your code looks like an old AWT example and should not be used for Swing.
there is no need to override update()
paintComponent() should invoke super.paintComponent()
Read the Swing tutorial on How to Use Icons for example code that uses images on labels.

Related

Create a clickable area around round JLabel image

I am trying to create a JLabel with a oval shaped image, like this.
My code is the following:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public final class RoundedButtonDemo {
private static Image bi;
public static void main(String[] args) {
try {
loadImage();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
} catch (IOException e) {
// handle exception
}
}
private static void loadImage() throws IOException {
int newWidth = 80;
int newHeight = 40;
bi = ImageIO.read(RoundedButtonDemo.class.getResource("/resources/login.png"));
bi = bi.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
}
private static void createAndShowGUI() {
final JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setSize(new Dimension(5, 5));
label.setIcon(new ImageIcon(bi));
label.setText("Hello World");
label.setHorizontalTextPosition(JLabel.CENTER);
// label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.addMouseListener(new MouseListener() {
private int count = 0;
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == label) {
if (count % 2 == 0) {
label.setText("Bye");
} else {
label.setText("Hello World");
}
count++;
}
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
frame.add(label);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This code creates a JLabel containing the above image. The text of the JLabel should alternate every time the button is clicked, based on a MouseListener added to the JLabel.
The problem I'm facing is that even when I click outside the image (also outside the JLabel), the MouseListener is triggered, and the text alternates.
The big picture of what I want to achieve is :
A rounded button which responds to a MouseListener, whenever it is clicked anywhere on its surface.
I tried your code. You are getting this behavior because your JLabel is actually filling the entire frame. You need to set a layout for your frame; something like this:
// ...
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout()); // <-- you need this
final JLabel label = new JLabel();
label.setPreferredSize(new Dimension(80, 40)); // <-- also this
label.setIcon(new ImageIcon(bi));
label.setText("Hello World");
// ...
FlowLayout is one of the simplest layout managers, of which there are many. You can read about them here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Hope this helps.
PS: you had a good idea trying to debug this problem, adding a border like this:
// label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
Not sure why you commented it out. Maybe you didn't notice that there was a black border around the whole frame area. Try setting to color to Color.RED or something more noticeable.
As yoshi pointed out, the JLabel fills the entire content pane, so clicking anywhere in the content pane results in mouse events for the label.
Some words:
1) You could also use GridBagLayout, instead of FlowLayout, in case you want the JLabel centered in its parent component (both vertically and horizontally). This solution is here.
Like so:
final JPanel singleCenteredComponentJPanel = new JPanel(new GridBagLayout());
singleCenteredComponentJPanel.add(label);
frame.add(singleCenteredComponentJPanel);
2) If, furthermore, you want to ignore mouse clicks on any transparent pixels of the label icon (if any) you can do the following:
Use the BufferedImage.getRGB(int x, int y), BufferedImage.getColorModel() and ColorModel.getAlpha(int pixel) methods to determine the alpha value of the clicked pixel for each mouse event. If the alpha value is equal to 0, then the pixel is completely transparent, otherwise the alpha value is between 1 and 255 (both inclusive) which in turn means the pixel is not completely transparent. Example code follows below.
And instead of Image.getScaledInstance(...) to scale the image (which returns Image, but we need BufferedImage), use the solution provided here.
Like so:
private static void loadImage() throws IOException {
int newWidth = 80;
int newHeight = 40;
bi = ImageIO.read(RoundedButtonDemo.class.getResource("/resources/login.png"));
bi = getScaledBufferedImage(bi, newWidth, newHeight);
}
public static BufferedImage getScaledBufferedImage(final Image img,
final int newWidth,
final int newHeight) {
final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gdev = genv.getDefaultScreenDevice();
final GraphicsConfiguration gcnf = gdev.getDefaultConfiguration();
final BufferedImage simg = gcnf.createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);
//Painting input image to output image:
final Graphics2D g2d = simg.createGraphics();
g2d.drawImage(img, 0, 0, newWidth, newHeight, null); //#Docs "...is scaled if necessary.".
g2d.dispose();
return simg;
}
Also change the reference bi from Image to BufferedImage.
And then the MouseListener of the label:
private int count = 0;
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == label) {
//Get the mouse click position (in pixels),
//relative to the top-left corner of label:
final Point relativeClickPoint = e.getPoint();
//Obtain alpha value from the TYPE_INT_ARGB pixel:
final int pixel = bi.getRGB(relativeClickPoint.x, relativeClickPoint.y);
final int alpha = bi.getColorModel().getAlpha(pixel);
if (alpha > 0) { //Check if the pixel is not transparent.
if (count % 2 == 0) {
label.setText("Bye");
} else {
label.setText("Hello World");
}
count++;
}
}
}
Be aware for the above implementation: All transparent pixels of the image will be ignored (including transparent pixels "inside" the shape, if any). Which means the user could click in the center of the image and nothing would happen if the clicked pixel was completely transparent. So I made the assumption that this is not the case with your image here.
3) For the scaling of the Image, you can determine the new size with the method collapseInside(...):
/**
* #param resultDim Output dimension (same aspect ratio as the original dimension, and inside containerDim).
* #param originalDim Original dimension.
* #param containerDim Dimension with the maximum width and maximum height.
*/
public static void collapseInside(final Dimension resultDim,
final Dimension originalDim,
final Dimension containerDim) {
resultDim.setSize(originalDim);
if (resultDim.width > containerDim.width) {
//Adjusting height for max width:
resultDim.height = ( resultDim.height * containerDim.width ) / resultDim.width;
resultDim.width = containerDim.width;
}
if (resultDim.height > containerDim.height) {
//Adjusting width for max height:
resultDim.width = ( resultDim.width * containerDim.height ) / resultDim.height;
resultDim.height = containerDim.height;
}
}
With this method:
a) Scaled Image's width will be less than or equal to container's size width (resultDim.width <= containerDim.width).
b) Same for height (resultDim.height <= containerDim.height).
c) Aspect ratio of the original Image size will be preserved in the new Image size (resultDim.width / resultDim.height == originalDim.width / originalDim.height).
With Image.getScaledInstance(...) and the previous getScaledBufferedImage(...) the aspect ratio of the Image could change.
So I edit the getScaledBufferedImage(...) to use collapseInside(...):
public static BufferedImage getScaledBufferedImage(final Image img,
final int newWidth,
final int newHeight) {
final Dimension originalDim = new Dimension(img.getWidth(null), img.getHeight(null)),
containerDim = new Dimension(newWidth, newHeight),
resultDim = new Dimension();
collapseInside(resultDim, originalDim, containerDim);
final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gdev = genv.getDefaultScreenDevice();
final GraphicsConfiguration gcnf = gdev.getDefaultConfiguration();
final BufferedImage simg = gcnf.createCompatibleImage(resultDim.width, resultDim.height, Transparency.TRANSLUCENT);
//Painting input image to output image:
final Graphics2D g2d = simg.createGraphics();
g2d.drawImage(img, 0, 0, resultDim.width, resultDim.height, null); //#Docs "...is scaled if necessary.".
g2d.dispose();
return simg;
}
Complete code for all three above:
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Transparency;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public final class RoundedButtonDemo {
private static BufferedImage bi;
public static void main(String[] args) {
try {
loadImage();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
} catch (IOException e) {
// handle exception
}
}
private static void loadImage() throws IOException {
int newWidth = 80;
int newHeight = 40;
bi = ImageIO.read(RoundedButtonDemo.class.getResource("/resources/login.png"));
bi = getScaledBufferedImage(bi, newWidth, newHeight);
}
public static BufferedImage getScaledBufferedImage(final Image img,
final int newWidth,
final int newHeight) {
final Dimension originalDim = new Dimension(img.getWidth(null), img.getHeight(null)),
containerDim = new Dimension(newWidth, newHeight),
resultDim = new Dimension();
collapseInside(resultDim, originalDim, containerDim);
final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gdev = genv.getDefaultScreenDevice();
final GraphicsConfiguration gcnf = gdev.getDefaultConfiguration();
final BufferedImage simg = gcnf.createCompatibleImage(resultDim.width, resultDim.height, Transparency.TRANSLUCENT);
//Painting input image to output image:
final Graphics2D g2d = simg.createGraphics();
g2d.drawImage(img, 0, 0, resultDim.width, resultDim.height, null); //#Docs "...is scaled if necessary.".
g2d.dispose();
return simg;
}
/**
* #param resultDim Output dimension (same aspect ratio as the original dimension, and inside containerDim).
* #param originalDim Original dimension.
* #param containerDim Dimension with the maximum width and maximum height.
*/
public static void collapseInside(final Dimension resultDim,
final Dimension originalDim,
final Dimension containerDim) {
resultDim.setSize(originalDim);
if (resultDim.width > containerDim.width) {
//Adjusting height for max width:
resultDim.height = ( resultDim.height * containerDim.width ) / resultDim.width;
resultDim.width = containerDim.width;
}
if (resultDim.height > containerDim.height) {
//Adjusting width for max height:
resultDim.width = ( resultDim.width * containerDim.height ) / resultDim.height;
resultDim.height = containerDim.height;
}
}
private static void createAndShowGUI() {
final JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setSize(new Dimension(5, 5));
label.setIcon(new ImageIcon(bi));
label.setText("Hello World");
label.setHorizontalTextPosition(JLabel.CENTER);
// label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.addMouseListener(new MouseListener() {
private int count = 0;
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == label) {
//Get the mouse click position (in pixels),
//relative to the top-left corner of label:
final Point relativeClickPoint = e.getPoint();
//Obtain alpha value from the TYPE_INT_ARGB pixel:
final int pixel = bi.getRGB(relativeClickPoint.x, relativeClickPoint.y);
final int alpha = bi.getColorModel().getAlpha(pixel);
if (alpha > 0) { //Check if the pixel is not transparent.
if (count % 2 == 0) {
label.setText("Bye");
} else {
label.setText("Hello World");
}
count++;
}
}
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
final JPanel singleCenteredComponentJPanel = new JPanel(new GridBagLayout());
singleCenteredComponentJPanel.add(label);
frame.add(singleCenteredComponentJPanel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Alternatively, you may also override the paintComponent(...) of JPanel and use Graphics.drawImage(...) to paint the Image bi to the JPanel like so:
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
//Drawing the image:
g.drawImage(img, 0, 0, null);
//Drawing the text:
//For centering the text, I used code from:
//https://stackoverflow.com/questions/27706197/how-can-i-center-graphics-drawstring-in-java
final FontMetrics metrics = g.getFontMetrics(g.getFont());
final int x = (img.getWidth(null) - metrics.stringWidth(text)) / 2;
final int y = (img.getHeight(null) - metrics.getHeight()) / 2 + metrics.getAscent();
g.drawString(text, x, y);
}
Where img is bi and text is the text of the button (i.e. "Hello World" and "Bye").
Then add the MouseListener to the JPanel as is.
Complete code for this (as "RoundedButtonDemo1" class, in the same package):
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Transparency;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public final class RoundedButtonDemo1 {
private static BufferedImage bi;
public static void main(String[] args) {
try {
loadImage();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
} catch (IOException e) {
// handle exception
}
}
private static void loadImage() throws IOException {
int newWidth = 80;
int newHeight = 40;
bi = ImageIO.read(RoundedButtonDemo1.class.getResource("/resources/login.png"));
bi = getScaledBufferedImage(bi, newWidth, newHeight);
}
public static BufferedImage getScaledBufferedImage(final Image img,
final int newWidth,
final int newHeight) {
final Dimension originalDim = new Dimension(img.getWidth(null), img.getHeight(null)),
containerDim = new Dimension(newWidth, newHeight),
resultDim = new Dimension();
collapseInside(resultDim, originalDim, containerDim);
final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gdev = genv.getDefaultScreenDevice();
final GraphicsConfiguration gcnf = gdev.getDefaultConfiguration();
final BufferedImage simg = gcnf.createCompatibleImage(resultDim.width, resultDim.height, Transparency.TRANSLUCENT);
//Painting input image to output image:
final Graphics2D g2d = simg.createGraphics();
g2d.drawImage(img, 0, 0, resultDim.width, resultDim.height, null); //#Docs "...is scaled if necessary.".
g2d.dispose();
return simg;
}
/**
* #param resultDim Output dimension (same aspect ratio as the original dimension, and inside containerDim).
* #param originalDim Original dimension.
* #param containerDim Dimension with the maximum width and maximum height.
*/
public static void collapseInside(final Dimension resultDim,
final Dimension originalDim,
final Dimension containerDim) {
resultDim.setSize(originalDim);
if (resultDim.width > containerDim.width) {
//Adjusting height for max width:
resultDim.height = ( resultDim.height * containerDim.width ) / resultDim.width;
resultDim.width = containerDim.width;
}
if (resultDim.height > containerDim.height) {
//Adjusting width for max height:
resultDim.width = ( resultDim.width * containerDim.height ) / resultDim.height;
resultDim.height = containerDim.height;
}
}
private static class CustomButton extends JPanel {
private Image img;
private String text;
public void setImage(final Image img) {
this.img = img;
final Dimension imgDim = new Dimension(img.getWidth(null), img.getHeight(null));
setMinimumSize(imgDim);
setPreferredSize(imgDim);
repaint();
}
public Image getImage() {
return img;
}
public void setText(final String text) {
this.text = text;
repaint();
}
public String getText() {
return text;
}
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
//Drawing the image:
g.drawImage(img, 0, 0, null);
//Drawing the text:
//For centering the text, I used code from:
//https://stackoverflow.com/questions/27706197/how-can-i-center-graphics-drawstring-in-java
final FontMetrics metrics = g.getFontMetrics(g.getFont());
final int x = (img.getWidth(null) - metrics.stringWidth(text)) / 2;
final int y = (img.getHeight(null) - metrics.getHeight()) / 2 + metrics.getAscent();
g.drawString(text, x, y);
}
}
private static void createAndShowGUI() {
final JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final CustomButton button = new CustomButton();
button.setSize(new Dimension(5, 5));
button.setImage(bi);
button.setText("Hello World");
//button.setHorizontalTextPosition(SwingConstants.CENTER);
//button.setOpaque(false);
//button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
button.addMouseListener(new MouseListener() {
private int count = 0;
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == button) {
//Get the mouse click position (in pixels),
//relative to the top-left corner of label:
final Point relativeClickPoint = e.getPoint();
//Obtain alpha value from the TYPE_INT_ARGB pixel:
final int pixel = bi.getRGB(relativeClickPoint.x, relativeClickPoint.y);
final int alpha = bi.getColorModel().getAlpha(pixel);
if (alpha > 0) { //Check if the pixel is not transparent.
if (count % 2 == 0) {
button.setText("Bye");
} else {
button.setText("Hello World");
}
count++;
}
}
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
final JPanel singleCenteredComponentJPanel = new JPanel(new GridBagLayout());
singleCenteredComponentJPanel.add(button);
frame.add(singleCenteredComponentJPanel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Alternatively, if you want normal buttons, istead of labels, it seems here you can reshape buttons as well !
Other than that, I have also tried the following:
1) Using JLayeredPanes. There is a tutorial explaining them, and I guessed they could probably have a method to obtain the order of the visible pane for a given Point, or something like that, but they don't.
2) Using Container.getComponentAt(Point) and Container.findComponentAt(Point) in the MouseListener, but (as I found out after testing) these methods don't "see through" non-opaque (+transparent) pixels.
3) Searching for reshaping or translucency in JPanels (inspired by How to Create Translucent and Shaped Windows), but nothing found.
4) How to Decorate Components with the JLayer Class with first line saying:
... enables you to draw on components and respond to component events without modifying the underlying component directly.
seems promising, but I'm done here.
That's it for me for now. Goodbye.

Calling Graphics.drawImage in the contructor of a JFrame Java [duplicate]

I'm new on working with Java and Netbeans. In many others languages, it's a simple stuff to do. But after broke my brain thinking, I couldn't. My doubt is simple to explain.
How can I show bitmaps (stored on Hard Drive) in runtime, in a commom JFrame, using java2D? What I need to edit, and or create? Is it simple to do?
Thanks in advance...
The basic process is use Graphics#drawImage to render an image you have previously loaded.
There are a number of things you need in order to achieve this...
An image to paint
A surface to paint on
Luckily, both these are relatively easy to achieve in Swing
See ImageIO for reading images, Reading/Loading an Image
See Performing Custom Painting for more details about how to perform painting in Swing...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ShowMyImage {
public static void main(String[] args) {
new ShowMyImage();
}
public ShowMyImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ImagePane pane = new ImagePane();
try {
pane.setImg(ImageIO.read(new File("C:\\hold\\thumbnails\\_MTCGAC__Pulling_Cords_by_Dispozition.png")));
} catch (IOException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage img;
public ImagePane() {
}
public void setImg(BufferedImage value) {
if (img != value) {
img = value;
repaint();
}
}
public BufferedImage getImg() {
return img;
}
#Override
public Dimension getPreferredSize() {
BufferedImage img = getImg();
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
BufferedImage img = getImg();
if (img != null) {
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight()- img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
}
g2d.dispose();
}
}
}
You need, as was told below to extend JPanel class and override paintComponent method in it.
public class DrawImageClass extends JPanel {
private static final long serialVersionUID = 1L;
private Image image;
public DrawImageClass(Image image) throws HeadlessException {
super();
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image, null, null);
}
}
And then create JFrame and add this class to it.
public class App {
private static final int WIDTH=480, HEIGHT = 640;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(HEIGHT, WIDTH));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
DrawImageClass panel = new DrawImageClass(ImageIO.read(new File(App.class.getResource("/1.png").getPath())));
panel.setPreferredSize(new Dimension(HEIGHT, WIDTH));
frame.add(panel);
frame.setVisible(true);
} catch (Exception e) {
System.out.println("wrong path or smth other");
}
}
}
Example for picture which is in resource folder in project
You can do this with the basic AWT package in java. You will need to extend the JFrame class and override its paintComponent method to display an image there. i.e.
public class MyFrame extends JFrame {
private java.awt.Image bitmap = ImageIO.read(new File("myBitmap.bmp"));
#Override
public void paintComponent(java.awt.Graphics graphics) {
int x = 0;
int y = 0;
int w = bitmap.getWidth(null);
int h = bitmap.getHeight(null);
graphics.drawImage(bitmap, x, y, w, h, this);
}
}
You can simply instantiate it then
MyFrame myFrame = new MyFrame();
myFrame.setPreferredSize(new Dimension(100, 100));
myFrame.setVisible(true);
This is a basic example of course.

Placing JButton on image

Aim: to place JButton on a top of an image that is loaded from a different class.
Problem: only JButton or image can be displayed
//main class
JPanel mPanel.add(new JButton("ddd") );
mPanel.add(drawbg);
class DrawBg extends JPanel {
public Dimension getPreferredSize() {
return new Dimension(1280, 720);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// background
Image img = new ImageIcon(getClass().getResource("/images/test_bg.jpg")).getImage();
int imgX = img.getWidth(null);
int imgY = img.getHeight(null);
g.drawImage(img, (getWidth() - imgX), (getHeight() - imgY), imgX, imgY, null);
}
Edit Ah yes, as ControlAltDel points out, you're not adding your JButton to your drawing JPanel instance. Just do that, and your problem is solved.
I don't see the bug in your code, so it might lie in code not shown. I do see a problem in that you're reading the image in, inside of paintComponent, something that should never be done as it will slow down painting and thus slow down the perceived responsiveness of your program. Also, why re-read in the image with each paint. Instead read it in once.
For example:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ButtonOnImg extends JPanel {
public static final String IMG_PATH = "https://duke.kenai.com/gun/Gun.jpg";
private BufferedImage img;
public ButtonOnImg() throws IOException {
URL url = new URL(IMG_PATH);
img = ImageIO.read(url);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, null);
}
}
#Override
public Dimension getPreferredSize() {
if (img == null) {
return super.getPreferredSize();
} else {
int w = img.getWidth();
int h = img.getHeight();
return new Dimension(w, h);
}
}
private static void createAndShowGui() {
try {
ButtonOnImg mainPanel = new ButtonOnImg();
mainPanel.add(new JButton("Button!"));
JFrame frame = new JFrame("ButtonOnImg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Which displays as:

Calling setVisible(true) on an already visible frame

Question:
What does calling setVisible(true) on a JFrame which is already visible do? I was digging through the source code of JFrame, and ultimately, it boils down to this function in Component which does nothing to a frame if it is already visible. Why does it act like revalidate(); repaint();? (See SSCCE below)
Motivation:
I am working on a java app, for which I wrote a class JImagePanel which extends JPanel and allows the user to set an image as the background (see SSCCE). I have found that after editing the background of the panel, I was having issues repainting the background to the correct size. After scouring the internet, I found that the following works:
if(frame.isVisible()) frame.setVisible(true);
Ultimately, I solved the issue using
panel.revalidate();
panel.repaint();
, which I think is the better solution, but it got me to thinking what setVisible(true) actually does on an already visible frame. From my viewpoint, it shouldn't work - but in fact it does.
SSCCE
Here is an example that illustrates my issue. If nothing else, hopefully you find this class extremely useful in the future.
NOTE: Updated source of this file can be found on the project homepage on GitHub of the project this was created for.
Enjoy!
package com.dberm22.utils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JImagePanel extends JPanel {
private static final long serialVersionUID = 6841876236948317038L;
private Image img = null;
private Position position = Position.CENTER;
public enum Position{
STRETCH,
CENTER,
FIT,
FILL,
NONE;
}
public JImagePanel() {
}
public JImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public JImagePanel(Image img) {
setBackgroundImage(img);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
if (this.position.equals(Position.STRETCH))
{
if(this.img != null) g2.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
else if (this.position.equals(Position.FILL) || this.position.equals(Position.FIT))
{
if(this.img != null)
{
double scaleFactor = getScaleFactor(new Dimension(img.getWidth(null), img.getHeight(null)), getSize());
int scaleWidth = (int) Math.round(img.getWidth(null) * scaleFactor);
int scaleHeight = (int) Math.round(img.getHeight(null) * scaleFactor);
//Image img_scaled = img.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);
g2.drawImage(scaleImage(img, scaleWidth, scaleHeight, getBackground()), (getWidth() - scaleWidth)/2, (getHeight() - scaleHeight)/2, scaleWidth, scaleHeight, null);
}
}
else if (this.position.equals(Position.CENTER)) { if(this.img != null) g2.drawImage(img, (getWidth() - img.getWidth(null))/2, (getHeight() - img.getHeight(null))/2, null); }
}
public void setBackgroundImage(String img)
{
setBackgroundImage(new ImageIcon(img).getImage());
}
public void setBackgroundImage(Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
repaint();
}
public static double getScaleFactor(int iMasterSize, int iTargetSize) {
double dScale = 1;
if (iMasterSize > iTargetSize) {
dScale = (double) iTargetSize / (double) iMasterSize;
} else {
dScale = (double) iTargetSize / (double) iMasterSize;
}
return dScale;
}
public double getScaleFactor(Dimension original, Dimension targetSize) {
double dScale = 1d;
if (original != null && targetSize != null) {
double dScaleWidth = getScaleFactor(original.width, targetSize.width);
double dScaleHeight = getScaleFactor(original.height, targetSize.height);
if (this.position.equals(Position.FIT)) dScale = Math.min(dScaleHeight, dScaleWidth);
else if(this.position.equals(Position.FILL)) dScale = Math.max(dScaleHeight, dScaleWidth);
}
return dScale;
}
public BufferedImage scaleImage(Image img, int width, int height, Color background) {
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setBackground(background);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
} finally {
g.dispose();
}
return newImage;
}
public void setBackgroundImagePosition(String pos)
{
if("Stretch".equals(pos)) setBackgroundImagePosition(Position.STRETCH);
else if("Center".equals(pos)) setBackgroundImagePosition(Position.CENTER);
else if("Fit".equals(pos)) setBackgroundImagePosition(Position.FIT);
else if("Fill".equals(pos)) setBackgroundImagePosition(Position.FILL);
else if("None".equals(pos)) setBackgroundImagePosition(Position.NONE);
}
public void setBackgroundImagePosition(Position pos)
{
this.position = pos;
repaint();
}
public static void main(String[] args)
{
JFrame frame = new JFrame("JImagePanel Test");
frame.setSize( Toolkit.getDefaultToolkit().getScreenSize());
frame.setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //sets appropriate size for frame
JImagePanel panel = new JImagePanel();
frame.add(panel);
frame.setVisible(true);
try {Thread.sleep(2000);} catch (InterruptedException e) {}
panel.setBackgroundImage("C:\\Users\\David\\Pictures\\Wood.jpg");
panel.setBackgroundImagePosition(JImagePanel.Position.STRETCH);
panel.revalidate(); // need to revalidate()
panel.repaint(); //doesnt work by itself
try {Thread.sleep(2000);} catch (InterruptedException e) {}
panel.setBackgroundImage("C:\\Users\\David\\Pictures\\Wood.jpg");
panel.setBackgroundImagePosition(JImagePanel.Position.FIT);
frame.setVisible(true); //also works --why?
}
}
Calling setVisible(true) on a JFrame which is already visible works for you because this ends up calling validate() internally, which in turn revalidates all subcomponents in the frame.
To see why, refer to the implementation of Component.setVisible(boolean b):
public void setVisible(boolean b) {
show(b);
}
public void show(boolean b) {
if (b) {
show();
} else {
hide();
}
}
But the show() method is overriden in Window (of which JFrame is a subclass). So this ends up calling Window.show():
public void show() {
if (peer == null) {
addNotify();
}
validate();
[...]
Hope this explains the behaviour you are seeing.
Assuming you are after a half-way clean implementation of your imagePanel:
let the panel do its own revalidation/-paint as need (vs application code)
do not call setXXSize, ever, not even internally in the panel
That's change your setters and override the getXXSize. Note that basing the sizing hints on the image size alone is not what you would want to do in real world code, you probably want to take super's hint into account as well (f.i. if the panel has children)
#Override
public Dimension getPreferredSize() {
if (img != null) {
return new Dimension(img.getWidth(this), img.getHeight(this));
}
return super.getPreferredSize();
}
public void setBackgroundImage(Image img) {
this.img = img;
// need to revalidate as our sizing hints might have changed
revalidate();
repaint();
}
public void setBackgroundImagePosition(Position pos) {
this.position = pos;
repaint();
}
Application code that uses it now simply calls the setters, nothing else:
JFrame frame = new JFrame("JImagePanel Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setLayout(new FlowLayout()); // just to see the effect of a pref-respecting layout
final JImagePanel panel = new JImagePanel();
frame.add(panel);
final Image[] images = new Image[]{
XTestUtils.loadDefaultImage(), XTestUtils.loadDefaultImage("500by500.png"), null};
Action toggleImage = new AbstractAction("toggle image") {
int index = 0;
#Override
public void actionPerformed(ActionEvent e) {
panel.setBackgroundImage(images[index]);
index = (index +1) % images.length;
}
};
Action togglePosition = new AbstractAction("toggle position") {
int index = 0;
#Override
public void actionPerformed(ActionEvent e) {
panel.setBackgroundImagePosition(Position.values()[index]);
index = (index +1) % Position.values().length;
}
};
frame.add(new JButton(toggleImage), BorderLayout.NORTH);
frame.add(new JButton(togglePosition), BorderLayout.SOUTH);
// size for frame
//frame.setSize(800, 800);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //sets appropriate
frame.setVisible(true);

JPanel does not fill containing JFrame

Hi so I'm writing an simple physics engine to understand object collisions and Java graphics a bit better, and so far I have successfully written the code to add the JPanel to the JFrame and allow them to show up somewhat the correct size, but when I view the actually program, the JPanel seems tobe the right size but it does not start in the upper corner of the window, but rather the upper left of the frame. I seem to have this problem alot where I want something to be at (0, 0) and starts in the upper corner of the frame rather than the panel. Here is my code:
I have an engine class that extends JFrame and contains the main method -->
package io.shparki.PhysicsEngine;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Engine extends JFrame{
public Engine(){
super("Physics Engine and Simulator");
setLayout(new BorderLayout());
add(new EnginePanel(), BorderLayout.CENTER);
pack();
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new Engine();
}
}
and this is my second class, the EnginePanel which extends JPanel and implements Runnable -->
package io.shparki.PhysicsEngine;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class EnginePanel extends JPanel implements Runnable{
private static final int WIDTH = 300;
private static final int HEIGHT = WIDTH / 16 * 9;
private static final int SCALE = 4;
public int getWidth() { return WIDTH * SCALE; }
public int getHeight() { return HEIGHT * SCALE; }
#Override
public Dimension getPreferredSize(){ return new Dimension(WIDTH * SCALE, HEIGHT * SCALE); }
private static final int FPS = 85;
private static final int PERIOD = 1000 / FPS;
private int currentFPS = 0;
private Thread animator;
private boolean running = false;
private Graphics dbg;
private Image dbImage = null;
public EnginePanel(){
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setVisible(true);
}
public void addNotify(){
super.addNotify();
startEngine();
}
public void startEngine(){
running = true;
animator = new Thread(this, "Animator");
animator.start();
}
public void stopEngine(){
running = false;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (dbImage != null){
g.drawImage(dbImage, 0, 0, null);
}
}
public void paintScreen(){
Graphics g;
try{
g = this.getGraphics();
if ( g != null && dbImage != null){
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
} catch(Exception ex) { System.out.println("Graphics Context Error : " + ex); }
}
public void run(){
running = true;
init();
Long beforeTime, timeDiff, sleepTime;
while(running){
beforeTime = System.currentTimeMillis();
updateEngine();
renderEngine();
paintScreen();
timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = PERIOD - timeDiff;
if (sleepTime <= 0){
sleepTime = 5L;
}
currentFPS = (int) (1000 / (sleepTime + timeDiff));
try{
Thread.sleep(sleepTime);
} catch (InterruptedException ex) { ex.printStackTrace(); }
}
}
private TextField FPSTextField;
public void init(){
FPSTextField = new TextField("Currnet FPS: " + currentFPS, 25, 25);
}
public void updateEngine(){
FPSTextField.setText("Currnet FPS: " + currentFPS);
}
public void renderEngine(){
if (dbImage == null){
dbImage = createImage((int)getWidth(), (int)getHeight());
if (dbImage == null){
System.out.println("Graphical Context Error : DBImage is Null");
return;
} else {
dbg = dbImage.getGraphics();
}
}
Graphics2D g2d = (Graphics2D) dbg;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
FPSTextField.render(g2d, Color.MAGENTA);
}
}
I'm not quite sure why this keeps happening and I have searched for help but can not find the answer. Thanks in advance for all who help :)
EDIT: Added code for the TextField object:
package io.shparki.PhysicsEngine;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
public class TextField{
private Point location;
public Point getLocation(){ return location; }
public double getX() { return location.getX(); }
public double getY() { return location.getY(); }
private String text;
public void setText(String text) { this.text = text; }
public String getText() { return this.text; }
public TextField(String text, int x, int y){
this.location = new Point(x, y);
this.text = text;
}
public TextField(String text, Point location){
this.location = location;
this.text = text;
}
public void render(Graphics g){
g.drawString(text, (int)location.getX(), (int)location.getY());
}
public void render(Graphics2D g2d){
g2d.drawString(text, (int)location.getX(), (int)location.getY());
}
public void render(Graphics g, Color color){
g.setColor(color);
g.drawString(text, (int)location.getX(), (int)location.getY());
}
public void render(Graphics2D g2d, Color color){
g2d.setColor(color);
g2d.drawString(text, (int)location.getX(), (int)location.getY());
}
public void render(Graphics g, Color color, Font font){
g.setColor(color);
g.setFont(font);
g.drawString(text, (int)location.getX(), (int)location.getY());
}
public void render(Graphics2D g2d, Color color, Font font){
g2d.setColor(color);
g2d.setFont(font);
g2d.drawString(text, (int)location.getX(), (int)location.getY());
}
}
The preferred size of the JPanel EnginePanel restricts the panel from being resized the JFrame is rendered non-resizable. Invoke JFrame#pack after calling setResizable(false). Also move setLocationRelativeTo after pack so that the frame appears centered.
pack();
setLocationRelativeTo(null);
setVisible(true);
If you use a GridBagLayout in the JFrame, then the JPanel will be centered if it is the only thing you add to the JFrame. It will even stay in the center if you resize the window.
Also, the reason the coordinates seem off to you is that you are viewing it as an (x, y) coordinate while it is actually a (row, col) coordinate like in a 2D Array.

Categories

Resources