Java 不使用 ImageIcon 的幻灯片放映
Java slideshow without using ImageIcon
这是我的第一个问题所以请不要这么残忍。
我的程序有问题,这意味着我不知道为什么会出现 NullpointerException,有人知道为什么吗?
我的代码:`package ex2;
public class Screen extends JPanel {
private BufferedImage image ;
private Timer tm ;
private Graphics g ;
private static int index = 0 ;
private ArrayList<String> paths = new Test().getArrayList();
public Screen(){
System.out.println(paths);
String interval ;
interval = JOptionPane.showInputDialog("Please write time interval "
+ "between images in miliseconds");
tm = new Timer(Integer.parseInt(interval), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
paint(index);
index += 1 ;
if (index >= paths.size())
System.exit(index);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
tm.start();
}
public void paint(int i) throws IOException{
image = ImageIO.read(new File(paths.get(i)));
g.drawImage(image, 40, 30, image.getWidth() , image.getHeight() , null);
}
}
private Graphics g ;
在我看来是空的。
如果您想进行自定义绘画,则需要重写 JPanel
的 paintComponent()
方法并使用传递给该方法的 Graphics 对象。
阅读 Swing 教程中关于 Custom Painting 的部分,以获取展示绘画正确方法的工作示例。
Java slideshow without using ImageIcon
你为什么要在没有 ImageIcon
的情况下尝试这样做?为什么要重新发明轮子?
只需阅读图像并创建一个 ImageIcon
并将图标添加到 JLabel
并将标签添加到框架。然后使用 setIcon(...)
方法更改图像。本教程还有一个关于 How to Use Icons
的部分,可帮助您入门。
这是我的第一个问题所以请不要这么残忍。
我的程序有问题,这意味着我不知道为什么会出现 NullpointerException,有人知道为什么吗?
我的代码:`package ex2;
public class Screen extends JPanel {
private BufferedImage image ;
private Timer tm ;
private Graphics g ;
private static int index = 0 ;
private ArrayList<String> paths = new Test().getArrayList();
public Screen(){
System.out.println(paths);
String interval ;
interval = JOptionPane.showInputDialog("Please write time interval "
+ "between images in miliseconds");
tm = new Timer(Integer.parseInt(interval), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
paint(index);
index += 1 ;
if (index >= paths.size())
System.exit(index);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
tm.start();
}
public void paint(int i) throws IOException{
image = ImageIO.read(new File(paths.get(i)));
g.drawImage(image, 40, 30, image.getWidth() , image.getHeight() , null);
}
}
private Graphics g ;
在我看来是空的。
如果您想进行自定义绘画,则需要重写 JPanel
的 paintComponent()
方法并使用传递给该方法的 Graphics 对象。
阅读 Swing 教程中关于 Custom Painting 的部分,以获取展示绘画正确方法的工作示例。
Java slideshow without using ImageIcon
你为什么要在没有 ImageIcon
的情况下尝试这样做?为什么要重新发明轮子?
只需阅读图像并创建一个 ImageIcon
并将图标添加到 JLabel
并将标签添加到框架。然后使用 setIcon(...)
方法更改图像。本教程还有一个关于 How to Use Icons
的部分,可帮助您入门。