First of all, you don't want to paint directly on the JApplet but rather paint in a JPanel that the JApplet holds. This way you can override paintComponent and not paint and get all of the benefits of Swing graphics over AWT graphics, including double buffering.
Next of all, if all you want to do is to display the position of the mouse, then for my money, I'd use a JLabel. Something like so:
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MousePositionApp extends JApplet {
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
private void createGUI() {
getContentPane().add(new GraphicsPanel());
}
}
class GraphicsPanel extends JPanel {
private static final String MOUSE_POS = "Mouse Position: ";
JLabel mousePositionLabel = new JLabel(MOUSE_POS);
public GraphicsPanel() {
setLayout(new BorderLayout());
add(mousePositionLabel, BorderLayout.SOUTH);
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
String labelTxt = MOUSE_POS + String.format("[%d, %d]", e.getPoint().x, e.getPoint().y);
mousePositionLabel.setText(labelTxt);
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.green);
g.drawOval(20, 20, 50, 50);
g.drawLine(45, 70, 45, 100);
g.drawLine(45, 70, 80, 100);
g.drawLine(45, 70, 10, 100);
}
}
Much luck!