?? sliding block.txt
字號:
//================================================================
// Sliding Block Program
//================================================================
//================================================================
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
//================================================================
// Sliding Applet
//================================================================
public class Slide extends Applet {
final int BUTTON_H = 35;
final int MSG_H = 35;
final int COPY_H = 30;
public Dimension app_size;
Board board;
AudioClip sound = null;
String filename;
String nextfile;
private
Label stepCounter;
Label doneMsg;
ControlButton nextButton;
ControlButton backButton;
ControlButton redoButton;
ControlButton replayButton;
final Color backcolor = new Color(180,180,180); // background - grey
final Color pagecolor = new Color(222,222,222);
// final Color pagecolor = Color.white; // for white background
final Color shadowLight = Color.white; // top, light shadow
final Color shadowDark = Color.black; // bottom, dark shadow
final Color piececolor = new Color(0.40f,0.40f,1.0f); // default piece color
final Color limitcolor = Color.green;
final Color hintcolor = Color.black; // goal line color
final Color msgcolor = Color.red;
//----------------------------------------------------------------
public void init() {
String crMessage = "Slide Puzzle V3.5 " +
"Copyright(c)1996-1999 Hirofumi Fujiwara, Nick Baxter";
System.out.println(crMessage);
System.out.println("Version 3.5.1 - Netscape 4.5 fix part 1");
sound = getAudioClip(getCodeBase(),"tick.au");
filename = getParameter( "problemfile" );
app_size = this.size();
Rectangle r = new Rectangle(0,BUTTON_H + MSG_H,
app_size.width,
app_size.height - 2*BUTTON_H - MSG_H - COPY_H);
board = new Board(this,r);
Font f10 = new java.awt.Font( "Helvetica", 0, 10 );
Font f14 = new java.awt.Font( "Helvetica", 0, 14 );
Font bf16 = new java.awt.Font( "Helvetica", 1, 16 );
Font f20 = new java.awt.Font( "Helvetica", 0, 20 );
setBackground( pagecolor );
setLayout( new BorderLayout() );
{
Panel p1 = new Panel();
add("North",p1);
p1.setLayout(new FlowLayout());
backButton = new ControlButton(this,"Back"," Back ");
backButton.setFont( f14 );
redoButton = new ControlButton(this,"Forward"," Forward ");
redoButton.setFont( f14 );
p1.add(backButton);
p1.add(redoButton);
stepCounter = new Label();
stepCounter.setFont( f20 );
showcounter();
p1.add( stepCounter );
replayButton = new ControlButton(this,"Replay"," Restart ");
replayButton.setFont( f14 );
p1.add(replayButton);
p1.reshape(0,0,app_size.width,BUTTON_H);
}
setLayout(new BorderLayout());
{
Panel p4 = new Panel();
add("North",p4);
p4.setLayout(new FlowLayout());
doneMsg = new Label();
doneMsg.setFont(bf16);
doneMsg.setForeground(msgcolor);
doneMsg.setAlignment(Label.CENTER);
doneMsg.setText(" "+
" ");
p4.add(doneMsg);
p4.reshape(0, BUTTON_H, app_size.width, MSG_H);
}
setLayout(new BorderLayout());
{
Panel p3 = new Panel();
add("South", p3 );
p3.setLayout( new FlowLayout());
nextButton = new ControlButton(this,"Next"," Go to next problem ");
nextButton.setFont(f14);
p3.add(nextButton);
p3.reshape( 0, app_size.height - BUTTON_H - COPY_H,
app_size.width, BUTTON_H);
}
setLayout(new BorderLayout());
{
Panel p2 = new Panel();
p2.setLayout( new FlowLayout());
add("South", p2 );
Label copyright = new Label( crMessage, Label.CENTER );
copyright.setFont( f10 );
p2.add( copyright );
p2.reshape( 0, app_size.height - COPY_H,
app_size.width, COPY_H);
}
replay();
}
//----------------------------------------------------------------
public void showcounter() {
if(board.minStep==0)
stepCounter.setText( " Step "+board.step+" " );
else stepCounter.setText( " Step "+board.step+"/"+
board.minStep+" " );
}
//----------------------------------------------------------------
public void showMsg(String msg) {
doneMsg.setText( msg );
}
//----------------------------------------------------------------
public void replay() {
board.setup(filename);
showcounter();
backButton.disable();
redoButton.disable();
if(nextfile == null) nextButton.disable();
else enable();
paint( getGraphics() );
}
//----------------------------------------------------------------
public void next() {
if(nextfile != null) filename = nextfile;
replay();
}
//----------------------------------------------------------------
public boolean mouseDown( Event evt, int x, int y ) {
board.mouseDown(x,y);
return true;
}
public boolean mouseDrag( Event evt, int x, int y ) {
board.mouseDrag(x,y);
return true;
}
public boolean mouseUp( Event evt, int x, int y ) {
board.mouseUp(x,y);
return true;
}
//----------------------------------------------------------------
public void paint( Graphics g ) {
// boolean flg = g.drawImage(backImage,0,CTRLPANEL_H*2,
// app_size.width,app_size.height-CTRLPANEL_H*2-COPY_H,
// shadowLight, null);
board.paintHint(g);
board.paint(g);
}
public void repaint() {
paint(getGraphics());
}
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// ControlButton
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
class ControlButton extends Button {
Slide slide;
String name;
ControlButton( Slide s, String str, String label ) {
super();
slide = s;
name = str;
setLabel( label );
}
//----------------------------------------------------------------
public boolean action( Event evt, Object obj ) {
if(name.equals("Replay")) slide.replay();
if(name.equals("Back")) slide.board.backHistory();
if(name.equals("Forward")) slide.board.forwardHistory();
if(name.equals("Next")) slide.next();
return true;
}
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// Board
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
class Board extends Rectangle {
final int maxPiece = 32;
final int maxHistory = 512;
public
Block blocks[][];
String targetBlocks[][];
char hintBlocks[][];
int ax,ay;
Slide slide;
int unit;
int minStep;
int step;
private
Rectangle req;
Piece pieces[];
int piecen;
Piece currentPiece;
Point dragPoint;
Point dPoint;
boolean dragFlag;
String boardType = "none";
boolean painting;
Piece pieceHistory[];
Point pointHistory[];
int historyCount;
int redoLimit;
int originalRedoLimit;
int originalX;
int originalY;
boolean adjusting;
//----------------------------------------------------------------
Board(Slide s,Rectangle r){
pieces = new Piece[maxPiece];
slide = s;
unit = 16;
x = y = 0;
req = r;
step = 0;
pieceHistory = new Piece[maxHistory];
pointHistory = new Point[maxHistory];
}
//----------------------------------------------------------------
public void setup(String filename){
minStep = step = 0;
targetBlocks = null;
hintBlocks = null;
currentPiece = null;
painting = false;
ax = ay = 0;
piecen = 0;
dragFlag = false;
loadData(filename);
if(ax==0 || ay==0){
ax = ay = 1;
System.out.println("File read error!");
}
int u1 = (int)(req.width*0.92)/ax;
int u2 = (int)(req.height*0.92)/ay;
unit = (u1<u2)? u1 : u2;
width = ax*unit;
height = ay*unit;
x = (req.width-width)/2+req.x;
y = (req.height-height)/2+req.y;
setBlockShape();
dragPoint = new Point(0,0);
dPoint = new Point(0,0);
historyCount = redoLimit = 0;
originalRedoLimit = originalX = originalY = 0;
adjusting= false;
slide.showMsg(" "+
" ");
}
//----------------------------------------------------------------
public void mouseDown(int v, int w){
if(dragFlag) return;
if(!this.inside(v,w)) return;
v-=x; w-=y;
painting = true;
dragPoint.move(v,w);
v/=unit; w/=unit;
if(blocks[v][w]!=null && blocks[v][w].outside)
return;
if(blocks[v][w]!=null){
currentPiece = blocks[v][w].piece;
currentPiece.resetAdjust();
dragFlag = true;
}
dPoint.move(0,0);
painting = false;
}
//----------------------------------------------------------------
public void mouseDrag(int v, int w){
int radius = 5; // radius of pieces corner
if(!dragFlag || painting) return;
painting = true;
v-=x; w-=y;
dragFlag = false;
int dx = v - dragPoint.x;
int dy = w - dragPoint.y;
boolean f = false;
int dx2 = currentPiece.moveX(dPoint.x,dPoint.y,dx);
// if (dx!=0 && dx2==0) {
// for (int d=-radius;d<=radius;d++) {
// dx2 = currentPiece.moveX(dPoint.x,dPoint.y-d,dx);
// if (dx2 != 0) {
// System.out.println("ADJUST-y: "+ -d);
// dPoint.y-=d;
// dy+=d;
//
// int ddx;
// if(dx>0) ddx=2*radius-dx; else ddx=dx-2*radius;
// dPoint.x-=ddx;
// dx+=ddx;
// System.out.println("BOOST-x: "+dx);
// dx2 = currentPiece.moveX(dPoint.x,dPoint.y,dx);
// break;
// }
// }
// }
dPoint.x += dx2;
if(dx2!=dx && dx2!=0 &&
currentPiece.moveX(dPoint.x,dPoint.y,dx)==0) {
f = true;
}
int dy2 = currentPiece.moveY(dPoint.x,dPoint.y,dy);
// if (dy!=0 && dy2==0) {
// for (int d=-radius;d<=radius;d++) {
// dy2 = currentPiece.moveY(dPoint.x-d,dPoint.y,dy);
// if (dy2 != 0) {
// System.out.println("ADJUST-x: "+ -d);
// dPoint.x-=d;
// dx+=d;
//
// int ddy;
// if(dy>0) ddy=2*radius-dy; else ddy=dy-2*radius;
// dPoint.y-=ddy;
// dy+=ddy;
// System.out.println("BOOST-y: "+dy);
// dy2 = currentPiece.moveY(dPoint.x,dPoint.y,dy);
// break;
// }
// }
// }
dPoint.y += dy2;
if(dy2!=dx && dy2!=0 &&
currentPiece.moveY(dPoint.x,dPoint.y,dy)==0){
f = true;
}
dragPoint.move(v,w);
currentPiece.update(slide.getGraphics(),dPoint.x,dPoint.y);
if(f) slide.sound.play();
dragFlag = true;
painting = false;
}
//----------------------------------------------------------------
public void mouseUp(int v, int w){
v-=x; w-=y;
if(!dragFlag) return;
dragFlag = false;
int offx = (dPoint.x+unit/2+unit*1024)/unit-1024;
int offy = (dPoint.y+unit/2+unit*1024)/unit-1024;
// see if this move is really same as Back
if (historyCount>0)
if (pieceHistory[historyCount-1].cid==currentPiece.cid &&
pointHistory[historyCount-1].x == -offx &&
pointHistory[historyCount-1].y == -offy) {
backHistory();
return;
}
// see if this move is really same as Forward
if (historyCount<redoLimit)
if (pieceHistory[historyCount].cid==currentPiece.cid)
if (pointHistory[historyCount].x == offx &&
pointHistory[historyCount].y == offy) {
forwardHistory();
return;
}
currentPiece.update(slide.getGraphics(),offx*unit,offy*unit);
currentPiece.move(offx,offy);
// check to see if this is same piece as previous move
// know it can't be a "back", but maybe a "forward"
if ( (historyCount>0) &&
(pieceHistory[historyCount-1].cid==currentPiece.cid) ) {
int offxx = pointHistory[historyCount-1].x + offx;
int offyy = pointHistory[historyCount-1].y + offy;
System.out.println("Replace " + String.valueOf(step) + ": " +
currentPiece.cid + ", "+String.valueOf(offxx)+","+
String.valueOf(offyy));
// check to see if Forward should be enabled or disabled
if ((originalRedoLimit>0) && (offxx==originalX) &&
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -