Hello all, I'm working with java images for the first time and having a problem viewing them when the applet loads. The code I've posted below is a dramatically pared-down version of the code I'm actually working with, hopefully figuring out why I can't see an image with this code will show me while I have to resize the window to see images with this code. All help is greatly appreciated and thanks are extended in advance :)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;
public class example extends JApplet implements Runnable
{
boolean updating;
Thread thread;
private int width, height;
TestImageDraw aTable; //used to create and store values
private AudioClip[] sounds = new AudioClip[4]; //array to hold audio clips
private int counter = 0; //counter for audio clip array
private Image GameImage;
private Graphics GameGraphics;
public example() //set up applet gui
{
this.resize(new Dimension(600, 500));
//setup table
//aTable = new Table(50, 50, 50, 50, 16, 16, getImage("images/FLY.gif", Color.white),
//getImage("images/FlySwatter.gif", Color.white)); //Table must be square or flyswatter wont move straight
aTable = new TestImageDraw(getImage("images/FLY.gif", Color.white));
//this.add(aTable);
super.resize(800, 600);
repaint();
}
public void init()
{
width = getSize().width;
height = getSize().height;
GameImage = createImage(width, height);
GameGraphics = GameImage.getGraphics();
// Automatic in some systems, not in others
GameGraphics.setColor(Color.black);
repaint();
validate();
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void stop()
{
updating = false;
}
public void run()
{
while(updating)
{
//aTable.update();
}
aTable.revalidate();
}
//returns a transparent image.
//color is made transparent
private Image getImage(String imgPath, final Color color)
{
Image img = Toolkit.getDefaultToolkit().getImage(imgPath);
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFFFFFF;
public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
img = Toolkit.getDefaultToolkit().createImage(ip);
return img;
}
}
TestImageDraw.java
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class TestImageDraw extends JPanel
{
Image itemImg; // stores the item image
public TestImageDraw(Image itemImg)
{
this.itemImg = itemImg;
}
/** Description of draw(Graphics g)
*
* Function draws the lines used in the table
* @param g object used to draw the table
* @return none
*/
public void draw(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
//draw flyswatter
drawValues(g2); //draw values
}
private void drawValues(Graphics g)
{
g.drawImage(itemImg,20,140,30,40, null);
g.setColor(Color.black); // set color of table to black
}
}