hi,
I have the following code and it compiles on Netbeans with no errors or warnings but when I load the applet in a browser the browser does not display all of the pictures that I got it to draw. The only picture it draws is my_gif. I have tried using the repaint() function and I have googled around but still no solution to this problem. Can somebody spot the bug which my compiler is unable to see? It would be much appreciated as I don't have a clue where the problem might be.
Thankyou
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
public class viewer extends Applet implements MouseListener, MouseMotionListener
{
// Your image name;
Image my_gif;
boolean is_init = true;
Image img_arrowl;
Image img_arrowr;
Image img_btn;
Image img_btnzoomin = null;
boolean show_zoomin = false;
// The applet base URL
URL base;
int wpos = 300;
int hpos = 450;
int xpos = 150;
int ypos = 0;
int zoom = 0;
int xzoom[] = new int[10];
int yzoom[] = new int[10];
int wzoom[] = new int[10];
int hzoom[] = new int[10];
// This object will allow you to control loading
MediaTracker mt;
public static BufferedImage toBufferedImage(Image image,boolean hasAlpha) {
if (image instanceof BufferedImage) {return (BufferedImage)image;}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha == true) {transparency = Transparency.BITMASK;}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
}
catch (HeadlessException e) {} //No screen
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public void init()
{
img_arrowl = getImage(base,"l.png");
img_arrowr = getImage(base,"r.png");
img_btn = getImage(base,"btn.png");
//img_btnzoomin=createImage(new FilteredImageSource(img_btn.getSource(),new CropImageFilter(7,51,22,22)));
xzoom[0]=xpos;
yzoom[0]=ypos;
wzoom[0]=wpos;
hzoom[0]=hpos;
addMouseListener(this);
// initialize the MediaTracker
mt = new MediaTracker(this);
// The try-catch is necassary when the URL isn't valid
// Ofcourse this one is valid, since it is generated by
// Java itself.
try {
// getDocumentbase gets the applet path.
base = getDocumentBase();
}
catch (Exception e) {}
try {
// Create a URL for the image's location
URL url = new URL("http://localhost/tst.jpg");
// Get the image
my_gif = Toolkit.getDefaultToolkit().createImage(url);
} catch (MalformedURLException e) {
} catch (IOException e) {
}
// Here we load the image.
// Only Gif and JPG are allowed. Transparant gif also.
//my_gif = getImage(base,"imageExample.gif");
// tell the MediaTracker to kep an eye on this image, and give it ID 1;
mt.addImage(my_gif,1);
// now tell the mediaTracker to stop the applet execution
// (in this example don't paint) until the images are fully loaded.
// must be in a try catch block.
try {
mt.waitForAll();
}
catch (InterruptedException e) {}
// when the applet gets here then the images is loaded.
}
public Graphics2D transform(Image img, float alpha, Graphics2D g2d,int xx, int yy) {
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(img, 0, 0, null);
RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
//BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
//Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(bi, (BufferedImageOp) rop,xx,yy);
return g2d;
}
public Image urlImage(String link) {
/* Resources
* http://www.exampledepot.com/egs/java.net/GetImage.html
* http://www.go4expert.com/forums/showthread.php?t=6529
*/
Image image = null;
//link = "http://hostname:80/image.gif"
try {
URL url = new URL(link);
image = Toolkit.getDefaultToolkit().createImage(url);
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return image;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g)
{
// now we are going to draw the gif on the screen
float alpha;
alpha = 0.66f;
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0,0,600,450);
g2d.fillRect(600,0,85,82);
g.drawImage(my_gif,xpos,ypos,wpos,hpos,this);
g2d.fillRect(600,82,87,370);
g2d.setColor(Color.BLACK);
g2d.drawLine(600,82,600,450);
g2d.drawLine(601,82,601,450);
g2d.drawLine(600,82,685,82);
g2d.drawLine(600,83,685,83);
g2d.drawLine(685,0,685,82);
g2d.drawLine(686,0,686,82);
g2d.drawLine(0,450,600,450);
g2d.drawLine(0,451,600,451);
transform(img_arrowl,alpha, g2d,269,419);
transform(img_arrowr,alpha,g2d,300,419);
transform(img_btn,alpha,g2d,600,0);
if (show_zoomin==true) {
img_btnzoomin=img_btn;
BufferedImage dest = toBufferedImage(img_btn,false);
dest = dest.getSubimage(7, 51, 22, 22);
img_btnzoomin = Toolkit.getDefaultToolkit().createImage(dest.getSource());
g.drawImage(img_btnzoomin,607,51,22,22,this);
repaint(0,0,687,542);
}
if (is_init==true) {
is_init=false;
repaint(0,0,687,542);
} else {
is_init=true;
}
//g = (Graphics) g2d;
//int white = img_arrowl .getRGB(0, 0);
//int white = 16777215;
//Color white = new Color(255,255,255);
//img_arrowl = makeColorTransparent(img_arrowl,white);
//img_arrowr = makeColorTransparent(img_arrowr,white);
//g.drawImage(img_arrowl,269,419,31,31,this);
//g.drawImage(img_arrowr,300,419,31,31,this);
// you can resize the image easily
//g.drawImage(my_gif,20,140,30,40,this);
}
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
zoom++;
wpos *=2;
hpos *=2;
wzoom[zoom]=wpos;
hzoom[zoom]=hpos;
xpos-=(int) Math.floor(wpos/4);
ypos-=(int) Math.floor(hpos/4);
xzoom[zoom]=xpos;
yzoom[zoom]=ypos;
repaint(0,0,687,542);
}
if (e.getY()<72 && e.getY()>50 && e.getX()>657 && e.getX()<677 && zoom>0) {
//else { //if (this.zoom>0) {
zoom--;
wpos =wzoom[zoom];
hpos =hzoom[zoom];
xpos =xzoom[zoom];
ypos =yzoom[zoom];
repaint(0,0,687,542);
}
}
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
repaint(0,0,687,542);
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
repaint(0,0,687,542);
}
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseMoved(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
if (e.getY()<72 && e.getY()>50 && e.getX()>607 && e.getX()<628 && zoom<10) {
show_zoomin=true;
} else {
show_zoomin=false;
}
}
}