ImageIcon is displayed as a small square - java

where is the problem. It does not show image properly.
default.png = *********
what i see = *********
I dont think there is a problem with png. It is also in my src and I refreshed it on eclipse.
codes:
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle( "test");
jf.setLayout( new FlowLayout());
jf.setSize(350, 450);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new Panel());
}
}
panel:
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Panel extends JPanel {
// PROPERTIES
public ImageIcon icon;
// CONSTRUCTORS
public Panel() {
icon = new ImageIcon(this.getClass().getResource("default.png"));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(icon.getImage(), 0, 0, null);
}
}

This answer addresses my criticism of the answer by the OP. The example incorporates the advice added in various comments.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.border.LineBorder;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.imgur.com/o0E0aGD.png");
BufferedImage bi = ImageIO.read(url);
JFrame jf = new JFrame();
jf.setTitle("test");
jf.setLayout(new FlowLayout());
//jf.setSize(350, 450); just pack()
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new BackgroundImagePanel(bi));
jf.pack();
jf.setMinimumSize(jf.getSize());
jf.setVisible(true);
}
}
class BackgroundImagePanel extends JPanel {
public Image img;
// CONSTRUCTOR
public BackgroundImagePanel(Image img) {
this.img = img;
this.setBorder(new LineBorder(Color.RED, 2));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
// g.drawImage(img, 0, 0, getWidth(), getHeight(), this); // Better!
}
#Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int w = d.width>img.getWidth(this) ? d.width : img.getWidth(this);
int h = d.height>img.getHeight(this) ? d.height : img.getHeight(this);
return new Dimension(w, h);
}
}

I solved this by adding at Panel class constructor:
setPreferredSize(new Dimension( 500, 500 ));

Related

Java JFrame rectangle in window

so i'm trying to put Rectangle2D.Float in window using JFrame but when i'm compiling code i'm getting just blank window without rectangle. Can you guys take look on it and tell me what i'm doing wrong?
package zestaw8;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class Plansza85 extends JPanel
{
Shape figura;
Plansza85(Shape figura)
{
this.figura=figura;
}
}
public class Zestaw8_cw85
{
public static void main(String[] args)
{
Shape obj1;
obj1=new Rectangle2D.Float(100,100,140,140);
zestaw8.Plansza85 p;
p=new zestaw8.Plansza85(obj1);
JFrame jf=new JFrame();
jf.setTitle("Plansza p");
jf.setSize(400,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.add(p);
}
}
You seem to have a misunderstanding of how painting works in Swing.
Start by looking at Performing Custom Painting, Painting in Swing and 2D Graphics. Rectangle2D is a graphics primitive, which needs to be painted through the normal custom painting process
As per the common recommendations of Performing Custom Painting you should override the paintComponent method of the Plansza85 and paint the Shape through the Graphics2D API, something like...
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Shape obj1;
obj1 = new Rectangle2D.Float(100, 100, 140, 140);
Plansza85 p;
p = new Plansza85(obj1);
JFrame jf = new JFrame();
jf.setTitle("Plansza p");
jf.add(p);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
});
}
class Plansza85 extends JPanel {
Shape figura;
Plansza85(Shape figura) {
this.figura = figura;
}
#Override
public Dimension getPreferredSize() {
if (figura == null) {
return super.getPreferredSize();
}
Rectangle2D bounds = figura.getBounds2D();
double width = bounds.getMaxX();
double height = bounds.getMaxY();
return new Dimension((int)width + 1, (int)height + 1);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getForeground());
g2d.draw(figura);
g2d.dispose();
}
}
}
for example.
I've also overridden the getPreferredSize method to generate an appropriate sizing hint for the component based on the size of the shape, I've done this because I dislike guess work and the window also includes variable sized borders and title bars which will change the size the panel if you only rely on setSize
You need to override the paintComponent method of Plansza85
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class Plansza85 extends JPanel {
private Shape figura;
Plansza85(Shape figura) {
this.figura = figura;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.draw(figura);
}
}
public class Zestaw8_cw85 {
public static void main(String[] args) {
Shape obj1;
obj1 = new Rectangle2D.Float(100, 100, 140, 140);
Plansza85 p;
p = new Plansza85(obj1);
JFrame jf = new JFrame();
jf.setTitle("Plansza p");
jf.setSize(400, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(p);
jf.setVisible(true);
}
}
Hope it helps!

Set Transparent Color for the Whole JPanel

I have a JPanel with a JLabel which owns an Icon picture.
how do I set a transparent red color at the top of the whole JPanel (including JLabel Icon)?
I have the transparent backgriound color on for the panel but I want the whole panel including the picture and everything get this transparent color. something like a transparent colorful glass at the top of the JPanel
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TransparentJLabel {
private static final String IMAGE_PATH = "http://duke.kenai.com/Oracle/OracleStratSmall.png";
private static void createAndShowUI() {
JPanel panel = new JPanel();
panel.setBackground(Color.pink);
URL imageUrl;
try {
imageUrl = new URL(IMAGE_PATH);
BufferedImage image = ImageIO.read(imageUrl);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
panel.add(label);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("TransparentJLabel");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
If you just need a layered panel over the whole contentPane, a simple glassPane will do fine (override it's paintComponent(...) method). For example:
JPanel glassPane = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(new Color(0, 100, 0, 100));
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
}
};
glassPane.setOpaque(false);
frame.setGlassPane(glassPane);
frame.getGlassPane().setVisible(true);
However, if you want a layered panel over only one JPanel, I would use JLayer combined with LayerUI, as MadProgrammer already mentioned. You will need a custom LayerUI in which you override the paint(Graphics g, JComponent c) method. I know that sounds dangerous, but I honestly don't know of another way of doing it...
I've provided an example below, this is the output:
As you can see, panel1 (or more accurately, the JLayer) is slighty transparent (RGBA = "0, 100, 0, 100") and panel2 is normal.
Code:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
public class Example {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
public Example() {
JFrame frame = new JFrame("Example");
JPanel panel1 = new JPanel();
panel1.add(new JButton("Panel 1"));
MyLayerUI layerUI = new MyLayerUI();
JLayer<JPanel> panel1Layer = new JLayer<JPanel>(panel1, layerUI);
panel1.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (layerUI.hasOverlay()) {
layerUI.setOverlay(false);
} else {
layerUI.setOverlay(true);
}
panel1Layer.repaint();
}
});
JPanel panel2 = new JPanel();
panel2.add(new JButton("Panel 2"));
JPanel contentPane = new JPanel(new GridLayout(2, 1));
contentPane.add(panel1Layer);
contentPane.add(panel2);
frame.setContentPane(contentPane);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class MyLayerUI extends LayerUI<JPanel> {
private boolean overlay = true;
#Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
if (hasOverlay()) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(new Color(0, 100, 0, 100));
g2.fillRect(0, 0, c.getWidth(), c.getHeight());
g2.dispose();
}
}
public boolean hasOverlay() {
return overlay;
}
public void setOverlay(boolean overlay) {
this.overlay = overlay;
}
}

Java window doesn't repaint properly until I resize the window manually

I am using a quite basic setup with a class extending JPanel, which I add to a JFrame.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class PinTestMCVE extends JPanel implements ActionListener{
BufferedImage loadedImage;
JButton calcButton;
public static void main(String[] args) {
new PinTestMCVE();
}
public PinTestMCVE() {
loadedImage = getTestImage();
JPanel toolbarPanel = new JPanel();
calcButton = new JButton("calcButton...");
toolbarPanel.add(calcButton);
calcButton.addActionListener(this);
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(toolbarPanel, BorderLayout.NORTH);
jf.getContentPane().add(this, BorderLayout.CENTER);
jf.setSize(1250, 950);
jf.setVisible(true);
}
public void paintComponent(Graphics g) {
g.drawImage(loadedImage, 0, 0, this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent " + e.getActionCommand());
if(e.getSource().equals(calcButton)){
this.repaint();
}
}
//Please ignore the inner workings of this
public static BufferedImage getTestImage(){
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(Color.GRAY);
g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.gray);
int x = 5;
int y = 7;
GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y, Color.blue);
g2d.setPaint(redtowhite);
g2d.fill(new RoundRectangle2D.Double(x, y, 200, 200, 10, 10));
return image;
}
}
What happens is that INITIALLY the window is painted properly, but once paintComponent is called, a strip of the old image (with the same height as the toolbar panel) is visible below the newly painted images - similar to playing card sticking out from a deck. But then, if I manually resize the window by for instance dragging the border, the background is grayed out as it should.
What is going on and how do I fix this?
As outlined here, you need to pack() the frame before calling setVisible(). You can override getPreferredSize() to specify a suitable initial Dimension. Also consider using a Border. See also Initial Threads.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;
public class PinTestMCVE extends JPanel implements ActionListener{
private static final int SIZE = 200;
BufferedImage loadedImage;
JButton calcButton;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new PinTestMCVE();
}
});
}
public PinTestMCVE() {
loadedImage = getTestImage();
JPanel toolbarPanel = new JPanel();
calcButton = new JButton("calcButton...");
toolbarPanel.add(calcButton);
calcButton.addActionListener(this);
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(toolbarPanel, BorderLayout.NORTH);
jf.add(this, BorderLayout.CENTER);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(loadedImage, 0, 0, this);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent " + e.getActionCommand());
if(e.getSource().equals(calcButton)){
this.repaint();
}
}
//Please ignore the inner workings of this
public static BufferedImage getTestImage(){
BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(Color.GRAY);
g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.gray);
GradientPaint redtowhite = new GradientPaint(5, 5, Color.red, SIZE, 5, Color.blue);
g2d.setPaint(redtowhite);
g2d.fill(new RoundRectangle2D.Double(5, 5, SIZE - 10, SIZE - 10, 10, 10));
return image;
}
}

How do I draw an image to a JPanel or JFrame?

How do I draw an Image to a JPanel or JFrame, I have already read oracle's tutorial on this but I can't seem to get it right. I need the image "BeachRoad.png" to be displayed on a specific set of coordinates. Here is what I have so far.
public class Level1 extends JFrame implements ActionListener {
static JLayeredPane EverythingButPlayer;
static Level1 l1;
public Level1() {
EverythingButPlayer = new JLayeredPane();
BufferedImage img = null;
try {
img = ImageIO.read(new File("BeachRoad.png"));
} catch (IOException e) {
}
Graphics g = img.getGraphics();
g.drawImage(img,0, 0, EverythingButPlayer);
this.add(EverythingButPlayer);
}
And in the Main(),
l1 = new Level1();
l1.setTitle("poop");
l1.setSize(1920, 1080);
l1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1.setVisible(true);
Thanks in advance!
Try this:
package com.sandbox;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SwingSandbox {
public static void main(String[] args) throws IOException {
JFrame frame = buildFrame();
final BufferedImage image = ImageIO.read(new File("C:\\Projects\\MavenSandbox\\src\\main\\resources\\img.jpg"));
JPanel pane = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
frame.add(pane);
}
private static JFrame buildFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
return frame;
}
}
There are a lot of methods, but I always override the paint(Graphics g) of a JComponent and use g.drawImage(...)
edit: I was making a sample, but Daniel Kaplan did it perfectly, look at his answer :)

How to set background logo in my JPanel or into JFrame?

How can I set the wall1.jpg as background to my JFrame or JPanel?
public class JBackGroundImageClass extends JFrame
{
Image img;
private final JPanel JPanel;
public JBackGroundImageClass()
{
setLayout (new BorderLayout ());
setBounds(22,33,400, 400);
setVisible(true);
img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");
JPanel = new JPanel()
{
public void paintComponent(Graphics g)
{img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");
g.drawImage(img, 0, 0, null);
}
};
this.add("North" , JPanel);
JPanel.setSize(400, 400);
JPanel.setBackground(Color.red);
JPanel.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JBackGroundImageClass().setVisible(true);
}
}
Here's one common approach:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JBackGroundImageClass extends JFrame {
private Image img;
public JBackGroundImageClass() {
this.setLayout(new BorderLayout());
try {
img = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
e.printStackTrace(System.err);
}
this.add(new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new JBackGroundImageClass().setVisible(true);
}
});
}
}
In the answer by trashgod, there is no reason to do custom painting. The image is painted at its preferred size. Therefore you can just add an ImageIcon to a JLabel and add the label to the frame. You can set the layout manager of the label to be anything you want, the same as you can for a panel.
You would use custom painting is you want to scale the image in which case you would use:
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)

Categories