Problem in drawing a rectangle in swings...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumuka
    New Member
    • Oct 2007
    • 38

    Problem in drawing a rectangle in swings...

    Hello,

    I'm doing a project in java using swings ,im able to create a panel and frame but i have a problem in event handling.I have added the MouseMotionList ener but it's giving some error .Here i want to draw a rectangle when the user clicks and drags the mouse button and releases a rectangle has to be drawn.can somebody help me to debug the error ASAP.

    My code goes like this:

    import javax.swing.*;
    import javax.swing.JPa nel;
    import javax.swing.Bor derFactory;
    import java.awt.Color;
    import java.awt.Dimens ion;
    import java.awt.Graphi cs;
    //import javax.swing.Swi ngUtilities;


    public class Swing_Create_Fr ame
    {
    public static void main(String[] args)
    {
    JFrame frame = new JFrame("Frame in Java Swing");
    frame.setBounds (400,400, 400,450);
    frame.setVisibl e(true);
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.add(new MyPanel());
    frame.pack();
    }
    }
    class MyPanel extends JPanel {

    public MyPanel() {
    setBorder(Borde rFactory.create LineBorder(Colo r.red));
    }

    public Dimension getPreferredSiz e() {
    return new Dimension(400,4 00);
    }

    public void paintComponent( Graphics g) {
    super.paintComp onent(g);
    // Draw Text
    g.drawString("T his is my custom Panel!",10,20);
    }

    MyListener myListener = new MyListener();
    void addMouseListene r(myListener);

    addMouseMotionL istener(myListe ner);

    public class MyListener extends MouseInputAdapt er {
    public void mousePressed(Mo useEvent e) {
    int x = e.getX();
    int y = e.getY();
    currentRect = new Rectangle(x, y, 0, 0);
    updateDrawableR ect(getWidth(), getHeight());
    repaint();
    }

    public void mouseDragged(Mo useEvent e) {
    updateSize(e);
    }

    public void mouseReleased(M ouseEvent e) {
    updateSize(e);
    }

    void updateSize(Mous eEvent e) {
    int x = e.getX();
    int y = e.getY();
    currentRect.set Size(x - currentRect.x,
    y - currentRect.y);
    updateDrawableR ect(getWidth(), getHeight());
    Rectangle totalRepaint = rectToDraw.unio n(previouseRect Drawn);
    repaint(totalRe paint.x, totalRepaint.y,
    totalRepaint.wi dth, totalRepaint.he ight);
    }
    }
    }
    }

    Errors i'm getting are
    Multiple markers at this line
    - MyListener cannot be resolved to a type
    - Watchpoint:MyPa nel [access and modification] - myListener
    - MyListener cannot be resolved to a type

    Multiple markers at this line
    - Syntax error on token ")", { expected after this token
    - myListener cannot be resolved to a type
    - Syntax error on token "myListener ", VariableDeclara torId expected after this token


    These are the errors i'm getting at line no 38 and 39 which i have underlined.

    Can anybody tell me what should i do?

    Thanks in anticipation,
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code
    2.) Read Sun's event handling tutorial here.

    Comment

    • sumuka
      New Member
      • Oct 2007
      • 38

      #3
      Originally posted by r035198x
      1.) Use code tags when posting code
      2.) Read Sun's event handling tutorial here.

      I read it but i'm not able to figure to my mistake.Plz anybody guide me ....

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sumuka
        I read it but i'm not able to figure to my mistake.Plz anybody guide me ....
        What do you want to achive with this line then?

        void addMouseListene r(myListener);
        If you look at the examples from the link I posted, you'll see that you don't put the return type (in this case void) when registering the listener. You are supposed to be calling a method not defining one.

        Comment

        Working...