import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.util.* ;
public class KanjiRoku extends JFrame{
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 roku[]={1,2,3,4} ;
int taker ;
int result = 0 ;
Boolean complete = false ;
KanjiRoku(){
//Create Panel for graphics
graphArea = new MyComponent() ;
graphArea.setBackground(Color.WHITE) ;
add(graphArea, BorderLayout.CENTER) ;
setVisible( true ) ;
//Adding listener
graphArea.addMouseListener(new MouseCatcher ()) ;
}
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) ;
// StrokeChecker() ;
taker++ ;
}
if(secondStroke.contains(xpos,ypos)){
two = Color.red ;
strokes.add(2) ;
// StrokeChecker() ;
taker++ ;
}
if(thirdStroke.contains(xpos,ypos)){
three = Color.red ;
strokes.add(3) ;
// StrokeChecker() ;
taker++ ;
}
if(fourthStroke.contains(xpos,ypos)){
four = Color.red ;
strokes.add(4) ;
// StrokeChecker() ;
taker++ ;
}
repaint() ;
if(taker == 4){
StrokeChecker() ;
}
}
}
public void StrokeChecker(){
int firstCom1 = strokes.get(0) ;
int firstCom2 = roku[0] ;
int secondCom1 = strokes.get(1) ;
int secondCom2 = roku[1] ;
int thirdCom1 = strokes.get(2) ;
int thirdCom2 = roku[2] ;
int fourthCom1 = strokes.get(3) ;
int fourthCom2 = roku[3] ;
if(firstCom1 == firstCom2){
result += 1 ;
}
if(secondCom1 == secondCom2){
result += 1 ;
}
if(thirdCom1 == thirdCom2){
result += 1 ;
}
if(fourthCom1 == fourthCom2){
result += 1 ;
}
switch(result) {
case 0:
JOptionPane.showMessageDialog( null, "Not even one right...", "Result", JOptionPane.PLAIN_MESSAGE ) ;
break ;
case 1:
JOptionPane.showMessageDialog( null, "You need more practice", "Result", JOptionPane.PLAIN_MESSAGE ) ;
break ;
case 2:
JOptionPane.showMessageDialog( null, "Good try", "Result", JOptionPane.PLAIN_MESSAGE ) ;
break ;
case 3:
JOptionPane.showMessageDialog( null, "Nearly! Try again!", "Result", JOptionPane.PLAIN_MESSAGE ) ;
break ;
case 4:
JOptionPane.showMessageDialog( null, "You did it!", "Result", JOptionPane.PLAIN_MESSAGE ) ;
break ;
}
}
}