KnotFunny
Java Source:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class knotfunny extends Applet implements Runnable {
Thread t;
Image bi;
Graphics bg;
boolean sa;
Random r;
final Color whi=Color.white;
final Color bla=Color.black;
final Color red=Color.red;
final Color gra=Color.lightGray;
int i,j,x,y;
int ei,ej,ex,ey;
int mod;
int[][] kfpp;
int vo;
int btx,bty,bbx,bby;
int cx,cy,dkf;
public void init() {
t=new Thread(this);
bi=createImage(245,180);
bg=bi.getGraphics();
r=new Random();
kfpp=new int[4][4];
for (i=0;i<4;i++)
for (j=0;j<4;j++)
kfpp[i][j]=i+j*4;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
mod=e.getModifiers();
ex=e.getX();
ey=e.getY();
if (ex>180 && ex<200 && ey>30 && ey<50) {moveClockwise();return;}
if (ex>210 && ex<230 && ey>30 && ey<50) {moveAntiClockwise();return;}
if (ex>180 && ex<230 && ey>80 && ey<100) {scramble();return;}
if (ex>180 && ex<230 && ey>120 && ey<140) {resetBoard();return;}
for (ei=0;ei<4;ei++)
for (ej=0;ej<4;ej++) {
if (ex>(20+ei*40) && ex<(40+ei*40) && ey>(20+ej*40) && ey<(40+ej*40)) {
if (mod==16)
if (ei<bbx && ej<bby) {
btx=ei;
bty=ej;
}
if (mod==4)
if (ei>btx && ej>bty) {
bbx=ei;
bby=ej;
}
break;
}
}//end ej
}//mousePressed
});
btx=0;
bty=0;
bbx=3;
bby=3;
}
public void moveAntiClockwise() {
dkf=kfpp[btx][bty];
cx=btx;
cy=bty;
while (cx++<bbx) kfpp[cx-1][cy]=kfpp[cx][cy];
cx--;
while (cy++<bby) kfpp[cx][cy-1]=kfpp[cx][cy];
cy--;
while (cx-->btx) kfpp[cx+1][cy]=kfpp[cx][cy];
cx++;
while (cy-->bty+1) kfpp[cx][cy+1]=kfpp[cx][cy];
kfpp[cx][cy+1]=dkf;
}
public void moveClockwise() {
dkf=kfpp[btx][bty];
cx=btx;
cy=bty;
while (cy++<bby) kfpp[cx][cy-1]=kfpp[cx][cy];
cy--;
while (cx++<bbx) kfpp[cx-1][cy]=kfpp[cx][cy];
cx--;
while (cy-->bty) kfpp[cx][cy+1]=kfpp[cx][cy];
cy++;
while (cx-->btx+1) kfpp[cx+1][cy]=kfpp[cx][cy];
kfpp[cx+1][cy]=dkf;
}
public void scramble() {
for (i=0;i<500;i++) {
btx=Math.abs(r.nextInt()%3);
bty=Math.abs(r.nextInt()%3);
bbx=Math.abs(r.nextInt()%3)+1;
bby=Math.abs(r.nextInt()%3)+1;
while (btx>=bbx || bty>=bby) {
btx=Math.abs(r.nextInt()%3);
bty=Math.abs(r.nextInt()%3);
bbx=Math.abs(r.nextInt()%3)+1;
bby=Math.abs(r.nextInt()%3)+1;
}
if (r.nextInt()%2==0) moveClockwise(); else moveAntiClockwise();
}
btx=Math.abs(r.nextInt()%3);
bty=Math.abs(r.nextInt()%3);
bbx=Math.abs(r.nextInt()%3)+1;
bby=Math.abs(r.nextInt()%3)+1;
while (btx>=bbx || bty>=bby) {
btx=Math.abs(r.nextInt()%3);
bty=Math.abs(r.nextInt()%3);
bbx=Math.abs(r.nextInt()%3)+1;
bby=Math.abs(r.nextInt()%3)+1;
}
}
public void resetBoard() {
for (i=0;i<4;i++)
for (j=0;j<4;j++)
kfpp[i][j]=i+j*4;
}
public void start() {
sa=true;
t.start();
}
public void stop() {
sa=false;
t.stop();
}
public void run() {
while (sa) {
repaint();
try {Thread.sleep(30);} catch (Exception e) {}
}
}
public void update(Graphics g) {
bg.setColor(bla);
bg.fillRect(0,0,245,180);
bg.setColor(whi);
for (x=0;x<4;x++)
for (y=0;y<4;y++) {
if (kfpp[x][y]>9) vo=-3; else vo=0;
bg.drawRect(20+x*40,20+y*40,20,20);
bg.drawString(""+kfpp[x][y],27+vo+x*40,35+y*40);
}
bg.setColor(red);
bg.drawRect(15+btx*40,15+bty*40,30+(bbx-btx)*40,30+(bby-bty)*40);
bg.setColor(gra);
bg.fillRect(180,30,20,20);
bg.fillRect(210,30,20,20);
bg.fillRect(180,80,50,20);
bg.fillRect(180,120,50,20);
bg.setColor(bla);
bg.drawString("-->",185,45);
bg.drawString("<--",215,45);
bg.drawString("Scramble",183,95);
bg.drawString("Reset",190,135);
g.drawImage(bi,0,0,this);
}
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|