📄 loadimagedemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoadImageDemo extends JFrame implements ActionListener
{
private ImagePanel imagePanel; //绘制图像的面板
private JButton button;
public LoadImageDemo()
{
super("getImage");
setSize(400, 400);
//获取内容面板
Container container = getContentPane();
//创建用于绘制图像的面板
imagePanel = new ImagePanel();
container.add(imagePanel, BorderLayout.CENTER);
//创建按钮
button = new JButton("下帧图像");
//设置字体
button.setFont(new Font("Serif", Font.PLAIN, 14));
//注册监听器
button.addActionListener(this);
container.add(button, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
imagePanel.showNext();
}
public static void main(String[] args)
{
LoadImageDemo application = new LoadImageDemo();
}
class ImagePanel extends JPanel
{
private ImageIcon images;
public ImagePanel()
{
super();
setBackground(Color.WHITE);
//载入图像
// images= new ImageIcon("boy01.png");
images= new ImageIcon("Image.jpg");
}
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//绘制图像,坐标为屏幕中心
int x = (this.getWidth() - images.getIconWidth())/2;
int y = (this.getHeight() - images.getIconHeight())/2;
images.paintIcon(this, g, x, y);
}
//该方法显示下一帧图像
public void showNext()
{
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -