【发布时间】:2013-04-24 14:25:20
【问题描述】:
我知道到处都有关于如何绘制图像的辅导。通常人们建议显示它添加一个加载该图像的对象。但就我而言,我不想每次更改图像中的某些内容时都实例化一个新对象。
所以,我正在使用 Graphics 类来做到这一点。 另外,我正在使用 MVC 方法。
问题: 正如我们所看到的,只有一小部分图像被绘制,如果我加载另一个图像,这个小区域会根据图片而变化。然后,我认为缓冲图像已正确加载。
所以,我在寻找问题所在: 这是我的代码:
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class DisplayView extends JFrame implements Observer {
private static final long serialVersionUID = 1L;
/**
* @param args
*/
private static DisplayView instance;
private DisplayControl control;
private JFileChooser fileChooser;
Panel imageLeft, imageRight;
private DisplayView() {
JMenuItem exit = new JMenuItem("Exit");
exit.setMnemonic('E');
exit.setToolTipText("Exit Application");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new ImageFileFilter());
JMenuItem loadImage = new JMenuItem("Load Image");
loadImage.setMnemonic('O');
loadImage.setToolTipText("Loads an Image to Process");
loadImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int ret = fileChooser.showDialog(DisplayView.getInstance(),
"Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
System.out.println(fileChooser.getSelectedFile());
control.onFileChoose(fileChooser.getSelectedFile()
.getAbsolutePath());
}
}
});
JMenu file = new JMenu("File");
file.setMnemonic('F');
file.add(loadImage);
file.add(exit);
JMenuBar menuBar = new JMenuBar();
menuBar.add(file);
imageLeft = new Panel();
imageLeft.setSize(500, 500);
imageLeft.setVisible(true);
imageRight = new Panel();
this.setLayout(new FlowLayout());
this.add(imageLeft);
// this.add(imageRight);
this.setTitle("Test");
this.setSize(300, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setJMenuBar(menuBar);
}
static public DisplayView getInstance() {
if (instance == null)
instance = new DisplayView();
return DisplayView.instance;
}
public void setControl(DisplayControl control) {
this.control = control;
}
@Override
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
if (o instanceof DisplayModel) {
this.imageLeft.setImage(((DisplayModel) o).getOriginalImage());
// this.imageRight.setImage(((DisplayModel) o).getProcessedImage());
}
}
}
class Panel extends JPanel {
BufferedImage image;
public void setImage(BufferedImage image) {
if (image != null)
this.image = image;
this.repaint();
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
if (image != null)
g.drawImage(image, 0, 0, this);
}
}
【问题讨论】:
-
你的形象有什么变化?你不能修改然后重绘它吗?
-
这只是一个简单应用程序的视图。因此,每当模型中的图像发生变化时,视图都会收到通知并再次设置图像,然后重新绘制自己。对吗?
-
您可以这样做,或者您可以保留它并重复使用它。无论哪种方式都可以。
标签: java swing drawing jframe bufferedimage