Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Drawing circles using the coordinate values that are stored in an ArrayList.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Drawing circles using the coordinate values that are stored in an ArrayList.

    I want to make circles onto a JPanel using the x and y coordinates that are generated by another method in the same class. I have stored these coordinate values in an ArrayList<Integer>.
    How do i access these values or the ArrayList inside the paint(Graphics g) method?
    I tried making the ArrayList as final and declared it as a class variable. But, i still couldnt access the list in paint().
    Is there some way to pass parameters into the paint() ??? ???

    Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Drawing circles using the coordinate values that are stored in an ArrayList.

    You can't pass parameters into paint. You can put the object at the class level so that code in the paint method can see it.
    i still couldnt access the list in paint()
    How did you code it?

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing circles using the coordinate values that are stored in an ArrayList.

    Quote Originally Posted by Norm View Post
    You can't pass parameters into paint. You can put the object at the class level so that code in the paint method can see it.

    How did you code it?
    I tried to simplify my code. Now, the x and y coordinates have been stored in x[] and y[] coordinates.

    package manet;
    import java.awt.*;
    import java.awt.Point;
    import java.awt.geom.*;
    import java.util.ArrayList;
    import javax.swing.*;
     
    public class Simulation {
        public void init()  //called by a method of another class in the same package
        {
            JFrame frame = new JFrame("Circles");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            DisplayPanel myPanel = new DisplayPanel(); 
            myPanel.setBackground(Color.WHITE);
            frame.add( myPanel );
            frame.setSize(1000, 540 );
            frame.setVisible( true );
            myPanel.setSize(1000, 500);
            myPanel.setVisible(true);
           }
    }
     
     
     
     
    class DisplayPanel extends JPanel
    {     private int noOfNodes1;
          private int x[];
          private int y[];
          private ArrayList<ArrayList<String>> nodeList1 = new ArrayList<ArrayList<String>>();
          ArrayList<Point>coordinates = new ArrayList();
     
          public void set(int noOfNodes, ArrayList<String> nodeList){    //called by a  //method of another class in the same package.
             noOfNodes1 = noOfNodes;
             x = new int[noOfNodes1+1];
             y = new int[noOfNodes1+1];
             nodeList1 = new ArrayList(nodeList);
             System.out.println(noOfNodes1);
             for(int i = 0; i<noOfNodes+1; i++){
                   ArrayList<String> temp = (ArrayList<String>)nodeList1.get(i);
                   x[i] = Integer.parseInt(temp.get(5));
                   y[i] = Integer.parseInt(temp.get(6));
              }
        }
     
          @Override
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.setColor(Color.BLACK);
                    g.translate(0,500);
                      for(int i = 0; i<x.length; i++){      //This is NOT working
                        g.fillOval(x[i],y[i], 50, 50);
                    //    g.fillOval(1000, -50, 50, 50);
                        }
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Drawing circles using the coordinate values that are stored in an ArrayList.

    Does it work now?

Similar Threads

  1. Help with creating random circles
    By cool48 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2011, 09:18 AM
  2. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 07:57 PM
  3. issue in executing Stored Proc
    By Bhawesh Kurmi in forum JDBC & Databases
    Replies: 1
    Last Post: December 8th, 2010, 09:05 AM
  4. Value Not Being Stored
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 16th, 2010, 09:38 AM
  5. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM

Tags for this Thread