Question

I did not know how to phrase the title in few words, so I am going to explain my situation: I have already set-up a class which extends JPanel, includes all the code for mouse listener, etc... What my problem is that once the user drags the mouse, it does not draw the required line, even though the co-ords work as intended (will show console out-put which I have temporarily set-up just to verify if the co-ords do work).

Here is the code for the class:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Container extends JPanel implements MouseMotionListener{
Graphics2D board;
public void paintComponent(Graphics comp)
{
    board = (Graphics2D) comp;
    board.setColor(new Color(255,255,255));
    Rectangle2D.Double bg = new Rectangle2D.Double(0.0, 0.0, (double) getSize().width, (double) getSize().height);
    board.fill(bg);

    board.setColor(new Color(0,0,0));

    //Line2D.Double test = new Line2D.Double(100,100,150,150);
    //board.draw(test); works
}

public Container(String name, String text, String text2) 
{
    addMouseMotionListener(this);
}

public Container(String filepath)
{
    addMouseMotionListener(this);
}

int oldy;
int oldx;
int newy;
int newx;
boolean old;
@Override
public void mouseDragged(MouseEvent e) {
    newy = e.getY();
    newx = e.getX();

    if(old)
    {
        oldy = newy;
        oldx = newx;
    }
    else
    {
        old = true;
    }

    board.setColor(new Color(0,0,0));
    Line2D.Double line = new Line2D.Double(10,10,20,20);
    board.draw(line);
    board.draw(new Line2D.Double((int) oldx, (int) oldy, (int) newx,(int) newy));

    System.out.print(oldx + ", " + oldy + ", " + newx + ", " + newy + ".\n");
}

@Override
public void mouseMoved(MouseEvent arg0) {

}

}

and here is the console output ("oldx, oldy, newx, newy."):

418, 426, 418, 426.
409, 422, 409, 422.
403, 419, 403, 419.
400, 416, 400, 416.
396, 413, 396, 413.
393, 408, 393, 408.
390, 399, 390, 399.
390, 390, 390, 390.
390, 379, 390, 379.
392, 365, 392, 365.
397, 351, 397, 351.
403, 336, 403, 336.
410, 318, 410, 318.
418, 302, 418, 302.
428, 286, 428, 286.
436, 272, 436, 272.
446, 260, 446, 260.
455, 251, 455, 251.
464, 243, 464, 243.
475, 238, 475, 238.
490, 233, 490, 233.
510, 232, 510, 232.
532, 232, 532, 232.
557, 235, 557, 235.
581, 241, 581, 241.
607, 251, 607, 251.
631, 262, 631, 262.
652, 273, 652, 273.
671, 283, 671, 283.
687, 291, 687, 291.
698, 299, 698, 299.
710, 306, 710, 306.
720, 311, 720, 311.
728, 316, 728, 316.
735, 320, 735, 320.
737, 321, 737, 321.
739, 322, 739, 322.
741, 322, 741, 322.
745, 320, 745, 320.
751, 316, 751, 316.
761, 311, 761, 311.
772, 305, 772, 305.
784, 298, 784, 298.
799, 289, 799, 289.
814, 283, 814, 283.
829, 277, 829, 277.
840, 272, 840, 272.
852, 268, 852, 268.
862, 264, 862, 264.
867, 262, 867, 262.
872, 261, 872, 261.
874, 259, 874, 259.
877, 257, 877, 257.
879, 255, 879, 255.
880, 254, 880, 254.
881, 253, 881, 253.
882, 253, 882, 253.
883, 252, 883, 252.
884, 251, 884, 251.
885, 251, 885, 251.
885, 252, 885, 252.
883, 256, 883, 256.
877, 267, 877, 267.
870, 280, 870, 280.
858, 300, 858, 300.
844, 322, 844, 322.
827, 349, 827, 349.
811, 379, 811, 379.
792, 407, 792, 407.
775, 432, 775, 432.
759, 455, 759, 455.
747, 469, 747, 469.
736, 482, 736, 482.

Thank you in advance!

Was it helpful?

Solution

See Custom Painting Approaches for the two common ways to do custom painting:

  1. Add the shapes to be painted to a List and repaint the shapes every time the component is repainted.
  2. draw the shapes onto a BufferedImage and just paint the buffered image when the component is repainted.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top