0

I have to draw a simple crosshair. All I am seeing is a blank panel.

class ChartPanel extends JPanel implements MouseMotionListener{
    Graphics2D g;
    Dimension dimFrame;
    ChartPanel() {
        addMouseMotionListener(this);
    }
    public void mouseMoved(MouseEvent e) {
        drawCrosshair(e.getX(),e.getY());
    }
    public void mouseDragged(MouseEvent e) {}
    protected void paintComponent(Graphics g2) {
        g = (Graphics2D)g2;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        dimFrame = getSize();
        setBackground(Color.WHITE);
    }
    public Dimension getPreferredSize() {
        return new Dimension(700, 500);
    }
    void drawCrosshair(double x, double y) {
        double maxx = dimFrame.getWidth();
        double maxy = dimFrame.getHeight();
        g.setPaint(Color.BLACK);
        g.draw(new Line2D.Double(0, y, maxx, y));
        g.draw(new Line2D.Double(x, 0, x, maxy));
    }
}
public class pra {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        ChartPanel chartPanel = new ChartPanel();
        jFrame.add(chartPanel);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

And it is getting into drawCrosshair() method with correct value. I have no clue what I am doing wrong.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Dheeraj
  • 13
  • 4
  • 1
    Small tip: instead of overriding `getPreferredSize`, it's better to call `setPreferredSize` with the dimensions you want. – byxor May 07 '17 at 08:26
  • did you get it working? – ItamarG3 May 07 '17 at 11:04
  • @byxor: [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 07 '17 at 12:34
  • Don't save the graphics context, `g`, as an instance variable. If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod May 07 '17 at 12:37

1 Answers1

1

You can just dispose of drawCrosshair(), and draw the crosshair in the paint method, which would replace the paintComponent method (Actually I think that you shouldn't ever override paintComponent):

    Graphics2D g;
    Dimension dimFrame;
    int x, y;
    ChartPanel() {
        addMouseMotionListener(this);
        setPreferredSize(new Dimension(700, 500));
    }

    public void mouseMoved(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void paint(Graphics g2) {
        super.paint(g2);
        g = (Graphics2D) g2;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        dimFrame = getSize();
        g.clearRect(0, 0, dimFrame.width, dimFrame.height);//clears previous drawings
        g.setColor(Color.BLACK);
        g.drawLine(x - 10, y, x + 10, y);
        g.drawLine(x, y - 10, x, y + 10);
    }

And this should do it (actually it does, as I've tested it ;) )

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 07 '17 at 12:35
  • Worked for me. But I don't understand what was wrong with my code? – Dheeraj May 07 '17 at 18:42