import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
Main()
{
setTitle("체크박스 만들기 에제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
ImageIcon cherryIcon=new ImageIcon("src/Cherry.jpg");
ImageIcon selectedcherryIcon=new ImageIcon("src/selectedCherry.jpg");
JCheckBox apple=new JCheckBox("사과");
JCheckBox pear=new JCheckBox("배",true);
JCheckBox cherry=new JCheckBox("체리",cherryIcon);
cherry.setBorderPainted(true);
cherry.setSelectedIcon(selectedcherryIcon);
c.add(apple);
c.add(pear);
c.add(cherry);
setSize(250,150);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Main extends JFrame{
int[][] ox=new int[3][3];
JCheckBox [] L=new JCheckBox[9];
int i=0;
int win=0;
int[] g=new int[9];
int[] g1=new int[9];
ImageIcon o=new ImageIcon("src/o3.PNG");
ImageIcon x=new ImageIcon("src/x.PNG");
Main(){
setTitle("삼목");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
GridLayout grid=new GridLayout(3,3);
grid.setVgap(5);
c.setLayout(grid);
ImageIcon line=new ImageIcon("src/Line.PNG");
MyItemListener listener=new MyItemListener();
for(int f=0;f<9;f++)
{
L[f]=new JCheckBox(line);
L[f].setBorderPainted(true);
c.add(L[f]);
L[f].addItemListener((ItemListener) listener);
}
setSize(300,300);
setVisible(true);
if(win==1)
{
System.exit(0);
}
}
class MyItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e) {
for(int f=0;f<9;f++)
{
if(L[f]==e.getItem())
{
if(i%2==0)
{
ox[f/3][f%3]=1;
L[f].setSelectedIcon(o);
L[f].setEnabled(false);
i++;
if(ox[f/3][f%3+1]==1&&ox[f/3][f%3+2]==1)
{
win=1;
}
if(ox[f/3][f%3-1]==1&&ox[f/3][f%3-2]==1)
{
win=1;
}
if(ox[f/3+1][f%3]==1&&ox[f/3+2][f%3]==1)
{
win=1;
}
if(ox[f/3-1][f%3]==1&&ox[f/3-2][f%3]==1)
{
win=1;
}
if(ox[f/3+1][f%3+1]==1&&ox[f/3+2][f%3+2]==1)
{
win=1;
}
if(ox[f/3+1][f%3-1]==1&&ox[f/3+2][f%3-2]==1)
{
win=1;
}
if(ox[f/3-1][f%3+1]==1&&ox[f/3-2][f%3+2]==1)
{
win=1;
}
if(ox[f/3-1][f%3-1]==1&&ox[f/3-2][f%3-2]==1)
{
win=1;
}
}
else if(i%2==1)
{
ox[f/3][f%3]=2;
L[f].setSelectedIcon(x);
L[f].setEnabled(false);
i++;
if(ox[f/3][f%3+1]==2&&ox[f/3][f%3+2]==2)
{
win=1;
}
if(ox[f/3][f%3-1]==2&&ox[f/3][f%3-2]==2)
{
win=1;
}
if(ox[f/3+1][f%3]==2&&ox[f/3+2][f%3]==2)
{
win=1;
}
if(ox[f/3-1][f%3]==2&&ox[f/3-2][f%3]==2)
{
win=1;
}
if(ox[f/3+1][f%3+1]==2&&ox[f/3+2][f%3+2]==2)
{
win=1;
}
if(ox[f/3+1][f%3-1]==2&&ox[f/3+2][f%3-2]==2)
{
win=1;
}
if(ox[f/3-1][f%3+1]==2&&ox[f/3-2][f%3+2]==2)
{
win=1;
}
if(ox[f/3-1][f%3-1]==2&&ox[f/3-2][f%3-2]==2)
{
win=1;
}
}
}
}
}
}
public static void main(String[] args) {
new Main();
}
}