Major help needed with drawing rectangles using JFrame and Mouse Events
Good evening!
So, I've been working on this program that draws rectangles using JFrame and implementing MouseListener and MouseMotionListener. You should be able to click and drag the mouse, drawing a rectangle, no matter where in the frame you start out. I have not had much luck getting it to work. The frame will display, but I am unable to draw the rectangles. It also produces one error when I compile, "JDrawRect.java:81: cannot find symbol symbol : method paintComponent(java.awt.Graphics) location: class javax.swing.JFrame super.paintComponent(g);". When the program runs, in addition to the frame loading, there is an additional error, "Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container".
What am I doing wrong?
Damien
Code:
package project2;
// JDrawRect.java
// Demonstrating mouse events.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JDrawRect extends JFrame
implements MouseListener, MouseMotionListener {
//imported variables follow...
private int pointCount = 0;
private Point points[] = new Point[100];
private Point points2[] = new Point[100];
private Point start = new Point();
private Point end = new Point();
Rectangle rect = new Rectangle();
//imported variables end
//private static final long serialVersionUID = 1L;
private JLabel mousePosition;
// set up GUI and register mouse event handlers
public JDrawRect()
{
super( "Drawing Rectangles" );
mousePosition = new JLabel();
mousePosition.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add( mousePosition, BorderLayout.CENTER );
addMouseListener( this ); // listens for own mouse and
addMouseMotionListener( this ); // mouse-motion events
//setSize( 400, 300 );
//setVisible( true );
}
// MouseListener event handlers
// handle event when mouse released immediately after press
public void mouseClicked( MouseEvent event ){}
// handle event when mouse pressed
public void mousePressed( MouseEvent event )
{
start = event.getPoint();
}
// handle event when mouse released after dragging
public void mouseReleased( MouseEvent event )
{
points[pointCount] = start;
points2[pointCount] = event.getPoint();
pointCount++;
rect.setFrameFromDiagonal(start, start);
repaint();
}
// handle event when mouse enters area
public void mouseEntered( MouseEvent event ){}
// handle event when mouse exits area
public void mouseExited( MouseEvent event ){}
// MouseMotionListener event handlers
// handle event when user drags mouse with button pressed
public void mouseDragged( MouseEvent event )
{
end = event.getPoint();
rect.setFrameFromDiagonal(start, end);
repaint();
}
// handle event when user moves mouse
public void mouseMoved( MouseEvent event ){}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Draw line being dragged.
g2.setPaint(Color.red);
g2.draw(rect);
// Draw lines between points in arrays.
g2.setPaint(Color.blue);
Rectangle r = new Rectangle();
for (int i =0; i < pointCount; i++)
{
r.setFrameFromDiagonal(points[i], points2[i]);
g2.fill(r);
}
}
public static void main( String args[] )
{
//JDrawRect application = new JDrawRect();
//application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JDrawRect test = new JDrawRect();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
} // end class JDrawRect