用paintComponent()方法显示Jpg图像,程序可以不仔细看
想问Image image=new ImageIcon("picture.jpg").getImage();中的文件名是随便写吗?还是有什么具体要求,我用了picture。jpg是我电脑里的照片,但运行后什么也显示不了import java.awt.*;
import javax.swing.*;
public class Picture extends JPanel
{
public void go()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭时结束程序
Picture paint=new Picture();
frame.getContentPane().add(BorderLayout.CENTER, paint);//将要上色区域放在中央
frame.setSize(1000,1000);
frame.setVisible(true);
}
public void paintComponent(Graphics g)//paintComponent如何被调用的?
{
Graphics2D g2D=(Graphics2D) g;
Image image=new ImageIcon("picture.jpg").getImage();
g.drawImage(image,3,4,this);//离左端三个像素,顶端四个像素
}//这一方法是对图像的设置
public static void main(String[] args)
{
Picture paint=new Picture();
paint.go();
}
}