Issue with drawing png file into swing GUI - java

I am facing an issue that I am not able to draw an png image into my swing GUI into JPanel (which I am adding into JScrollPane after).
Here is my code I am trying to use for this.
if(processCreated == true){
System.out.println("updating tree of organisms");
processCreated = false;
updateTree();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Svet.class.getName()).log(Level.SEVERE, null, ex);
}
tree = new File(path+"/out.png"); //File
try {
treeImage = ImageIO.read(tree); //BufferedImage
tp = new TreePane(treeImage.getScaledInstance( 500, 500 ,treeImage.SCALE_SMOOTH)); // my class, implementation below
treeOutput.add(tp); //adding "JPanel" with picture into GUI
treeOutput.repaint();
} catch (IOException ex) {
Logger.getLogger(Svet.class.getName()).log(Level.SEVERE, null, ex);
}
}
After this the JScrollPane remains empty. Is there anything wrong with my code? You can find what is what in comments.
Here is my TreePane class
public class TreePane extends JPanel{
Image img;
public TreePane( Image img ){
this.img = img;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int imgX = 0;
int imgY = 0;
g.drawImage(img, imgX, imgY, this);
}
}

Very likely your TreePane is layed out to size 0,0 and therefore you'll never see anything. Try adding a breakpoint and inspecting in paintComponent - it might not even ever get called.
To fix, you should explicitly set the size of your TreePane before adding it to the JScrollPane.
tp = new TreePane(treeImage.getScaledInstance( 500, 500 ,treeImage.SCALE_SMOOTH)); // my class, implementation below
tp.setSize(myWidth, myHeight);
treeOutput.add(tp); //adding "JPanel" with picture into GUI
treeOutput.repaint();

Related

How to load an image into a JPanel using Java

I would like to use an image as the background of a JPanel.
It needs to be loaded from a relative file path.
private void createBackground() {
try {
BufferedImage backgroundImage = ImageIO.read(new File("C:/Users/Developer/workspace/Java/BSC_Project/Application/src/resources/background.jpg"));
JLabel background = new JLabel(new ImageIcon(backgroundImage));
this.add(background);
} catch(IOException e) {
System.out.println(e.toString());
}
}
My current code is not working. Any help would be appreciated.
So can't comment(need 50 rep) but your file path is completely wrong
you need it to be something like this
new File("C:/Users/"Insert Username"/Desktop/workspace/Java/BSC_Project/Application/src/resources/background.jpg")
except I'm not on your computer so you need to figure out your own file path, This would be assuming your workspace folder is on your Desktop which it almost certainly isn't, do you understand?
Well if you want your code to have a panel this is what you would do...
private void createBackground() {
try {
BufferedImage backgroundImage = ImageIO.read(new File("C:/Users/Developer/workspace/Java/BSC_Project/Application/src/resources/background.jpg"));
JPanel panel = new JPanel();
JLabel background = new JLabel(new ImageIcon(backgroundImage));
panel.add(background);
this.add(panel);
} catch(IOException e) {
System.out.println(e.toString());
}
}
but in your case it looks to me like you want a frame background as image... to which you can set the image background to which for that one you can try this code
JFrame f = new JFrame ("SettingBackGround");
try{
f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Med.jpg")))));
}catch (IOException e){
System.out.println("Image Doesnt Exist");
}
f.setVisible(true);
f.setResizable(false);
f.pack();
}
}
I hope this helps though.
public WelcomeView() {
initComponents();
try {
image = ImageIO.read(new File("C:\\Users\\Developer\\workspace\\Java\\BSC_Project\\Application\\src\\application\\resources\\background.png"));
} catch (IOException ex) {
System.err.println(ex.toString());
}
}
private BufferedImage image;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 2, 0, null); // see javadoc for more info on the parameters
}

Resizing JFrame to fit JPanel

I have a GUI, which consists of a JFrame and Menu bar. Inside the JFrame is my custom Panel (extending JPanel), which is initially hidden. The user chooses an image (JFileChooser), this image is passed to the panel and drawn (using paintIcon). The panel is resized to fit the image and then set visible. I want to resize my JFrame to fit the panel, but I cannot seem to get it to work. I have tried using the pack() method, but this just makes a tiny GUI, that is the size of the JMenu! I have also tried resizing both the frame and the JPanel using the getIconWidth() and getIconHeight() properties of the ImageIcon, but while this correctly sizes the panel it does not size the JFrame. Any ideas as to how I would do this correctly?
The block of code where I am setting the size of the JFrame is here:
#Override
public void actionPerformed(ActionEvent e)
{
JFileChooser imgChooser = new JFileChooser();
JMenuItem evtSource = (JMenuItem) e.getSource();
String srcText = evtSource.getText();
if (srcText.equals("Add Image..."))
{
imgChooser = new JFileChooser();
imgChooser.showOpenDialog(frmMain);
chosenImage = imgChooser.getSelectedFile();
try
{
loadedImage = ImageIO.read(chosenImage);
}
catch (IOException ex)
{
String errorMsg = ex.getMessage();
JOptionPane.showMessageDialog(frmMain, "Error while loading file: " + errorMsg, "Error!", JOptionPane.ERROR_MESSAGE);
}
panelImage = new ImageIcon(loadedImage);
frmMain.setSize(panelImage.getIconWidth(), panelImage.getIconHeight() + mbMenu.getHeight());
displayImage.loadImage(panelImage);
displayImage.setVisible(true);
}
}
The code from the relevant sections of the "displayImage" custom panel is below:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (imgIsLoaded)
{
panelImage.paintIcon(this,g2,0,0);
resizePanel(panelImage.getIconWidth(), panelImage.getIconHeight());
}
}
public void loadImage(ImageIcon i)
{
panelImage = i;
imgIsLoaded = true;
}
public void resizePanel(int w, int h)
{
this.setSize(w, h);
}
Override getPreferredSize() in the panel to return a default value (e.g. 400x400) if it has no image, and the size of the image otherwise.
After setting an image, call frame.pack().
BTW - if all the panel does is paint the image, I'd opt for a JLabel instead.

Image inside JFrame - Swing/AWT

Some days ago, I wasted a lot of time searching some way to show a image in a JFrame. And here is my final solution:
jPanel1 = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g) {
BufferedImage image = null;
try {
BufferedImage in = ImageIO.read(Startup.class.getResource("imagem.jpg"));
image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
g.drawImage(in, 0, 0, null);
} catch (Exception ex) {}
super.paintComponents(g);
}
};
I just want to know, if it is the one way to do that, or exists another solutions like a image component on Swing o AWT that can be easily used?
You can show an image using JLabel, which is much simpler than your solution. For example:
label.setIcon(new ImageIcon("Path/to/your/image.jpg"));

BufferedImage in JFrame doesnt Show up

trying to get an image to print into a window. Everything runs without errors, and it also works if I replace the drawImage with another graphics class. However, the window is missing the image, and i'm not sure why. Again, the JFrame stuff and Graphics work fine with drawing other graphics, but only doesn't draw the image here. Thanks.
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class GraphicsMovement2 extends JApplet{
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
JApplet example = new GraphicsMovement2();
JFrame frame = new JFrame("Movement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(example);
frame.setSize(new Dimension(1366,768)); //Sets the dimensions of panel to appear when run
frame.setVisible(true);
}
public void paint (Graphics page){
page.drawImage(image, 100, 100, 100, 100, Color.RED, this);
}
}
You've defined image twice...
BufferedImage image = null;
public static void main(String args[]){
BufferedImage image = null;
This essentially means that by the time you get to the paint method, it is null as you haven't initialized the instance variable.
Another problem you will have is the fact that you are trying to load the image from a static reference but the image isn't declared as static. Better to move this logic into the constructor or instance method.
Don't use JApplet as your container when you're adding to a JFrame, you're better of using something like JPanel. It will help when it comes to adding things to the container.
YOU MUST CALL super.paint(g)...in fact, DON'T override the paint method of top level containers like JFrame or JApplet. Use something like JPanel and override the paintComponent method instead. Top level containers aren't double buffered.
The paint methods does a lot of important work and it's just easier to use JComponent#paintComponent ... but don't forget to call super.paintComponent
UPDATED
You need to define image within the context it is going to be used.
Because you declared the image as an instance field of GraphicsMovement2, you will require an instance of GraphicsMovement2 in order to reference it.
However, in you main method, which is static, you also declared a variable named image.
The paint method of GraphicsMovement2 can't see the variable you declared in main, only the instance field (which is null).
In order to fix the problem, you need to move the loading of the image into the context of a instance of GraphicsMovement2, this can be best achived (in your context), but moving the image loading into the constructor of GraphicsMovement2
public GraphicsMovement2() {
try {
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
ImageInputStream imgInpt = new FileImageInputStream(file);
image = ImageIO.read(file);
}
catch(FileNotFoundException e) {
System.out.println("x");
}
catch(IOException e) {
System.out.println("y");
}
}
The two examples below will produce the same result...
The Easy Way
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
public ImagePane() {
setLayout(new BorderLayout());
ImageIcon icon = null;
try {
icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
} catch (Exception e) {
e.printStackTrace();
}
add(new JLabel(icon));
}
}
}
The Hard Way
public class TestPaintImage {
public static void main(String[] args) {
new TestPaintImage();
}
public TestPaintImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage background;
public ImagePane() {
try {
background = ImageIO.read(new File("/path/to/your/image"));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
Take the time to read through the tutorials
Creating a GUI With JFC/Swing
Performing Custom Painting
Your class shouldn't extend JApplet when you're not even using applets -- this makes no sense. Instead
Have your drawing class extend JPanel
Draw in the JPanel's paintComponent method
Add this JPanel to the JFrame's contentPane.
Read the Swing painting tutorials. You can't guess at this stuff and expect it to work, and the tutorials will show you how it's done correctly.
Don't mix file deviders,
File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
should be replaced with:
File file = new File("C:/Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");

Added BufferedImage is only 2x2 pixels

I have a script that cycles (almost like a slideshow) through a Vector object (flipBook), using a Thread (animationThread), and adds them to a JPanel. However, the added image is only 2x2 pixels large.
I've verified the images are 50x50, but they don't appear to be properly showing.
Here's some of the code going on behind the Thread instance. I'm not entirely sure which code would be beneficial for finding the source for.
public void startThread() {
if (flipWidth != 0 && flipHeight != 0) {
System.out.println("[ AnimationAsset ] " + "We're starting the thread");
Runnable r = new Runnable() {
#Override
public void run() {
runWork();
}
};
animationThread = new Thread(r, "AnimationThread");
animationThread.start();
going = true;
}
}
private void runWork() {
try {
while (going) {
repaint();
flipIndex = (flipIndex + 1) % numFlips;
System.out.println("[ AnimationAsset ] flipIndex: " + flipIndex);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("[ AnimationAsset ] " + "Interrupted");
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
System.out.println("[ AnimationAsset ] " + flipIndex);
((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);
}
and adds them to a JPanel
This is not a Swing code. This is AWT code.
You would never override the update() and paint() methods this way when using Swing. Get rid of this code and start over.
To do this in Swing I would use a JLabel with an Icon and add the label to the frame.
Then, to do animation in SWing your should use a Swing Timer.
When the timer fires you simply use the setIcon(...) method of the label to replace the old icon with your new icon.
Emm you have a strange drawing code as
((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);
You can see the docs here... to use Graphic2D drawImage() method right
the most common way of image drawing is to paint image right on JComponent, as a rule, the JLabel. Here is a component example
public class MyLabel extends JLabel
{
private Image image;
public MyLabel(Image image)
{
this.image=image;
}
public void paintComponent(Graphics g)
{
g.drawImage(this.image,x,y,width,height,null);
}
}
so here you can use the component as a common swing object
public class MyPanel extends JPanel
{
public MyPanel()
{
Image image=null;
try{
image=ImageIO.read(new File("image.png"));
}
catch (IOException e) {
}
this.add(new MyLabel(image));
}
}
Good luck

Categories

Resources