<< Line of coin I >> import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class l_coin extends Applet implements Runnable{ Thread th = null; Image os; Graphics og; int r = 15,h = 26; int td[][] = {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,1,0,0,0,0}, {0,0,0,1,0,1,0,0,0,0}, {0,0,0,0,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}; int px,py,sx,sy,rx,ry,mx,my; int fg = 0 , rs; public void init(){ setBackground(new Color(200,250,250)); os = createImage(320,308); og = os.getGraphics(); addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ py = e.getY() / h; px = (e.getX() - (py%2)*r) / (2*r); if(td[py][px] == 1){ sx = px ; sy = py; td[py][px]=0; mx = e.getX() ; my = e.getY(); fg = 1; } repaint(); } public void mouseReleased(MouseEvent e){ ry = e.getY() / h; rx = (e.getX() - (ry%2)*r) / (2*r); if((rx > 0) && (rx < 9) && (ry > 0) && (ry < 10) && (fg == 1)){ rs = td[ry-1][rx-1+(ry%2)] + td[ry-1][rx+(ry%2)]+td[ry][rx-1]+td[ry][rx+1]+td[ry+1][rx-1+(ry%2)] + td[ry+1][rx+(ry%2)]; if((rs > 1) && (td[ry][rx] == 0)){ td[ry][rx] = 1; }else{ td[sy][sx] = 1; } }else if(fg == 1){ td[sy][sx] = 1; } fg = 0; repaint(); } } ); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ mx = e.getX(); my = e.getY(); repaint(); } } ); } public void paint(Graphics g){ og.setColor(new Color(200,250,250)); og.fillRect(0,0,320,308); for(int i = 0 ; i < 9 ; i++){ for(int j = 0 ; j < 10 ; j++){ if(i*j != 0){ og.setColor(Color.gray); og.fillOval(i*2*r+(j%2)*r+15,j*h+15,3,3); } if(td[j][i] == 1){ coin(i*2*r+(j%2)*r,j*h); } } } if(fg == 1){ coin(mx - 15,my - 13); } g.drawImage(os,0,0,this ); } public void coin(int x,int y){ og.setColor(new Color(255,153,0)); og.fillOval(x,y,2*r,2*r); og.setColor(new Color(50,50,50)); og.drawOval(x,y,2*r,2*r); for(int c = 0 ; c < 15 ; c ++){ og.setColor(new Color(c*10+100,c*10+100,c*10+100)); og.drawOval(x+5+c,y+5+c,2*r-10-2*c,2*r-10-2*c); } return; } public void start(){ if(th == null){ th = new Thread(this); th.start(); } } public void stop(){ th = null; } public void run(){ while (th != null){ try { Thread.sleep(30); } catch (InterruptedException e){ } } } public void update( Graphics g ){ paint( g ) ; } } << Line of coin II >> import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class l_coin2 extends Applet implements Runnable{ Thread th = null; Image os; Graphics og; int r = 15,h = 30; int td[][] = {{0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,1,2,3,4,5,6,7,8,9,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}}; int dx[] = {1,2,3,4,5,6,7,8,9}; int dy[] = {8,8,8,8,8,8,8,8,8}; int px,py,sx,sy,rx,ry,mx,my; int fg = 0 , rs , ss ,ll; public void init(){ setBackground(new Color(200,250,250)); os = createImage(360,360); og = os.getGraphics(); addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ py = e.getY() / h; px = e.getX() / h; if(td[py][px] != 0){ rs = td[py][px]; sx = px ; sy = py; td[py][px]=0; mx = e.getX() ; my = e.getY(); fg = 1; } repaint(); } public void mouseReleased(MouseEvent e){ ry = e.getY() / h; rx = e.getX() / h; if((rx > 0) && (rx < 11) && (ry > 0) && (ry < 11) && (fg == 1)){ if(td[ry][rx] == 0){ td[ry][rx] = rs; dx[rs-1] = rx ; dy[rs-1] = ry ; }else{ td[sy][sx] = rs; dx[rs-1] = sx ; dy[rs-1] = sy ; } fg = 0; } fg = 0; repaint(); } } ); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ mx = e.getX(); my = e.getY(); repaint(); } } ); } public void paint(Graphics g){ og.setColor(new Color(200,250,250)); og.fillRect(0,0,360,360); for(int i = 0 ; i < 11 ; i++){ for(int j = 0 ; j < 11 ; j++){ if(i*j != 0){ og.setColor(Color.gray); og.fillOval(i*h+15,j*h+15,3,3); } if(td[j][i] != 0){ coin(i*h,j*h); } } } if(fg == 1){ coin(mx - 15,my - 13); } ll = 0; for(int c1 = 0;c1 < 9;c1 ++){ for(int c2 = 0;c2 < 9;c2 ++){ ss = 0; if(c1 != c2){ for(int c3 = 0;c3 < 9;c3 ++){ if((c1 != c2) && (c2 != c3) && (c3 != c1)){ if((dy[c1]-dy[c2])*(dx[c1]-dx[c3])-(dx[c1]-dx[c2])*(dy[c1]-dy[c3]) == 0){ ss ++; } } } if(ss == 1){ og.setColor(Color.blue); og.drawLine(dx[c1]*h+15,dy[c1]*h+15,dx[c2]*h+15,dy[c2]*h+15); ll ++; } } } } if(ll/6 == 10){ og.setColor(Color.blue); }else{ og.setColor(Color.red); } og.setFont(new Font("Century",Font.PLAIN,20)); og.drawString(""+ll/6+" lines",150,25); g.drawImage(os,0,0,this ); } public void coin(int x,int y){ og.setColor(new Color(255,153,0)); og.fillOval(x,y,2*r,2*r); og.setColor(new Color(50,50,50)); og.drawOval(x,y,2*r,2*r); for(int c = 0 ; c < 15 ; c ++){ og.setColor(new Color(c*10+100,c*10+100,c*10+100)); og.drawOval(x+5+c,y+5+c,2*r-10-2*c,2*r-10-2*c); } return; } public void start(){ if(th == null){ th = new Thread(this); th.start(); } } public void stop(){ th = null; } public void run(){ while (th != null){ try { Thread.sleep(30); } catch (InterruptedException e){ } } } public void update( Graphics g ){ paint( g ) ; } }