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):
Code Java:
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) ;
}
}
Re: No errors but just won't print out the first element of an array list
Here's your problem:
Code java:
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.
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 :)