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 3 of 3

Thread: No errors but just won't print out the first element of an array list

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default No errors but just won't print out the first element of an array list

    Hi, in my program the user clicks on certain polygons that make up the shape of a Japanese kanji character. Each polygon has a number and when clicked that number goes to an array list (which I'm not even sure is happening btw). I now need to get the number that is in the 0 position of the array list (for something I plan to do afterwards with), but I'm trying to check if I'm getting anything at all by printing out whatever comes into the list (still at position 0 I believe), this means theoretically that every time the user would click on a polygon the polygon's number would print out, however it seems I'm doing something wrong, there are no errors but nothing comes out.

    If anyone can see the flaws in my program or can offer me advice on doing what I want a different way then please tell me. Thank you.

    Here is my code (minus main):

    import javax.swing.* ;
    import java.awt.* ;
    import java.awt.event.* ;
    import java.util.* ;
     
    public class KanjiInterface extends JFrame {
     
    	JMenuBar menuBar ;
    	JMenu menuSession ;
    	JMenuItem subNewGame, subLoadGame ;
    	MyComponent graphArea ;
    	Color one = Color.blue ;
    	Color two = Color.blue ;
    	Color three = Color.blue ;
    	Color four = Color.blue ;
    	int firstx[]={275,325,325,275} ;
    	int firsty[]={100,100,175,175} ;
    	int secondx[]={150,450,450,150} ;
    	int secondy[]={180,180,230,230} ;
    	int thirdx[]={255,292,180,143} ;
    	int thirdy[]={263,300,412,375} ;
    	int fourthx[]={345,308,420,457} ;
    	int fourthy[]={263,300,412,375} ;
    	Polygon firstStroke = new Polygon(firstx,firsty,4) ;
    	Polygon secondStroke = new Polygon(secondx,secondy,4) ;
    	Polygon thirdStroke = new Polygon(thirdx,thirdy,4) ;
    	Polygon fourthStroke = new Polygon(fourthx,fourthy,4) ;
    	ArrayList<Integer> strokes = new ArrayList<Integer>() ;
    	int totalElements = strokes.size() ;
     
     
    	KanjiInterface(){
    		//Creates the window
    		super("Kanji Game") ;
    		setLocation( new Point(100, 100) ) ;
    		//this.getContentPane().setBackground( Color.white ) ;
    		setSize( 600, 600 ) ;
    		setResizable( true ) ;
     
    		//Creates the Menu Bar along with sub-menus
    		menuBar = new JMenuBar() ;
    		menuSession = new JMenu("Session") ;
    		subNewGame = new JMenuItem("New Session") ;
    		subLoadGame = new JMenuItem("Load Session") ;
    		menuBar.add(menuSession) ;
    		menuSession.add( subNewGame ) ;
    		menuSession.add( subLoadGame ) ;
    		this.setJMenuBar(menuBar) ;
     
    		//Create Panel for graphics
    		graphArea = new MyComponent() ;
    		graphArea.setBackground(Color.WHITE) ;
    	 	add(graphArea, BorderLayout.CENTER) ;
    	 	setVisible( true ) ;
     
    	 	//Adding listener
    	 	graphArea.addMouseListener(new MouseCatcher ()) ;
     
    	 	for(int index = 0 ; index < totalElements ; index++){
    	 	StrokeChecker() ;
    	 	}
     
     
    	}
     
    	class MyComponent extends JPanel{
    	public void paintComponent(Graphics g){
    		super.paintComponent(g) ;
    	    g.setColor(one) ;
    	    g.fillPolygon(firstStroke) ;
    	    g.setColor(two) ;
    	    g.fillPolygon(secondStroke) ;	
    	    g.setColor(three) ;
    	    g.fillPolygon(thirdStroke) ;	
    	    g.setColor(four) ;
    	    g.fillPolygon(fourthStroke) ;	
     
    	}
    	}
     
    	public class MouseCatcher extends MouseAdapter {
     
    		public void mouseClicked(MouseEvent e)
    		{
     
        		int xpos = e.getX();
    			int ypos = e.getY();
    			if(firstStroke.contains(xpos,ypos)){
    			    one = Color.red ;
    			    strokes.add(1) ;
    			}
    			if(secondStroke.contains(xpos,ypos)){
    			    two = Color.red ;
    			    strokes.add(2) ;
    			}
    			if(thirdStroke.contains(xpos,ypos)){
    			    three = Color.red ;
    			    strokes.add(3) ;
    			}
    			if(fourthStroke.contains(xpos,ypos)){
    			    four = Color.red ;
    			    strokes.add(4) ;
    			}
    		    repaint() ;
     
    		}
     
    	}
     
    	public void StrokeChecker(){
    		int x = strokes.get(0) ;
    		System.out.println("The first stroke is " + x) ;
     
     
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: No errors but just won't print out the first element of an array list

    Here's your problem:
    ArrayList<Integer> strokes = new ArrayList<Integer>() ;
    int totalElements = strokes.size() ;
    //.....
    for(int index = 0 ; index < totalElements ; index++){
        StrokeChecker() ;
    }
    First problem: When the ArrayList is first created its size is 0 and you assign that to totalElements. The value of this variable will never change. If the size of the ArrayList increases it does not automagically update your variable.

    Second problem: you have the loop inside the constructor therefore it will only ever run once. It will not run everytime a component is clicked.

  3. The Following User Says Thank You to Junky For This Useful Post:

    DudeJericho (April 20th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    15
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: No errors but just won't print out the first element of an array list

    I see! Thank you! I will try to fix that

Similar Threads

  1. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 07:52 AM
  2. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 01:50 AM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 08:04 AM