Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 11-03-2005, 03:09 PM   #1 (permalink)
ibexpiotr's Avatar
 
True Techie

Join Date: Nov 2003

Posts: 197

ibexpiotr is on a distinguished road

Send a message via AIM to ibexpiotr
Default java ArrayList s

my ArrayList stores Point objects (each with x, y coordinates), how do i go about accessing their member functions using ArrayList.

i mean this. in c++, with nodes,or smoething else i'd use
current->data().getSomething();
ibexpiotr is offline  
Old 11-04-2005, 06:36 AM   #2 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

Code:
int xCoord = ((ObjectType)ArrayListName[indexOfObject]).getXCoord();
Something like that. Dont forget you need to cast any object you are accessing in the array list since it can hold any data type. In the above you are casting the object in the array list at poistion indexOfObject as type ObjectType and then calling the getXCoord() method on it to return an int that is stored in the variable xCoord.
fitzjj is offline  
Old 11-04-2005, 08:43 AM   #3 (permalink)
ibexpiotr's Avatar
 
True Techie

Join Date: Nov 2003

Posts: 197

ibexpiotr is on a distinguished road

Send a message via AIM to ibexpiotr
Default whats wrong with my logic

im writing a program that draws stuff, rite now im only trying to draw lines, and storing their values in an arraylist, and then reading them back and drawing.
heres the code
Code:
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class DrawPanel extends JPanel
{
       private Point point1 = null, point2 = null;
       private ArrayList line = new ArrayList();

       public DrawPanel()
       {
               DrawListener listener = new DrawListener();
               addMouseListener (listener);
               addMouseMotionListener(listener);

               setBackground (Color.white);
               setPreferredSize (new Dimension(400, 300));
       }

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

               page.setColor (Color.black);
               for(int x = 0; x < line.size(); ++x)
               {
                       if(line.get(x) != null & line.get(x) != null)
                       {
							  		page.drawLine (((Point)line.get(x)).x, ((Point)line.get(x)).y, ((Point)line.get(x+1)).x, ((Point)line.get(x+1)).y);
                       }
               }
       }

       private class DrawListener implements MouseListener, MouseMotionListener
       {
               public void mousePressed (MouseEvent event)
               {
                       point1 = event.getPoint();
                       line.add(point1);
               }
               public void mouseDragged(MouseEvent event)
               {
                       point2 = event.getPoint();
                       line.add(point2);
                       repaint();
               }

               public void mouseClicked (MouseEvent event) {}
               public void mouseReleased (MouseEvent event) {}
               public void mouseEntered (MouseEvent event) {}
               public void mouseExited (MouseEvent event) {}
               public void mouseMoved (MouseEvent event) {}
       }
}

ibexpiotr is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On