class DrawablePicture extends JPanel implements MouseMotionListener {
private final static String STARTPIC_URL = "
http://localhost/bg.jpg"; private final static int W = 750;
private final static int H = 250;
private BufferedImage img;
private Color drawcol;
private int lastx;
private int lasty;
public DrawablePicture() {
try {
img = ImageIO.read(new URL(STARTPIC_URL));
setPreferredSize(new Dimension(W, H));
this.addMouseMotionListener(this);
drawcol = Color.BLACK;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
public BufferedImage getImg() {
return img;
}
public void mouseDragged(MouseEvent ev) {
Graphics g = img.getGraphics();
g.setColor(drawcol);
g.drawLine(lastx, lasty, ev.getX(), ev.getY());
repaint();
lastx = ev.getX();
lasty = ev.getY();
}
public void mouseMoved(MouseEvent ev) {
lastx = ev.getX();
lasty = ev.getY();
}
public void setColor(Color drawcol) {
this.drawcol = drawcol;
}
}