Results 1 to 6 of 6

Thread: rezise shape

  1. #1
    frankenstein is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default rezise shape

    hi,

    I am trying to compile this example:

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class ResizeRectangle extends JPanel {
    private int SIZE = 8;
    private Rectangle2D[] points = { new Rectangle2D.Double(50, 50,SIZE, SIZE), new Rectangle2D.Double(150, 100,SIZE, SIZE) };
    Rectangle2D s = new Rectangle2D.Double();

    ShapeResizeHandler ada = new ShapeResizeHandler();

    public ResizeRectangle() {
    addMouseListener(ada);
    addMouseMotionListener(ada);
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    for (int i = 0; i < points.length; i++) {
    g2.fill(points[i]);
    }
    s.setRect(points[0].getCenterX(), points[0].getCenterY(),
    Math.abs(points[1].getCenterX()-points[0].getCenterX()),
    Math.abs(points[1].getCenterY()- points[0].getCenterY()));

    g2.draw(s);
    }

    class ShapeResizeHandler extends MouseAdapter {
    Rectangle2D r = new Rectangle2D.Double(0,0,SIZE,SIZE);
    private int pos = -1;
    public void mousePressed(MouseEvent event) {
    Point p = event.getPoint();

    for (int i = 0; i < points.length; i++) {
    if (points[i].contains(p)) {
    pos = i;
    return;
    }
    }
    }

    public void mouseReleased(MouseEvent event) {
    pos = -1;
    }

    public void mouseDragged(MouseEvent event) {
    if (pos == -1)
    return;

    points[pos].setRect(event.getPoint().x,event.getPoint().y,poi nts[pos].getWidth(),
    points[pos].getHeight());
    repaint();
    }
    }

    public static void main(String[] args) {

    JFrame frame = new JFrame("Resize Rectangle");

    frame.add(new ResizeRectangle());
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }
    }

    But always get this error:

    ResizeRectangle.java:20: addMouseMotionListener(java.awt.event.MouseMotionL istener) in java.awt.Component cannot be applied to (ResizeRectangle.ShapeResizeHandler)
    addMouseMotionListener(ada);
    ^
    1 error



    Any idea?

    regards

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code compiles and runs for me. I'm not sure why you're getting this error.

  3. #3
    frankenstein is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    mmm did you compile the code in a standar way?, i mean:

    javac ResizeRectangle.java

    could be my version of java, i am using leopard my version of java is:

    java version "1.5.0_19"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_19-b02-304)
    Java HotSpot(TM) Client VM (build 1.5.0_19-137, mixed mode, sharing)

    what is your version?

    regards

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I used a Java 1.6 compliant compiler, but when I recompiled with 1.5 compliance, it still worked fine.

    Which line is throwing the error? One of these in the constructor?
    Java Code:
      public ResizeRectangle() {
        addMouseListener(ada);
        addMouseMotionListener(ada);
      }

  5. #5
    frankenstein is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    yes, i got the error in line number 20 in the constructor, wich is:

    addMouseMotionListener(ada);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, re-write, and recompile. This is the code that I used:
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ResizeRectangle extends JPanel {
      private int SIZE = 8;
      private Rectangle2D[] points = { new Rectangle2D.Double(50, 50, SIZE, SIZE),
          new Rectangle2D.Double(150, 100, SIZE, SIZE) };
      Rectangle2D s = new Rectangle2D.Double();
    
      ShapeResizeHandler ada = new ShapeResizeHandler();
    
      public ResizeRectangle() {
        addMouseListener(ada);
        addMouseMotionListener(ada);
      }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        Graphics2D g2 = (Graphics2D) g;
    
        for (int i = 0; i < points.length; i++) {
          g2.fill(points[i]);
        }
        s.setRect(points[0].getCenterX(), points[0].getCenterY(), Math
            .abs(points[1].getCenterX() - points[0].getCenterX()), Math
            .abs(points[1].getCenterY() - points[0].getCenterY()));
    
        g2.draw(s);
      }
    
      class ShapeResizeHandler extends MouseAdapter {
        Rectangle2D r = new Rectangle2D.Double(0, 0, SIZE, SIZE);
        private int pos = -1;
        public void mousePressed(MouseEvent event) {
          Point p = event.getPoint();
    
          for (int i = 0; i < points.length; i++) {
            if (points[i].contains(p)) {
              pos = i;
              return;
            }
          }
        }
    
        public void mouseReleased(MouseEvent event) {
          pos = -1;
        }
    
        public void mouseDragged(MouseEvent event) {
          if (pos == -1)
            return;
    
          points[pos].setRect(event.getPoint().x, event.getPoint().y, points[pos]
              .getWidth(), points[pos].getHeight());
          repaint();
        }
      }
    
      public static void main(String[] args) {
    
        JFrame frame = new JFrame("Resize Rectangle");
    
        frame.add(new ResizeRectangle());
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 03:25 AM
  2. basic shape compose gui
    By entropicwarrior in forum New To Java
    Replies: 7
    Last Post: 04-04-2009, 12:04 PM
  3. Drawing a shape on an image
    By Yoruichi in forum Java 2D
    Replies: 2
    Last Post: 03-29-2009, 02:49 PM
  4. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 PM
  5. How to change shape of JButton
    By FaRuK in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •