나는 어디에서나 이미지를 그리는 방법에 사교육이 있다는 것을 알고있다. 일반적으로 사람들은 이미지를로드하는 객체를 추가하여 이미지를 표시 할 것을 제안합니다. 그러나 제 경우에는 이미지에서 어떤 것을 바꿀 때마다 새로운 객체를 인스턴스화하고 싶지 않습니다.자바 그리기 이미지 JPanel : 작동하지만 이상한
그래서 그래픽 클래스를 사용하고 있습니다. 또한 MVC 방식을 사용하고 있습니다.
문제점 : 우리가 보는 바와 같이, I는 다른 이미지를로드하면 그려지는 이미지의 작은 영역에 존재 이 작은 영역 화상에 따라 변경한다. 그런 다음 버퍼링 된 이미지가 올바르게로드되었다고 가정합니다.
그래서, 문제가 될 것입니다 곳을 찾고 : 이 나의 코드 :
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);
}
}
사용자 정의 회화
paintComponent
보다는를 오버라이드 (override)은 "적절한LayoutManager
사용"대답은 항상 동일합니다? 그냥 다시 그걸 수정하지 못했습니다? –이것은 단순한 응용 프로그램의보기입니다. 따라서 모델의 이미지가 변경 될 때마다 뷰는 알림을 받고 이미지를 다시 설정하고 다시 그리기 작업을 수행합니다. 권리? –
그런 식으로 할 수도 있고, 주변에두고 재사용 할 수도 있습니다. 어느 쪽이든 작동합니다. –