drawImage() 不绘制
import java.awt.*;
import javax.swing.*;
public class Main
{
JFrame jf;
Main()
{
jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new MyCanvas());
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
}
}
class MyCanvas extends JComponent
{
Image img;
MyCanvas()
{
setPreferredSize(new Dimension(200,200));
img=Toolkit.getDefaultToolkit().createImage("1.jpg");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,null);
}
}
我想使用自己的 paintComponent
方法获取画布,但有时我会看到没有图像的空窗口。我需要调整窗口大小才能看到该图像。什么是问题?为什么drawImage有时不绘制?
import java.awt.*;
import javax.swing.*;
public class Main
{
JFrame jf;
Main()
{
jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new MyCanvas());
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
}
}
class MyCanvas extends JComponent
{
Image img;
MyCanvas()
{
setPreferredSize(new Dimension(200,200));
img=Toolkit.getDefaultToolkit().createImage("1.jpg");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,null);
}
}
I'd like to get canvas with its own paintComponent
method, but sometimes I see empty window without image. And I need to resize window for doing visible this image. What is problem? Why drawImage doesn't draw sometimes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为
,您应该可以开始了。
Change
to
and you should be good to go.