Original Post
Hei! Im a newbie with swing, heres my question.. my code, an copy paste from modified "google 'java almanac'" code is displaying an shape instead of my image.gif, why.. Yours : JariTapio HomePage : www.EuroJari.net EMail : JariTapio@EuroJari.net
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.applet.*;
import java.awt.event.*;
public class FirstFrameApp {
Image image;
public static void main(String[] args) {
new FirstFrameApp();
boolean gamemode = true;
while ( gamemode == true )
{
gamemode = true;
// Main game loop..
// repaint (); for rendering..
};
}
FirstFrameApp() {
image = Toolkit.getDefaultToolkit().getImage("image.gif");// this image.gif is on an './build/classes/' directory, why it is not displayed..
//try { Thread.sleep(5000); } catch ( InterruptedException e ) { };
// Creates a frame
JFrame frame = new JFrame("my very first frame application!! hmmm... application is not displaying image.gif as it should do..");
// Add a component with a custom paint method
frame.getContentPane().add(new MyComponent());
// Display the frame
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int frameWidth = dim.width;
int frameHeight = dim.height;
frame.setSize(frameWidth,frameHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class MyComponent extends JComponent {
// This method is called whenever the contents needs to be painted
public void paint(Graphics g) {
// Retrieve the graphics context; this object is used to paint shapes
Graphics2D g2d = (Graphics2D)g;
// Draw an oval that fills the window
int x = 0;
int y = 0;
int width = getSize().width-1;
int height = getSize().height-1;
g2d.drawOval(x, y, width, height);
g2d.drawImage(image,0,0,this); // this line is as good as an //NULL; but why..
}
}
}