Click to See Complete Forum and Search --> : getting image from file in standalone application?
agent_x91
10-12-2004, 04:43 PM
I've used getImage() to get an image from a file before in applets, but I just found out that it's a method of java.applet.Applet... is there an equivalent for a window in a standalone application? I'm using a class which extends JFrame.
Java_Warrior
10-12-2004, 04:47 PM
you could use Toolkit.getImage()...but an easier way:
Image myImage = new ImageIcon("my_image.jpg").getImage();
agent_x91
10-12-2004, 04:50 PM
okay, cheers
agent_x91
10-12-2004, 04:52 PM
do I need to import any additional packages/Objects? it cannot resolve symbol (ImageIcon)
I don't get how a different object's constructor can be used on the Image variable...:confused:
HaganeNoKokoro
10-12-2004, 04:55 PM
it's javax.swing.ImageIcon, I believe
Java_Warrior
10-12-2004, 04:55 PM
BTW:
the ImageIcon class is located in javax.swing
the Image abstract class is located in java.awt, and cannot be instantiated.
Hope that helps! :-)
agent_x91
10-12-2004, 05:00 PM
yeah I know Image cannot be instantiated, i just didn't understand how a different object's constructor could construct an image.
well, I'm more used to getImage(URL) so I'll use Toolkit.getImage()
Java_Warrior
10-12-2004, 05:02 PM
OK, here is an example for that then:
Image myImage = Toolkit.getDefaultToolkit().getImage("my_image.jpg");
agent_x91
10-12-2004, 05:09 PM
don't worry, I figured that part out already. however, when I draw the image to the buffer, and then to the window, the window is just entirely gray. any reason for this?
Java_Warrior
10-12-2004, 05:15 PM
I assume that you have extended JFrame?
If so, you can use this method for drawing:
public void paint(Graphics g) {
super.paint(g);
g.drawImage(myImage, x, y, null);
}
where:
Image myImage = ...;
int x = ...;
int y = ...;
null - this is an ImageObserver component which isn't necessary
agent_x91
10-12-2004, 05:16 PM
It also now throws a large list of exceptions under java.lang.nullPointerException in the command prompt...
agent_x91
10-12-2004, 05:18 PM
Originally posted by Java_Warrior
I assume that you have extended JFrame?
If so, you can use this method for drawing:
public void paint(Graphics g) {
super.paint(g);
g.drawImage(myImage, x, y, null);
}
where:
Image myImage = ...;
int x = ...;
int y = ...;
null - this is an ImageObserver component which isn't necessary
hey I know that much don't treat me like a fool. I used buffer.drawImage(image,0,0,null); then drew the image to the window in the paint function. but originally it made the whole window grey - now it just leaves it as it is and paints nothing. a little help?
plus, the exceptions in the command prompt come up when it's run.
Java_Warrior
10-12-2004, 05:19 PM
Can you post your code please? That way I can look it over and help.
agent_x91
10-12-2004, 05:21 PM
it's the very beginning of the main structure for a game i'm creating. It should be fairly clear as it's well commented.
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Game extends JFrame
implements MouseListener,MouseMotionListener,KeyListener,WindowListener
{
final int Y_ZERO=31; //Allow for titlebar height
final int X_ZERO=5; //Allow for edge width
static Game gM; //stores the application object
Graphics backstage; //object for adding to the buffer
Image buffer; //buffer
Color startBackCol=new Color(80,10,80); //startGame background colour
Color startForeCol=new Color(40,40,40,110); //startGame foreground colour - 4th arg = opacity
Font startHeaderFont=new Font("Plump MT",Font.BOLD,45); //startGame header font
Color startHeaderCol=new Color(120,50,120); //startGame header colour
int sGB=20;
Toolkit tK; //contains various useful functions
Image startBackImg=Toolkit.getDefaultToolkit().getImage("bkG.jpg"); //background image storage
public static void main(String[] args)
{
gM=new Game(); //make a new application object and store it in gM
}
public Game()
{
super("insert name here"); //names the window
//LISTENERS------------------------------------------------------------------------------------------------------------->>>>>
addMouseListener(this); //mouse
addMouseMotionListener(this); //mouse movement
addKeyListener(this); //keyboard
addWindowListener(this); //window
//LISTENERS------------------------------------------------------------------------------------------------------------->>>>>
setSize(800,500); //application window's dimensions
setVisible(true); //make the window visible
setResizable(false); //make the window unresizeable
buffer=createImage(size().width,size().height); //buffer initialisation
backstage=buffer.getGraphics(); //initialises Graphics variable to draw to buffer
startGame(backstage); //begin the game
}
public void startGame(Graphics g)
{
g.setColor(startBackCol);
g.fillRect(0,0,size().width,size().height);
g.drawImage(startBackImg,0,0,this);
g.setColor(startForeCol);
g.fillRoundRect(X_ZERO+sGB,Y_ZERO+sGB,size().width-(sGB*2)-(X_ZERO*2),size().height-(sGB*2)-(X_ZERO+Y_ZERO),5,5);
g.setFont(startHeaderFont);
g.setColor(startHeaderCol);
repaint();
}
public void paint(Graphics g)
{
g.drawImage(buffer,0,0,null); //draw the buffer to the screen
}
public void update(Graphics g)
{
paint(g);
}
//LISTENER FUNCTIONS----------------------------------------------------------------------------------------------------->>>>>
public void mouseReleased(MouseEvent m)
{
}
public void mousePressed(MouseEvent m)
{
}
public void mouseEntered(MouseEvent m)
{
}
public void mouseExited(MouseEvent m)
{
}
public void mouseClicked(MouseEvent m)
{
}
public void mouseMoved(MouseEvent m)
{
}
public void mouseDragged(MouseEvent m)
{
}
public void keyTyped(KeyEvent k)
{
}
public void keyPressed(KeyEvent k)
{
}
public void keyReleased(KeyEvent k)
{
}
public void windowOpened(WindowEvent w)
{
}
public void windowClosing(WindowEvent w)
{
System.exit(0);
}
public void windowClosed(WindowEvent w)
{
}
public void windowIconified(WindowEvent w)
{
}
public void windowDeiconified(WindowEvent w)
{
}
public void windowActivated(WindowEvent w)
{
}
public void windowDeactivated(WindowEvent w)
{
}
//LISTENER FUNCTIONS----------------------------------------------------------------------------------------------------->>>>>
}
Java_Warrior
10-12-2004, 05:24 PM
Originally posted by agent_x91
hey I know that much don't treat me like a fool. I used buffer.drawImage(image,0,0,null); then drew the image to the window in the paint function. but originally it made the whole window grey - now it just leaves it as it is and paints nothing. a little help?
plus, the exceptions in the command prompt come up when it's run.
I'm not treating you like a fool. It is difficult for me to gauge your problems without seeing your code. Also, you don't need to paint the image to a buffer. Swing components are usually double buffered by default anyway.
agent_x91
10-12-2004, 05:28 PM
without the buffer, the quality is extremely poor. so I'm using a buffer.
Java_Warrior
10-12-2004, 05:33 PM
OK, a couple of suggestions:
1) consider creating a class that extends JPanel, then use public void paintComponent(Graphics g), and setting that as your content pane for your frame.
2) extend MouseAdapter rather than implementing MouseListener
I'll BRB, got some work to do :(
agent_x91
10-12-2004, 05:35 PM
JPanel isn't particularly helpful. nor are you really. anyone else offer some help?:rolleyes: :rolleyes: :rolleyes:;)
Java_Warrior
10-12-2004, 05:42 PM
Originally posted by agent_x91
JPanel isn't particularly helpful. nor are you really. anyone else offer some help?:rolleyes: :rolleyes: :rolleyes:;)
Well then, best of luck to you. I doubt you'll get much help, and even if you do, I doubt that it will matter SINCE YOU DON'T LISTEN.
HaganeNoKokoro
10-12-2004, 06:39 PM
Here's the (current) problem as I see it:
You're calling buffer=createImage() after you've already tried to paint it in paint() (happens when you setVisible(true)). But wait, there's more! If you tried to create it before calling setVisible(true), then it would return null because its parent component is not displayable. More NullPointerExceptions! The only way around it I can see is, at the beginning of paint(), test if buffer is null, and if it is, create it. Then paint() can do its thing (hopefully).
agent_x91
10-13-2004, 11:38 AM
You're calling buffer=createImage()
I assume that's what causes the exceptions, but it doesn't cause an actual problem. Everything is painted normally, but the picture is ignored.:confused:
I doubt you'll get much help, and even if you do, I doubt that it will matter SINCE YOU DON'T LISTEN.
I think you'll find that the people I don't listen to are patronising jerks. Not referring to anyone in particular.:rolleyes:
---------------------------------------------------------------------
Hagane, can the paint() function not be called after the JFrame is set to visible? I'm slightly confused...:confused:
HaganeNoKokoro
10-13-2004, 12:59 PM
I'm pretty sure the paint() method gets called as part of the call to setVisible(true). You're making the window visible, so it has to paint the contents.
The buffer=createImage is not causing problems, the fact that it's returning null is the problem. Since you call it before the component is displayable, (before you call setVisible(true)), you get null, and then when you call setVisible, you try to paint a null image and KABOOM.
From the Java API Documentation
createImage
public Image createImage(int width,
int height)
Creates an off-screen drawable image to be used for double buffering.
Parameters:
width - the specified width
height - the specified height
Returns:
an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.
Since:
JDK1.0
See Also:
isDisplayable(), GraphicsEnvironment.isHeadless()
The solution is like what Java_Warrior posted. Create another object to draw onto (like a JPanel). Make your own subclass and override the paint() method so it does what you want.
If you still want to create the offscreen buffer image, what you will do is set the window to visible before you add the JPanel to it. Then you can create the image, and since the window is displayed, it will not be null. Then you can add the JPanel to the frame using the add() method. At this point, it will probably call its own paint method, where it will presumably try to paint the buffer image. This should not cause problems since the image should not be null.
Of course, it probably won't go this smoothly. I don't really know what will happen. My AWT/Swing is kinda rusty.
agent_x91
10-13-2004, 05:12 PM
ah I think i understand now. The buffer is returning null because the size().width and size().height cannot be determined before the screen is visible, right?
but, I'm initialising the buffer after I make the screen visible, then drawing to it and painting it...so I'm still confused:(
I've only actually had problems drawing to my buffer and then to the screen with the image - everything else displays fine...
:confused: :confused: :confused:
ooh confusing:(