import java.awt.*;
import javax.swing.*;
class ImagePanel extends JPanel
{
//the height and width of the specified image
int height,width;
public ImagePanel()
{
try{
image = Toolkit.getDefaultToolkit().getImage("1.jpg"); MediaTracker mt=new MediaTracker(this);
mt.addImage(image,0);
height=image.getHeight(image);
width=image.getWidth(image);
try{
mt.waitForID(0);
}catch(Exception e){
System.out.print("error");
}
}catch(Exception e){
} }
height=image.getHeight(image);
width=image.getWidth(image);这两句不对啊?该怎么写?
要ImageObserver类型的参数。
import javax.swing.*;
class ImagePanel extends JPanel
{
//the height and width of the specified image
int height,width;
public ImagePanel()
{
try{
image = Toolkit.getDefaultToolkit().getImage("1.jpg"); MediaTracker mt=new MediaTracker(this);
mt.addImage(image,0);
height=image.getHeight(image);
width=image.getWidth(image);
try{
mt.waitForID(0);
}catch(Exception e){
System.out.print("error");
}
}catch(Exception e){
} }
height=image.getHeight(image);
width=image.getWidth(image);这两句不对啊?该怎么写?
要ImageObserver类型的参数。
getDefaultToolkit().
getImage("1.jpg");
...
height = imageIcon..getIconHeight();
width = imageIcon.getIconWidth();
原来我做一个像启动画面一样的,用.getWidth()试了好久,都没成功。。
import javax.swing.*;
class ImagePanel extends JPanel
{
//the height and width of the specified image
int height,width;
public ImagePanel()
{
try{
//image = Toolkit.getDefaultToolkit().getImage("1.jpg");
ImageIcon imageIcon = new ImageIcon(Toolkit.
getDefaultToolkit().
getImage("1.jpg"));
MediaTracker mt=new MediaTracker(this);
mt.addImage(image,0);
height=imageicon.getIconHeight();
width=imageicon.getIconWidth();
try{
mt.waitForID(0);
}catch(Exception e){
System.out.print("error");
}
}catch(Exception e){
} }
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,20,20,450,600,this);
g.drawString("the height of image is :"+height,800,300);
g.drawString("the width of image is :"+width,800,400);
}
private Image image;
}
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("Image for test");
setSize(Toolkit.getDefaultToolkit().getScreenSize().width,
Toolkit.getDefaultToolkit().getScreenSize().height);
getContentPane().add(new ImagePanel());
}
}class ImageTest
{
public static void main(String args[])
{
JFrame frame=new ImageFrame();
frame.show();
}
}还是不对啊
mt.addImage(image,0);
将这个换成this.add(image);试试