Results 1 to 11 of 11
  1. #1
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default geting object from other class

    Hi.. ı hae question. I have two calss and I want to take variable form other class with modified whit mouse event and want to show ıt when pressed button bu ıt gaves every tıme 0.0 not modıfıed data .... Any idea? :)
    Java Code:
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("X");
        private JButton YvalueButton = new JButton("Y");
        private JButton ZvalueButton = new JButton("Z");
        public    ShapeControl s = new ShapeControl(this) ;
        private boolean  circleSelected ;
        List circles;
    
        public MainPanel() {
    
          
       circles = new ArrayList();
    
        XvalueButton.addActionListener(this);
       YvalueButton.addActionListener(this);
        ZvalueButton.addActionListener(this);
    
    
        add(XvalueButton);
        add(YvalueButton);
        add(ZvalueButton);
    
        }
    
        public void actionPerformed(ActionEvent e) {
    
             if(e.getSource()==XvalueButton){
          circleSelected =true ;
        circles.add(new Ellipse2D.Double(50,50,50,50));
    
         repaint();
     System.out.print(""+circles.size());
        }
      if(e.getSource()==YvalueButton){
    
             System.out.print("" + s.loc1_X); //here
    
    
      }
    
    
    
    
    
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g ;
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            
            
            if(circleSelected)
    
                for(int i=0 ; i<circles.size();i++)
            g2.draw((Shape) circles.get(i));
    
            
        }
    
    
    
    
    }
    This clas which I wnat to take data. loc1_X in mouseclicked event
    Java Code:
    public class ShapeControl extends MouseInputAdapter {
    
        MainPanel  selectablePanel ;
        boolean dragging,doubleClicked;
        Point2D.Double offset;
        Ellipse2D.Double selectedCircle;
      public double loc1_X,loc1_Y,loc2_X,loc2_Y ;
    
        
        
        public ShapeControl(MainPanel mp ) {
           selectablePanel = mp;
       
            dragging =false;
            offset = new Point2D.Double();
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
    
            if(e.getClickCount()==2){
                doubleClicked = true;
            Point p = e.getPoint();
    
            List<Ellipse2D.Double> list = selectablePanel.circles;
            for (int j = 0; j < list.size(); j++) {
                Ellipse2D.Double circle = (Ellipse2D.Double) list.get(j);
    
    
    
                if (circle.contains(p)) {
       System.out.println("Double");
          
    
             loc1_X= circle.x ;
                  
           loc1_Y= circle.y ;
    
            
                   
                    break;
                }
    
    
    
            }
            }else {
                    Point p = e.getPoint();
    
            List<Ellipse2D.Double> list = selectablePanel.circles;
            for (int j = 0; j < list.size(); j++) {
                Ellipse2D.Double circle = (Ellipse2D.Double) list.get(j);
    
    
    
                if (circle.contains(p)) {
    
                   loc2_X= circle.x ;
                   loc2_Y= circle.y ;
    
               break;
    
    
                }
    
    
    
            }
        }
    
        }

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

  3. #3
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Here I try to take coordınates from object which I clicked and draw a line wich .ı will click :) it will connect the wiht drawwing line

  4. #4
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    My paint componen on upper class and I must reach this coordinates in That class..

  5. #5
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    In my class I used mouseClick event and this variable in mouse event it tooks corrdinate.Maybe because of that i cant reach it from other class?

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

    Default

    Your class ShapeControl extends MouseInputAdapter, and object "s" is an instance of ShapeControl. For a MouseInputAdapter to work, it must be added to a component via addMouseListener(s) or addMouseMotionListener(s), or addMouseWheelListener(s).

    I re-ask my question above: "Do you need to add "s" (terrible name for a variable, by the way) as a MouseListener to something perhaps?"

    You may wish to attempt to answer this question.

  7. #7
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    I think I dont add s as a MouseListener. Yes s is object of ShapeControl. and I spesified it in MainPanel class.IN MainPanel class I have paint component and I must get corrdinates from ShapeCOntrol class whic gets corrdinates with mouse click event and add them to variable like loc1_x = circle.x .this is in ShapeControl class and in mouseClick event .It works . But in MainPanel class when I tryed to reach this loc1_x it says that it is allvays 0.
    I used like s.loc1_x ; and here s is ShapeControl s = new ShapeCOntrol(this) ; ....

  8. #8
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Here my Codes .

    This is MAinPanel which I try to show data of variabele from other class
    Java Code:
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("X");
        private JButton YvalueButton = new JButton("Y");
        private JButton ZvalueButton = new JButton("Z");
        private ShapeControl s = new ShapeControl(this);
        private boolean circleSelected;
        List circles;
    
        public MainPanel() {
    
    
            circles = new ArrayList();
    
            XvalueButton.addActionListener(this);
            YvalueButton.addActionListener(this);
            ZvalueButton.addActionListener(this);
    
    
            add(XvalueButton);
            add(YvalueButton);
            add(ZvalueButton);
    
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
                circles.add(new Ellipse2D.Double(50, 50, 50, 50));
    
                repaint();
    
            }
            if (e.getSource() == YvalueButton) {
            }
    
    
    
    
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
    
    
            if (circleSelected) {
                for (int i = 0; i < circles.size(); i++) {
                    g2.draw((Shape) circles.get(i));
                }
            }
        
        }
    }
    This is ShapeControl class which includes MouseAdapter and local1_x

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package paıntwıthelips;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Meko
     */
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("X");
        private JButton YvalueButton = new JButton("Y");
        private JButton ZvalueButton = new JButton("Z");
        private ShapeControl s = new ShapeControl(this);
        private boolean circleSelected;
        List circles;
    
        public MainPanel() {
    
    
            circles = new ArrayList();
    
            XvalueButton.addActionListener(this);
            YvalueButton.addActionListener(this);
            ZvalueButton.addActionListener(this);
    
    
            add(XvalueButton);
            add(YvalueButton);
            add(ZvalueButton);
    
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
                circles.add(new Ellipse2D.Double(50, 50, 50, 50));
    
                repaint();
    
            }
            if (e.getSource() == YvalueButton) {
            }
    
    
    
    
    
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
    
    
            if (circleSelected) {
                for (int i = 0; i < circles.size(); i++) {
                    g2.draw((Shape) circles.get(i));
                }
            }
            
            if(s.doubleClicked){ // Here is PRoblem.. not recognize  changed values 
            
            g.drawLine((int)s.loc1_X,(int)s.loc1_Y,(int)s.loc2_X,(int)s.loc2_Y);
            
            
            }
            
        
        }
    }

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

    Default

    Again if the ShapeControl object is not added to a component, while it will exist in the class, its mouseClicked will never be called. You need to wrap your head around this as I don't know of any other way to tell you this.

    You will want to read the Sun tutorial on how to write and use a MouseListener. It may help you to re-write this code since I think that you may need to restart this from scratch.
    Last edited by Fubarable; 11-12-2009 at 01:45 AM.

  10. #10
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Vau.... I add addMouseListener(s); and it works :) thanks alot :)))) but I didnot understand why my ellips list have problem? when I click button it creates and compares with point of currsor.it works.what is problem ?codding?

  11. #11
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    A one question? How can I move this ellips and line together?line binded. for example if ellips on center and have two line connected when I drag ellips this two line must have move too..but how can I understand that wich line first x and y and which line last xand y must be changet to new corrdinates?

Similar Threads

  1. Replies: 1
    Last Post: 08-07-2009, 09:37 PM
  2. Geting below em while starting the was6.1 server.
    By kumarjp in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-08-2009, 08:52 PM
  3. Passing SWT object to another class
    By Vinaya Lal Shrestha in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 01:50 PM
  4. Getting name of object class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:22 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •