Hi everyone :))))
there is a problem here !this code should draw a rectangle whenever I presse on the mouse ! but actually it doesn't do that !!
when I execute the prog. it gives me the frame window ! but when I presse the mouse ,it should draw a rectangle !
but in fact it doesn't draw anything !!
so what is the wrong with my code ??!
thanks :)

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class VectorDraw extends JPanel {
private Point P1,P2;
private Vector V;
private Boolean flag ;
private Rectangle CurrRect,NewRect;
public VectorDraw(){
P1=new Point (0,0); 
P2=new Point (0,0); 
V=new Vector ();
Inner1 Handler1=new Inner1 ();
Inner2 Handler2=new Inner2 ();

addMouseListener(Handler1 );
addMouseMotionListener (Handler2 );
}
private class Inner1 extends MouseAdapter {
    public void mousePressed(MouseEvent E){
    P1=E.getPoint();

    }

    public void mouseReleased(MouseEvent E){
        P2=E.getPoint();//why !!
        NewRect=new Rectangle (Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),
                Math.abs(P1.x-P2.x),Math.abs(P1.y-P2.y)
                );
        //flag=false;
        V.add(NewRect);
        repaint();

        }

} 
private class Inner2 extends MouseMotionAdapter {
    public void  mouseDragged(MouseEvent E){
        P2=E.getPoint();
        CurrRect=new Rectangle (Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),
                Math.abs(P1.x-P2.x),Math.abs(P1.y-P2.y)
                );
        flag=false;
        repaint();

        }

}
public void paintComponent(Graphics g){
    super.paintComponent(g);
g.setColor(Color.BLUE);
for (int i=0;i<V.size();i++){
    //= new Rectangle ();
    Rectangle R;
    R=(Rectangle)V.get(i);
    g.drawRect(R.x, R.y, R.width, R.height);


}
g.setColor(Color.RED);
if (!flag){
    g.drawRect(CurrRect.x, CurrRect.y, CurrRect.width, CurrRect.height);


}



}
    public static void main(String[] args) {
        VectorDraw O=new VectorDraw();
        JFrame obj=new JFrame();
        obj.setSize (600,600);
        obj.setVisible(true);
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

You create a frame and a VectorDraw panel, but you don't add the panel to the frame!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.