Re: Help needed with Images.
Try using the ImageIO class for reading the image.
Is the image file being found by the program? What is the value of img after the getImage call?
Re: Help needed with Images.
How do I check the value of img, and use ImageIO?
Re: Help needed with Images.
Re: Help needed with Images.
I changed my gui class to the code below, but the drawing still doesn't show up.
Code java:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui extends JPanel {
BufferedImage img;
chatBox chatBox;
public gui(chatBox chatBox){
JFrame f = new JFrame("BrettScape");
f.setSize(640,480);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
try {
img = ImageIO.read(new File("testtile.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateScreen(){
repaint();
}
public void paintComponent(Graphics g){
System.out.println("hey");
g.drawImage(img, 0, 0, null);
}
}
Re: Help needed with Images.
Some things to check:
1. Is there any error information displayed (like file not found exceptions, or ioexceptions)? If so, please post them.
2. Does the image contain something (for example, is it not just a white rectangle being drawn onto a white background)?
3. Have you considered the size of the image (too small, too big)? This method doesn't properly scale the image to fit the region. There are other paint methods you can use to draw a scaled version of the image, or you can scale the image before-hand (either in code or externally)
edit:
as a side note, you don't want to put any print statements into the paintComponent method. This method gets called quite frequently, and if you have print statements, it can significantly reduce the performance of your application, as well as flooding the console and making any other console output pretty much useless.
Re: Help needed with Images.
1. No exceptions.
2. the picture is 4tiles consiting of grass dirt water stone.
3. the image is 96x64 the JFrame is 640x480
side note. Where do I put it then?
Re: Help needed with Images.
Quote:
as a side note, you don't want to put any print statements into the paintComponent method.
I disagree. For debugging one time, its useful to see when/if a method is being called. I use print statements a lot to verify that. Then remove them after the they've shown what is happening.
If fact in this case the print shows what is wrong!!!
The hey is printed before the image is loaded. If you add a print after img is given a value you will see this:
Quote:
hey
img=BufferedImage@6bade9: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@166afb3 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1024 height = 537 #numDataElements 3 dataOff[0] = 0
paint is called before the image is available.
Move the setVisible to after you have the image, the image is shown and you'll see:
Quote:
img=BufferedImage@126f75b: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@139b78e transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1024 height = 537 #numDataElements 3 dataOff[0] = 0
hey
Also you need to add the JPanel(this) to the frame.
Re: Help needed with Images.
I just remembered... For some reason my paintComponent method is NEVER called. ("hey" never prints in the terminal) I even put a while loop to keep the program running.
Code java:
while(true)
gui.updateScreen();
Here is my current gui.class
Code java:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui extends JPanel {
BufferedImage img;
chatBox chatBox;
public gui(chatBox chatBox){
JFrame f = new JFrame("BrettScape");
f.setSize(640,480);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
img = ImageIO.read(new File("testtile.png"));
} catch (IOException e) {
e.printStackTrace();
}
f.setVisible(true);
}
public void updateScreen(){
repaint();
}
public void paintComponent(Graphics g){
g.drawImage(img, 0, 0, null);
}
}
Re: Help needed with Images.
Did you read my last post? I pointed out several problems with your code and showed consoles where "hey" is printed.
The code works with those changes.
BTW This would show you a problem: System.out.println("hey img=" + img); // show contents of img variable
Re: Help needed with Images.
I did, I changed the setVisible to after the image is loaded.
Re: Help needed with Images.
Re: Help needed with Images.
Re: Help needed with Images.
Ok go back and read my post#8 again.
Did you do what I said on the last line of the post: you need to add the JPanel(this) to the frame.
Re: Help needed with Images.
I typed and I got an error. I'm sorry, but I'm very bad with images. I've looked at many different tutorials, but I just can't get it to work by myself.
Re: Help needed with Images.
Please copy and post the full text of the error message here.
What was the code you posted supposed to do?
What is the 'j' variable?
Your problem has nothing to do with images. It has to do with adding a component to a container.
Work your way thru the problem:
What variable is the frame you have created? Its f
How do you add a component to f? use its add() method
What do you want to add to f?
You want to add the JPanel that has the paintComponent method in it that will draw the image.
That is the gui class object.
How do you get a reference to the current object? its this
So putting it all together: f.add(this); // add the gui object to the frame
Re: Help needed with Images.
That worked! Thank you so much!
Quote:
What is the 'j' variable?
I meant f not j
Re: Help needed with Images.
Its better to copy and paste from the actual program than to try to remember what the code was.
Now that you have the code working, you need to rework it. Putting the JFrame code in the gui constructor doesn't make sense.
For testing your gui class, add a main() method to it and move all the JFrame code out of the gui constructor to the main method.
Leave the image code there because that belongs to the gui class.
After testing, you can comment out the main() method.
Re: Help needed with Images.
Quote:
Originally Posted by
Norm
I disagree. For debugging one time, its useful to see when/if a method is being called. I use print statements a lot to verify that. Then remove them after the they've shown what is happening.
true, I guess I tend to use debug tools more (breakpoints, watches, variables, stack traces, etc.).
Re: Help needed with Images.
They're great if you have em.