getting image from file in standalone application?
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.
>I suck at this game, can you give me some pointers?
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?
>I suck at this game, can you give me some pointers?
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.
>I suck at this game, can you give me some pointers?
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.
Code:
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----------------------------------------------------------------------------------------------------->>>>>
}
Last edited by agent_x91; 10-12-2004 at 04:24 PM.
>I suck at this game, can you give me some pointers?
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.
Bookmarks