I was trying to load a few images using java but it seems to be extremely slow, it's around 13 images I'm trying to get each of 9KB size.
Is it my code or is it java that's causing the problem. I can load all the images alot faster using the browser.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageSample {
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Display image");
//Image url here
String url="";
JPanel panel = new testImage();
frame.add(panel);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
}
class testImage extends JPanel {
static Image image;
public void testImage(String url)
{
image = Resources.getImage(url);
}
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, 40, 40, null);
}
}
class Resources
{
private static Resources myResource = new Resources();
// NOTE: there is no error checking here so if parameter is mistyped
// somewhere else in code, then this will probably throw a nullpointerexception
public static Image getImage(String name)
{
// TODO: Find out which way is better or preferred
URL url=null;
try {
url = new URL(name);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Toolkit.getDefaul开发者_JS百科tToolkit().getImage(url);
}
}
Thanks, Sreejith
Your program isn't actually doing what you think it's doing because you have made some fundamental mistakes with your class and method names:
class testImage extends JPanel {
static Image image;
public void testImage(String url)
{
image = Resources.getImage(url);
}
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, 40, 40, null);
}
}
The standard is that class names should always begin with an upper-case letter, and method names should begin with a lower-case letter to avoid confusion. Because you have confused the two, you didn't notice that the testImage(url)
you declare in this class is a void method, not a constructor, even though the name is the same. Therefore, when you call JPanel panel = new testImage();
you are not calling that method - you're just calling the default empty constructor that every class is given if no constructors are declared in the code. Note also that you haven't used the variable url
and that your field image
should not be static.
To be honest, you're going about the whole thing the wrong way and should start again from scratch. Costis' solution looks good. You should definitely give ImageIcon
s a try because they remove the hard work of having to manually get the resource URL and render it.
Try this example with your image. It is not slow.
public class ImageLoad extends JFrame {
public ImageLoad() {
setSize(800, 800);
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("singer.jpg");
JLabel label = new JLabel();
label.setIcon(icon);
panel.add(label);
add(panel);
}
public static void main(String[] args) {
new ImageLoad().setVisible(true);
}
}
精彩评论