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

    Default Creating ellips and connecting them

    Hi here codes that draws on screen ellipses and connecting them together with lines..
    Java Code:
    package paint;
    
    import javax.swing.JFrame;
    
    /**
     *
     * @author Meko
     */
    public class Main {
    
        public Main() {
          MainPanel selectablePanel = new MainPanel();
          ShapeControl control = new ShapeControl(selectablePanel);
          selectablePanel.addMouseListener(control);
          selectablePanel.addMouseMotionListener(control);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(selectablePanel);
            f.setSize(400,400);
            f.setLocation(200,200);
        
            f.setVisible(true);
        }
      
        public static void main(String[] args) {
          new Main();
        }
    
    }
    
    
    
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("Circle");
        private JButton YvalueButton = new JButton("Draw");
        private JButton ZvalueButton = new JButton("Z");
        private ShapeControl shape = new ShapeControl(this);
        public boolean circleSelected, drawlineSelected, clicked;
        double LineCenter;
        double a, b;
    
        public MainPanel() {
    
    
            JPanel buttonPanel=new JPanel();
    
            circleSelected = false;
            drawlineSelected = false;
    
            XvalueButton.addActionListener(this);
            YvalueButton.addActionListener(this);
            ZvalueButton.addActionListener(this);
            setLayout(new BorderLayout());
            add(buttonPanel,BorderLayout.NORTH);
            buttonPanel.add(XvalueButton);
            buttonPanel.add(YvalueButton);
            buttonPanel.add(ZvalueButton);
            addMouseMotionListener(shape);
            addMouseListener(shape);
      
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
             
                clicked = true;
                 for (int i = 0; i < shape.ellipsList.size(); i++) {
                    MyEllips circle = (MyEllips) shape.ellipsList.get(i);
            circle.repaint();
              
                }
               
    
    }
           if (e.getSource() == YvalueButton) {
    
                drawlineSelected = true;
    
            }
        }
    
    
      
    }
          
    
    
    public class MyEllips extends JPanel {
    Point c ;
        int posX;
        int posY;
        int width =50;
        int height =50;
    
        public MyEllips(Point center) {
       
         c = center;
    
        }
    
        protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int posX = c.x ;
            int posY = c.y;
            g.setColor(Color.BLUE);
            g.fillRect(posX, posY, width, height);
            g.setColor(Color.BLUE);
            g.drawRect(posX, posY, width, height);
    
    
    
        }
    
    
    
    
    }
    
    
    public class ShapeControl extends MouseInputAdapter {
    
        MainPanel selectablePanel;
        public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;
        Point selectedPoint;
        Line2D current_line;
       MyEllips selectedEllips;
        List lineList;
        List ellipsList;
        List pointList;
        Point mainPoint, a, b;
    
        public ShapeControl(MainPanel mp) {
    
    
            pointList = new ArrayList();
            ellipsList = new ArrayList();
            lineList = new ArrayList();
    
            selectablePanel = mp;
    
            dragging = false;
            firstClicked = false;
            secondClicked = false;
    
        }
    
        public void initLine(Point a, Point b) {
    
            lineList.add(new Line2D.Double(a, b));
            pointList.add(a);
            pointList.add(b);
        }
    
        public void initCircle(Point c) {
         
            MyEllips myellips = new MyEllips(c);
         // myellips.setPreferredSize(new Dimension(50,50));
           myellips.setLayout(new BoxLayout(myellips,BoxLayout.X_AXIS));
            myellips.setBackground(Color.red);
           
    
          ellipsList.add(myellips);
        selectablePanel.add(myellips);
          
          myellips.repaint();
    
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
    
            Point p = e.getPoint();
            if (selectablePanel.circleSelected) {
                if (selectablePanel.clicked) {
                    Point center = new Point(e.getX(), e.getY());
    
                    pointList.add(center);
                    initCircle(center);
                    selectablePanel.clicked = false;
    
              }
          }
            if (selectablePanel.drawlineSelected) {
                for (int j = 0; j < ellipsList.size(); j++) {
                    MyEllips circle = (MyEllips) ellipsList.get(j);
                    if (e.getClickCount() == 2) {
                        firstClicked = true;
                        if (firstClicked) {
                            Point point = (Point) pointList.get(j);
                            if (circle.contains(p)) {
                                a = point;
                                firstClicked = false;
                                secondClicked = true;
                            }
                        }
                    } else if (secondClicked) {
                        Point point = (Point) pointList.get(j);
                        if (circle.contains(p)) {
                            b = point;
                            firstClicked = true;
                            secondClicked = false;
                            initLine(a, b);
                            for (int i = 0; i < lineList.size(); i++) {
                                Line2D.Double line = (Line2D.Double) lineList.get(i);
                                current_line = line;
    
                            }
                        }
                    }
                }
    
            }
    
        }
    int downX, downY;
    Point movingPoint = null;
    
        public void mousePressed(MouseEvent e) {
           
    
    
    
            for (int j = 0; j < pointList.size(); j++) {
                Point point = (Point) pointList.get(j);
              
    
                if (e.getX() <= point.x + 25 && e.getX()>= point.x - 25 && e.getY() <= point.y + 25 &&e.getY()>= point.y - 25) {
               System.out.println("Found");
                    downX = e.getX();
          downY = e.getY();
          movingPoint = point;
              
                }
            }
        }
    
        public void mouseReleased(MouseEvent e) {
         if (movingPoint != null) {
          movingPoint.x += e.getX() - downX;
          movingPoint.y += e.getY() - downY;
          movingPoint = null;
       
    
        }
             selectablePanel.repaint();
        }
    
    
    
    }
    My problem is It creates RedSquare and I can move it but When I resize screen IT draws all screnn red..It means MyEllips object is holl screen.And When I try to create new object with clickin button and screen It doenst show.I think it is behind of first created Jpanel ..What must I do?
    Last edited by Mekonom; 11-30-2009 at 10:30 AM.

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

    Default

    no idea?:(

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    public class MyEllips extends JPanel
    If each ellipse is a JPanel, typically only one will appear when drawn.

    Instead: Have MyEllips be a simple object or a descendant of Ellipse2D.Double.
    It will have a method
    Java Code:
    void drawMe(Graphics g) {}
    Then MainPanel.paintComponent() will call drawMe for each point, line, and ellipse.

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

    Default

    and in drawMe methot I will create Ellips2D and it wil be a object of MyEllips? then when I press mouse and add object of My Ellips class it will add this Ellips2d object?
    or in My Ellips class I will create this Ellips 2d and this drawMe(Graphics g){} will be empty?

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Java Code:
    class MyEllips extends Ellipse2D.Double {
    	MyEllips(ellipse parameters) {
    		super(ellipse parameters);
    	}
    	void drawMe(Graphics g) {
    		g.drawEllipse(ellipse parameters for drawing);
    	}
    }
    Similarly for points and lines.
    MainPanel.paintComponent() will call drawMe for each point, line, and ellipse.

    {This will be my last response in this thread.}

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

    Default

    thank for all helps :) regards...

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

    Default

    Hi again..At least I done it work in a 3 months.and here codes :DThanks for helping:) (and how to change thread to SOLVED I dont know ...)
    Java Code:
    package paint;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    
    /**
     *
     * @author Meko
     */
    public class Main {
    
        public Main() {
    
    
            MainPanel selectablePanel = new MainPanel();
            ShapeControl control = new ShapeControl(selectablePanel);
            selectablePanel.addMouseListener(control);
            selectablePanel.addMouseMotionListener(control);
            JFrame f = new JFrame();
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(selectablePanel);
    
            f.setSize(400, 400);
            f.setLocation(200, 200);
    
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            new Main();
        }
    }
    
    
    
    package paint;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Point;
    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.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Meko
     */
    public class MainPanel extends JPanel implements ActionListener {
    
        private JButton XvalueButton = new JButton("Circle");
        private JButton YvalueButton = new JButton("Draw");
        private JButton ZvalueButton = new JButton("Z");
        private ShapeControl shape = new ShapeControl(this);
        public boolean circleSelected, drawlineSelected, clicked;
        double LineCenter;
        double a, b;
    
        public MainPanel() {
    
    
            circleSelected = false;
            drawlineSelected = false;
    
            XvalueButton.addActionListener(this);
            YvalueButton.addActionListener(this);
            ZvalueButton.addActionListener(this);
    
    
            add(XvalueButton);
            add(YvalueButton);
            add(ZvalueButton);
            addMouseMotionListener(shape);
            addMouseListener(shape);
    
    
        }
    
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == XvalueButton) {
                circleSelected = true;
    
                clicked = true;
    
    
            }
    
            if (e.getSource() == YvalueButton) {
    
                drawlineSelected = true;
    
            }
            if (e.getSource() == ZvalueButton) {
    
                System.out.println("" + shape.lineList.size());
    
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
    
            if (circleSelected) {
    
                for (int i = 0; i < shape.ellipsList.size(); i++) {
                    MyEllips circle = (MyEllips) shape.ellipsList.get(i);
    
                    circle.drawMe(g);
    
    
    
                }
    
    
            }
            if (drawlineSelected) {
    
    
                for (int i = 0; i < shape.lineList.size(); i++) {
                    MyLine line = (MyLine) shape.lineList.get(i);
                    line.drawMe(g);
    
                }
            }
            repaint();
        }
    }
    
        
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package paint;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.geom.Ellipse2D;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.border.Border;
    
    /**
     *
     * @author Meko
     */
    public class MyEllips extends Ellipse2D.Double {
    Point c ;
        int posX;
        int posY;
        int width =50;
        int height =50;
    
        public MyEllips(Point center) {
       
         c = center;
    
        }
    
       void drawMe(Graphics g) {
       
    int posX = c.x ;
            int posY = c.y;
            g.setColor(Color.RED);
            g.fillOval(posX-25, posY-25, width, height);
            g.setColor(Color.RED);
            g.drawOval(posX-25, posY-25, width, height);
    
    
    
        }
    
    
    
    
    }
    
    package paint;
    
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.geom.Line2D;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author Meko
     */
    public class MyLine extends Line2D.Double {
    
        Point a, b;
        int posX;
        int posY;
        int posX2;
        int posY2;
    
        public MyLine(Point up, Point down) {
    
            a = up;
            b = down;
    
    
        }
    
        void drawMe(Graphics g) {
    
            int posX = a.x;
            int posY = a.y;
            int posX2 = b.x;
            int posY2 = b.y;
    
    
            g.drawLine(posX, posY, posX2, posY2);
    
    
        }
    }
    
    package paint;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Ellipse2D.Double;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.BorderFactory;
    import javax.swing.event.MouseInputAdapter;
    import sun.awt.RepaintArea;
    
    /**
     *
     * @author Meko
     */
    public class ShapeControl extends MouseInputAdapter {
    
        MainPanel selectablePanel;
        public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;
        Point selectedPoint;
        MyLine current_line;
        MyEllips selectedEllips;
        List lineList;
        List ellipsList;
        List pointList;
        Point mainPoint, a, b;
    
        public ShapeControl(MainPanel mp) {
    
    
            pointList = new ArrayList();
            ellipsList = new ArrayList();
            lineList = new ArrayList();
    
            selectablePanel = mp;
    
            dragging = false;
            firstClicked = false;
            secondClicked = false;
    
        }
    
        public void initLine(Point a, Point b) {
            MyLine myline = new MyLine(a, b);
            lineList.add(myline);
    
            selectablePanel.repaint();
        }
    
        public void initCircle(Point c) {
    
            MyEllips myellips = new MyEllips(c);
    
    
            ellipsList.add(myellips);
    
            selectablePanel.repaint();
    
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            Point p = e.getPoint();
            if (selectablePanel.circleSelected) {
                if (selectablePanel.clicked) {
                    Point center = new Point(e.getX(), e.getY());
    
                    pointList.add(center);
                    initCircle(center);
                    selectablePanel.clicked = false;
    
                }
            }
            if (selectablePanel.drawlineSelected) {
                for (int j = 0; j < ellipsList.size(); j++) {
                    MyEllips circle = (MyEllips) ellipsList.get(j);
    
                    if (e.getClickCount() == 2) {
    
                        firstClicked = true;
                        if (firstClicked) {
                            Point point = (Point) pointList.get(j);
    
                            if (e.getX() <= point.x + 25 && e.getX() >= point.x - 25 && e.getY() <= point.y + 25 && e.getY() >= point.y - 25) {
                                a = point;
                                firstClicked = false;
                                secondClicked = true;
    
                            }
                        }
                    } else if (secondClicked) {
                        Point point = (Point) pointList.get(j);
    
                        if (e.getX() <= point.x + 25 && e.getX() >= point.x - 25 && e.getY() <= point.y + 25 && e.getY() >= point.y - 25) {
                            b = point;
                            firstClicked = true;
                            secondClicked = false;
    
                            initLine(a, b);
    
                        }
                    }
                }
    
            }
    
        }
        int downX, downY;
        Point movingPoint = null;
    
        public void mousePressed(MouseEvent e) {
    
            for (int j = 0; j < pointList.size(); j++) {
                Point point = (Point) pointList.get(j);
    
    
                if (e.getX() <= point.x + 25 && e.getX() >= point.x - 25 && e.getY() <= point.y + 25 && e.getY() >= point.y - 25) {
    
                    downX = e.getX();
                    downY = e.getY();
                    movingPoint = point;
    
                }
            }
        }
    
        public void mouseReleased(MouseEvent e) {
            if (movingPoint != null) {
                movingPoint.x += e.getX() - downX;
                movingPoint.y += e.getY() - downY;
                movingPoint = null;
    
    
            }
    
        }
    }

Similar Threads

  1. connecting to a database
    By kswiss in forum NetBeans
    Replies: 4
    Last Post: 06-25-2009, 06:22 PM
  2. Help connecting
    By xcallmejudasx in forum JDBC
    Replies: 3
    Last Post: 04-16-2009, 07:09 PM
  3. Connecting c with java
    By khajalid in forum Advanced Java
    Replies: 4
    Last Post: 09-11-2008, 04:47 AM
  4. Connecting to a Web server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 07:57 PM
  5. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 02:28 PM

Tags for this Thread

Posting Permissions

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