在drawingPanel中刷新图片扩展了JPanel [英] Refreshing picture in drawingPanel extends JPanel
问题描述
我必须在软件的底部加载一个小图标。只是有一个加载/确定/错误图标。如 http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html上所述,我创建了一个扩展的面板,扩展了JPanel。
I have to load a small icon on the botton of my software. just to have a loading/ok/error icon. as sudgested on "http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html" i create a dwowing panel extending JPanel.
class drawingPanel extends JPanel
{
Image img;
drawingPanel (Image img){
this.img = img;
}
public void paintComponent (Graphics g) {
super.paintComponent (g);
// Use the image width & height to find the starting point
int imgX = getSize ().width/2 - img.getWidth (this);
int imgY = getSize ().height/2 - img.getHeight (this);
//Draw image centered in the middle of the panel
g.drawImage (img, imgX, imgY, this);
} // paintComponent
}
我初始化组件
// Grab the image.
Image img = new ImageIcon(iconPath+"ok.png").getImage();
// Create an instance of DrawingPanel
iconPanel = new drawingPanel(img);
一切正常,但在运行时我希望能够更改面板中的图标。我尝试了所有操作;但是,没有一个尝试查看新图片:
all works well but at runtime i want to be able to change the icon within the pannel. i tryed all the fo;llowing but none managed to view the new picture:
Image img = new ImageIcon(iconPath+"loading.gif").getImage();
// Create a new instance of DrawingPanel
this.iconPanel = new drawingPanel(img);
this.iconPanel.repaint();
this.iconPanel.revalidate();
this.iconPanel.repaint();
this.repaint();
this.revalidate();
(我之所以这样,是因为我正在编写代码的类是JPanel的另一个扩展,其中包含IconPanel。关于我为什么不设法更改图片的任何想法吗?
(i tryed this because the class in whic i am writing the code is another extension of JPanel that contains IconPanel. Any idea about why i do not manage to change the picture?
谢谢,
Stefano
Thanks, Stefano
推荐答案
第一件事不要以小名字开头类名,将 drawingPanel
重命名为 DrawingPanel
。
First thing don't start class name with small name. Rename drawingPanel
to DrawingPanel
.
我已经尝试根据您的描述制作一个简单的演示,并且效果很好。面板中的图像变化完美。
I have tried making a simple demo based on your description and it works fine. The image in panel is changing perfectly.
public class Demo {
public Demo() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// Grab the image.
Image img = new ImageIcon("1.png").getImage();
// Create an instance of DrawingPanel
final DrawingPanel iconPanel = new DrawingPanel(img);
frame.add(iconPanel, BorderLayout.CENTER);
JButton button = new JButton("Change image..");
frame.add(button, BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
iconPanel.setImg(new ImageIcon("2.png").getImage());
iconPanel.repaint();
}
});
frame.setVisible(true);
}
public static void main(String[] args){
new Demo();
}
}
class DrawingPanel extends JPanel {
Image img;
DrawingPanel(Image img) {
this.img = img;
}
public void setImg(Image img) {
this.img = img;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Use the image width & height to find the starting point
int imgX = getSize().width / 2 - img.getWidth(this);
int imgY = getSize().height / 2 - img.getHeight(this);
// Draw image centered in the middle of the panel
g.drawImage(img, imgX, imgY, this);
} // paintComponent
}
我所做的更改在 DrawingPanel
类中添加了 img
的setter方法。因此,无需创建新的 DrawingPanel
,您只需使用新图像调用setImg(),然后调用reapint绘制新图像即可。
The changes I have made is add a setter method of img
in DrawingPanel
class. So instead of creating new DrawingPanel
you just have to call setImg() with new Image and then call reapint to paint the new image.
这篇关于在drawingPanel中刷新图片扩展了JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!