Tegneflate.java [Last ned]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Tegneflate extends JPanel
                        implements MouseListener  {
  int x;
  int y;
  int s = 20;
 
  public Tegneflate() {
    this.addMouseListener(this);
  }
 
  public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();
    repaint();
  }
 
  public void mousePressed(MouseEvent e)  { }
  public void mouseReleased(MouseEvent e) { }
  public void mouseEntered(MouseEvent e)  { }
  public void mouseExited(MouseEvent e)   { }
 
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.RED);
    if (x>0)
      g.fillRect(x, y, s, s);
  }
 
}
Kildekode blir vist ved hjelp av GeSHi.