Results 1 to 6 of 6

Thread: How to call?

  1. #1
    scopolamine is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default How to call?

    I want to draw an ellipse/line/rectangle when I click the JButtons. So far this is what I did. No compile error. But I don't figure out how to call functions on JButtons.

    Please help me.

    Java Code:
    import java.awt.AlphaComposite;
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.*;
    import java.util.ArrayList;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.ButtonGroup;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    
    
    public class DrawingBoardWithMatrix extends JFrame{
    
      public static void main(String[] args) {
          new DrawingBoardWithMatrix();
      }
    
      public DrawingBoardWithMatrix() {
        super("Mini Paint Applet");
        ButtonGroup cbg = new ButtonGroup();
        JButton lineButton = new JButton("Line");
        JButton ovalButton = new JButton("Oval");
        JButton rectangleButton = new JButton("Rectangle");
        cbg.add(lineButton);
        cbg.add(ovalButton);
        cbg.add(rectangleButton);
        JPanel shapePanel = new JPanel(new FlowLayout());
        shapePanel.add(lineButton);
        shapePanel.add(ovalButton);
        shapePanel.add(rectangleButton);
        this.setLayout(new BorderLayout());
        this.add(shapePanel, BorderLayout.NORTH);
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new PaintSurface(), BorderLayout.CENTER);
        this.setVisible(true);
      }
    
      private class PaintSurface extends JComponent {
        ArrayList<Shape> shapes = new ArrayList<Shape>();
        String shapeType = "Rectangle";
    
        Point startDrag, endDrag;
    
        public PaintSurface() {
          this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
              startDrag = new Point(e.getX(), e.getY());
              endDrag = startDrag;
              repaint();
            }
    
            public void mouseReleased(MouseEvent e) {
    //          Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
              Shape l = makeLine(startDrag.x, startDrag.y, e.getX(), e.getY());
    //          Shape el = makeEllipse(startDrag.x, startDrag.y, e.getX(), e.getY());
    //          shapes.add(r);
                shapes.add(l);
    //          shapes.add(el);
              startDrag = null;
              endDrag = null;
              repaint();
            }
          });
    
          this.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
              endDrag = new Point(e.getX(), e.getY());
              repaint();
            }
          });
        }
        private void paintBackground(Graphics2D g2){
          g2.setPaint(Color.LIGHT_GRAY);
          for (int i = 0; i < getSize().width; i += 10) {
            Shape line = new Line2D.Float(i, 0, i, getSize().height);
            g2.draw(line);
          }
    
          for (int i = 0; i < getSize().height; i += 10) {
            Shape line = new Line2D.Float(0, i, getSize().width, i);
            g2.draw(line);
          }
    
    
        }
        public void paint(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          paintBackground(g2);
          Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
          int colorIndex = 0;
    
          g2.setStroke(new BasicStroke(2));
          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
    
          for (Shape s : shapes) {
            g2.setPaint(Color.BLACK);
            g2.draw(s);
            g2.setPaint(colors[(colorIndex++) % 6]);
            g2.fill(s);
          }
    
          if (startDrag != null && endDrag != null) {
            g2.setPaint(Color.LIGHT_GRAY);
    //        Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    //        g2.draw(r);
    
            Shape l = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
            g2.draw(l);
    //        Shape el = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    //        g2.draw(el);
          }
        }
    
        private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
            return new Line2D.Float(x1, y1, x2, y2);
     }
    
    
    //    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
    //      return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    //    }
    
    /*    private Ellipse2D.Float makeEllipse(int x1, int y1, int x2, int y2) {
          return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
        }
    */  
    }
    }

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

    Default

    Quote Originally Posted by scopolamine View Post
    I want to draw an ellipse/line/rectangle when I click the JButtons. So far this is what I did. No compile error. But I don't figure out how to call functions on JButtons.
    Please have a look at the tutorials on this which you can find here:
    How to Use Buttons, Check Boxes, and Radio Buttons
    How to Write an Action Listener

    Much luck!

  3. #3
    scopolamine is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please have a look at the tutorials on this which you can find here:
    How to Use Buttons, Check Boxes, and Radio Buttons
    How to Write an Action Listener

    Much luck!
    When I add the ActionListener, it gives me a compile error.

    Leaking in this constructor.

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

    Default

    Quote Originally Posted by scopolamine View Post
    When I add the ActionListener, it gives me a compile error.

    Leaking in this constructor.
    Have you showed us the code where you try to do this, because I can't find it, so I don't know what you're doing wrong.

  5. #5
    scopolamine is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Here it is(edited version):

    Java Code:
    import java.awt.AlphaComposite;
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.*;
    import java.util.ArrayList;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.ButtonGroup;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class DrawingBoardWithMatrix extends JFrame{
    
      public static void main(String[] args) {
          new DrawingBoardWithMatrix();
      }
    
      public DrawingBoardWithMatrix() {
        super("Mini Paint Applet");
        ButtonGroup cbg = new ButtonGroup();
        JButton lineButton = new JButton("Line");
        JButton ovalButton = new JButton("Oval");
        JButton rectangleButton = new JButton("Rectangle");
        lineButton.addActionListener( this );
        cbg.add(lineButton);
        cbg.add(ovalButton);
        cbg.add(rectangleButton);
        JPanel shapePanel = new JPanel(new FlowLayout());
        shapePanel.add(lineButton);
        shapePanel.add(ovalButton);
        shapePanel.add(rectangleButton);
        this.setLayout(new BorderLayout());
        this.add(shapePanel, BorderLayout.NORTH);
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new PaintSurface(), BorderLayout.CENTER);
        this.setVisible(true);
      }
    
      
      private class PaintSurface extends JComponent {
        ArrayList<Shape> shapes = new ArrayList<Shape>();
        String shapeType = "Rectangle";
    
        Point startDrag, endDrag;
    
        public PaintSurface() {
          this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
              startDrag = new Point(e.getX(), e.getY());
              endDrag = startDrag;
              repaint();
            }
    
      public void ActionPerformed( ActionEvent e ) {
      Object source = e.getSource();
      if ( source == lineButton )
       System.out.println( "Button clicked" );
     }
            
            public void mouseReleased(MouseEvent e) {
    //          Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
              Shape l = makeLine(startDrag.x, startDrag.y, e.getX(), e.getY());
    //          Shape el = makeEllipse(startDrag.x, startDrag.y, e.getX(), e.getY());
    //          shapes.add(r);
                shapes.add(l);
    //          shapes.add(el);
              startDrag = null;
              endDrag = null;
              repaint();
            }
          });
    
          this.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
              endDrag = new Point(e.getX(), e.getY());
              repaint();
            }
          });
        }
        private void paintBackground(Graphics2D g2){
          g2.setPaint(Color.LIGHT_GRAY);
          for (int i = 0; i < getSize().width; i += 10) {
            Shape line = new Line2D.Float(i, 0, i, getSize().height);
            g2.draw(line);
          }
    
          for (int i = 0; i < getSize().height; i += 10) {
            Shape line = new Line2D.Float(0, i, getSize().width, i);
            g2.draw(line);
          }
    
    
        }
        public void paint(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          paintBackground(g2);
          Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
          int colorIndex = 0;
    
          g2.setStroke(new BasicStroke(2));
          g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
    
          for (Shape s : shapes) {
            g2.setPaint(Color.BLACK);
            g2.draw(s);
            g2.setPaint(colors[(colorIndex++) % 6]);
            g2.fill(s);
          }
    
          if (startDrag != null && endDrag != null) {
            g2.setPaint(Color.LIGHT_GRAY);
    //        Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    //        g2.draw(r);
    
            Shape l = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
            g2.draw(l);
    //        Shape el = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    //        g2.draw(el);
          }
        }
    
        private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
            return new Line2D.Float(x1, y1, x2, y2);
     }
    
    
    //    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
    //      return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    //    }
    
    /*    private Ellipse2D.Float makeEllipse(int x1, int y1, int x2, int y2) {
          return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
        }
    */  
    }
    }

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

    Default

    When posting code that causes an error, please post the error message and indicate which line causes the error. In your code, this line is giving you an error:

    Java Code:
    lineButton.addActionListener(this);

    "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (DrawingBoardWithMatrix)"

    Because this, the current DrawingBoardWithMatrix object is not an ActionListener, so the error makes sense. You can only pass an ActionListener into that method. I suggest that you create an anonymous inner ActionListener object for this method. You can read up on these here: Getting in Touch with your Inner Class

Similar Threads

  1. Call sql view
    By anilkumar_vist in forum Advanced Java
    Replies: 6
    Last Post: 10-12-2010, 04:46 PM
  2. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 11:03 AM
  3. gwt rpc asynchronous call
    By trust in forum Web Frameworks
    Replies: 1
    Last Post: 08-11-2009, 10:42 AM
  4. not able to call the ejb
    By deepak9976 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-07-2009, 10:12 AM
  5. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 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
  •