public class ImageFrame {
private static final int FRAME_DEFAULT_WIDTH = 800;
private static final int FRAME_DEFAULT_HEIGHT = 600;
private int bufferedimage_width;
private int bufferedimage_height;
private JPanel drawPanel;
private BufferedImage img;
private Graphics img_graphics;
private Object mouseClicked = new Object();
public ImageFrame(String title) {
this(title, FRAME_DEFAULT_WIDTH, FRAME_DEFAULT_HEIGHT);
}
public ImageFrame(String title, int frame_width, int frame_height) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frame_width, frame_height);
drawPanel = new JPanel(false) {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
super.paint(g);
if (img != null) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
}
};
frame.add(drawPanel);
drawPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
synchronized (mouseClicked) {
mouseClicked.notify();
}
}
});
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
bufferedimage_width = drawPanel.getWidth();
bufferedimage_height = drawPanel.getHeight();
img = new BufferedImage(bufferedimage_width, bufferedimage_height,
BufferedImage.TYPE_INT_RGB);
img_graphics = img.getGraphics();
img_graphics.setColor(Color.WHITE);
img_graphics.fillRect(0, 0, bufferedimage_width, bufferedimage_height);
frame.repaint();
}
public int getWidth() {
return bufferedimage_width;
}
public int getHeight() {
return bufferedimage_height;
}
public void setPixel(int x, int y, Color c) {
img_graphics.setColor(c);
img_graphics.drawLine(x, y, x, y);
drawPanel.repaint();
}
public void waitMouseClicked() {
try {
synchronized (mouseClicked) {
mouseClicked.wait();
}
} catch (InterruptedException e) {
}
}
}