Back again. Could anyone tell me why the points don't seem to be updating, thus drawing a red rect is failing on run? Essentially, I've been trying to get a more complicated version of this--an outline of a rect on drag, but right now even a stationary rectangle would be reassuring. I think I'm seeing a tiny red dot (points not updating in mouseDragged), but maybe I don't understand how MouseEvent.getPoint() works in conjunction with mouseDragged()? Here's my code, there's quite a few extra variables floating around from my main goal and testing...
With main...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab2Test extends JFrame
{
boolean pressed = false, dragged = false, released = false;
Point start = new Point(0,0);
Point old = new Point(0,0);
Point last = new Point(0,0);
public Lab2Test()
{
super("Lab 2");
setBounds(100,100,500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
addMouseListener(new MyListenerTest(this));
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g)
{
if(pressed)
{
super.paint(g);
}
g.setColor(Color.red);
System.out.println("old; x: " + (old.x - start.x) + " y: " + (old.y - start.y));
System.out.println("new; x: " + (last.x - start.x) + " y: " + (last.y - start.y));
g.drawRoundRect(start.x, start.y, (last.x - start.x), (last.y - start.y), 20, 20);
}
public static void main(String args[])
{
new Lab2Test();
}
}
And the adapter + motionlistener
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyListenerTest extends MouseAdapter implements MouseMotionListener
{
Lab2Test theLab;
public MyListenerTest(Lab2Test ref)
{
theLab = ref;
}
public void mouseClicked(MouseEvent e)
{
System.out.println("mouseClicked at "+e.getX()+", "+e.getY());
}
public void mousePressed(MouseEvent e)
{
theLab.pressed = true;
theLab.start = theLab.old = theLab.last = e.getPoint();
theLab.repaint();
}
public void mouseDragged(MouseEvent e)
{
theLab.dragged = true;
theLab.old = theLab.last;
theLab.last = e.getPoint();
theLab.repaint();
}
public void mouseReleased(MouseEvent e)
{
theLab.pressed = false;
theLab.dragged = false;
theLab.repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
Thank you in advance for your help.
-k