0

I am trying to write a code that draws a filled rectangle with a changeable size as the mouse is dragged. When I run this code it give me a blank window. it doesn't draw anything whenever I press and drag. What's the problem in this code?

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;

public class pr2 extends JFrame implements MouseListener, MouseMotionListener 
{
    Container cp;

    Point p1;
    Point p2;

    Rectangle2D rectangle;

    public pr2 (String Name)
    {
        super (Name);
        setLayout(new FlowLayout ());
        setBackground(Color.LIGHT_GRAY);
        setSize(500, 500);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        cp = getContentPane ();

        addMouseListener(this);
     }

    public boolean isPointTwoInQuadOne(Point p1, Point p2) 
    {
       return p1.x >= p2.x && p1.y >= p2.y;
    }

    public boolean isPointTwoInQuadTwo (Point p1, Point p2) 
    {
        return p1.x <= p2.x && p1.y >= p2.y;
    }

    public boolean isPointTwoInQuadThree(Point p1, Point p2) 
    {
        return p1.x <= p2.x && p1.y <= p2.y;
    }

    public boolean isPointTwoInQuadFour (Point p1, Point p2) 
    {
        return p1.x >= p2.x && p1.y <= p2.y;
    }

    public void paintComponent(Graphics g) 
    {
         g.setColor(Color.BLACK);
         super.paintComponents(g);
         Graphics2D g2 = (Graphics2D)g;
         if (rectangle != null) 
         {
             g2.fill(rectangle);
         }
     }

     @Override
     public void mousePressed(MouseEvent e) 
     {
         p1 = e.getPoint();
         rectangle = new Rectangle2D.Double(p1.x, p1.y, p1.x - p1.x, p1.y - p1.y);
     }

    @Override
    public void mouseDragged(MouseEvent e) 
    {
          p2 = e.getPoint();
          if (isPointTwoInQuadOne(p1, p2)) 
          { 
                rectangle.setRect(p2.x, p2.y, p2.x, p1.y);
                repaint();
          } 
          else if (isPointTwoInQuadTwo(p1, p2))
          {
                rectangle.setRect(p1.x, p2.y, p2.x - p1.x, p1.y - p2.y);
                repaint();
          }
          else if (isPointTwoInQuadThree(p1, p2))
          {
                rectangle.setRect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
                repaint();
          }
          else if (isPointTwoInQuadFour(p1, p2))
          {
                rectangle.setRect(p2.x, p1.y, p1.x, p2.y);
                repaint();
          }   
      }

      @Override
      public void mouseClicked(MouseEvent e) { }

      @Override
      public void mouseReleased(MouseEvent e) { }

      @Override
      public void mouseEntered(MouseEvent e) { }

      @Override
      public void mouseExited(MouseEvent e) { }

      @Override
      public void mouseMoved(MouseEvent e) { }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
TheGrimBoo
  • 199
  • 1
  • 4
  • 14
  • 1
    You mean like [this](http://stackoverflow.com/questions/15776549/create-rectangle-with-mouse-drag-not-draw/15776976#15776976) or [this](http://stackoverflow.com/questions/13948122/drawing-a-bounding-rectangle-to-select-what-area-to-record/13948198#13948198) or [this](http://stackoverflow.com/questions/22645172/java-draws-rectangle-one-way-not-both/22645343#22645343) or [this](http://stackoverflow.com/questions/24420761/issue-with-addmousemotionlistener-getting-wrong-coordinates/24421180#24421180)? – MadProgrammer Dec 24 '15 at 11:32
  • @MadProgrammer http://stackoverflow.com/questions/22645172/java-draws-rectangle-one-way-not-both/22645343#22645343 – TheGrimBoo Dec 24 '15 at 11:34

1 Answers1

1

Start with the obvious

  • JFrame doesn't have a paintComponent method (you're calling super.paintComponents <- Note the 's', which would normally be a bad idea
  • You're adding a MouseListener directly to the frame, which might not be notified of mouse events if a child component above it also has a MouseListener registered to it, besides, you also want to add a MouseMotionListener

Suggestions:

  • Start with a custom component, extending from something like JPanel, override it's paintComponent method, make sure you add the @Override annotation (which will raise a compiler error if you've attempted to override a method which is not implemented by the parent class) and call super.paintComponent
  • Add both a MouseListener AND MouseMotionListener to it.
  • Add an instance of the custom component to an instance of a JFrame
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366