⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cross sums.txt

📁 5个用java编写的小游戏源码
💻 TXT
📖 第 1 页 / 共 2 页
字号:

	void  changeValue( int v, boolean update ) {
		if( curX<0 && curY<0 )
			return;
		Cell c = table[curX][curY];
		int	prevvalue = c.value;
		boolean	prevhint  = (c.color != Color.black);

		c.setValue( v, ishint );
		db.history.set( curX, curY, prevhint, prevvalue, ishint, v );

		canvas.paintCell( curX, curY, true ); 

		if( update )
			canvas.paint( canvas.getGraphics() );
	}
}


class  CrosssumCanvas extends Canvas {
	PlayPanel	panel;
	CrossSum	app;
	Image	fullCanvas = null;
	Graphics        fg = null;
	Font		sumfont;
	Font		numfont;


	CrosssumCanvas( PlayPanel p ) {
		panel = p;
		app   = panel.app;
	}


	void resizeCanvas() {
		panel.fw = panel.bdwidth  * panel.bdunit + 1;
		panel.fh = panel.bdheight * panel.bdunit + 1;
		fullCanvas = app.createImage( panel.fw, panel.fh );
		fg = fullCanvas.getGraphics();
		paintFull();
		resize( panel.fw, panel.fh );
	}



	boolean isMark( int x, int y ) {
		if( x<0 || panel.bdwidth<=x || y<0 || panel.bdheight<=y ) {
			return false;
		}
		return panel.table[x][y].mark;
	}


	void paintCell( int x, int y, boolean act ) {
		int	unit = panel.bdunit;
		Cell	c = panel.table[x][y];

		if( c.mark ) {
			fg.setColor( Color.black );
			fg.fillRect( unit*x+1, unit*y+1, unit-1, unit-1 );
			drawSumNumber( fg, x, y, c );
			fg.setColor( Color.white );
			fg.drawLine( unit*x+1, unit*y+1, unit*(x+1), unit*(y+1) );
			if( isMark(x-1,y) ) {
				fg.drawLine( unit*x, unit*y, unit*x, unit*(y+1) );
			}
			if( isMark(x,y-1) ) {
				fg.drawLine( unit*x, unit*y, unit*(x+1), unit*y );
			}
		} else {
			fg.setColor( act ? Color.cyan : Color.white );
			fg.fillRect( unit*x+1, unit*y+1, unit-1, unit-1 );
			drawNumber( fg, x, y, c );
		}
	}


	void drawNumber( Graphics g, int x, int y, Cell c ) {
		int v = c.value;
		if( v==0 )
			return;

		int	unit = panel.bdunit;

		if( numfont == (Font)null ) {
			int fs = (app.fontsize!=0) ? app.fontsize : unit*9/10;
			numfont = new Font( "Helvetica", 0, fs );
		}

		Color col = c.color;
		if( c.error )
			col = Color.red;
		g.setColor( col );

		g.setFont( numfont );
		FontMetrics	fm = g.getFontMetrics();;
		int	sHeight = fm.getHeight();
		int	sAscent = fm. getAscent();

		String s = String.valueOf(v);
		int	sWidth = fm.stringWidth(s);
		int	xx = x*unit + (unit - sWidth)/2;
		int	yy = y*unit + sAscent+ (unit - sAscent)/2;
		g.drawString( s, xx, yy );
	}


	void drawSumNumber( Graphics g, int x, int y, Cell c ) {
		int v = c.vsum;
		int h = c.hsum;
		if( v==0 && h==0 )
			return;

		int	unit = panel.bdunit;

		if( sumfont == (Font)null ) {
			sumfont = new Font( "Helvetica", 0, unit/2 );
		}

		g.setFont( sumfont );

		FontMetrics fm = g.getFontMetrics();
		int	sHeight = fm.getHeight();
		int	sAscent = fm. getAscent();

		if( v != 0 ) {
			if( c.verror ) {
				Polygon p = new Polygon();
				p.addPoint( x*unit+1,   y*unit+1     );
				p.addPoint( x*unit+1,   (y+1)*unit );
				p.addPoint( (x+1)*unit, (y+1)*unit );
				g.setColor( Color.red );
				g.fillPolygon( p );
			}
			g.setColor( Color.white );
			String s = String.valueOf(v);
			int	sWidth = fm.stringWidth(s);
			int	xx = x*unit + (unit/2 - sWidth)/2 + 2;
			int	yy = (y+1)*unit - unit/20 - 1;
			g.drawString( s, xx, yy );
		}
		if( h != 0 ) {
			if( c.herror ) {
				Polygon p = new Polygon();
				p.addPoint( x*unit+1,   y*unit+1   );
				p.addPoint( (x+1)*unit, y*unit+1   );
				p.addPoint( (x+1)*unit, (y+1)*unit );
				g.setColor( Color.red );
				g.fillPolygon( p );
			}
			g.setColor( Color.white );
			String s = String.valueOf(h);
			int	sWidth = fm.stringWidth(s);
			int	xx = (x+1)*unit - (unit/2 + sWidth)/2 - 2;
			int	yy = y*unit + sAscent + unit/20 ;
			g.drawString( s, xx, yy );
		}
	}


	void paintFull() {
		int	unit = panel.bdunit;
		fg.setColor( Color.white );
		fg.fillRect( 0, 0, size().width, size().height );

		fg.setColor( Color.black );
		for( int i=0; i<=panel.bdwidth; ++i ) {
			fg.drawLine( i*unit, 0, i*unit, panel.fh );
		}
		for( int i=0; i<=panel.bdheight; ++i ) {
			fg.drawLine( 0, i*unit, panel.fw, i*unit );
		}

		for( int x=0; x<panel.bdwidth; ++x ) {
			for( int y=0; y<panel.bdheight; ++y ) {
				boolean isact = (x==panel.curX) && (y==panel.curY);
				paintCell( x, y,isact );
			}
		}
	}

	public void  paint( Graphics g ) {
		if( fullCanvas == null ) {
			paintFull();
		}
		g.drawImage( fullCanvas, 0, 0, panel.app );
	}


	public boolean mouseDown( Event evt, int x, int y ) {
		int unit = panel.bdunit;
		int cx = x/unit;
		int cy = y/unit;

		if( cx<0 || panel.bdwidth<=cx|| cy<0 || panel.bdheight<=cy )
			return true;

		int oldX = panel.curX;
		int oldY = panel.curY;

		panel.curX = cx;
		panel.curY = cy;

		if( oldX>=0 && oldY>=0 ) {
			paintCell( oldX, oldY, false );
		}

		if( panel.curX>=0 && panel.curY>=0 ) {
			paintCell( panel.curX, panel.curY, true );
		}

		paint( getGraphics() );

		return true;
	}


	void moveCurXY( int dx, int dy ) {
		int oldX = panel.curX;
		int oldY = panel.curY;

		int cx = panel.curX+dx;
		int cy = panel.curY+dy;
		if( cx < 1 )
			cx = 1;
		if( cx >= panel.bdwidth )
			cx = panel.bdwidth - 1;
		if( cy < 1 )			
			cy = 1;
		if( cy >= panel.bdheight )
			cy = panel.bdheight - 1;
		panel.curX = cx;
		panel.curY = cy;

		if( oldX>=0 && oldY>=0 ) {
			paintCell( oldX, oldY, false );
		}

		if( panel.curX>=0 && panel.curY>=0 ) {
			paintCell( panel.curX, panel.curY, true );
		}

		paint( getGraphics() );
	}
}


class Cell {
	boolean	mark;
	byte	vsum, hsum;
	byte	value;
	boolean verror;
	boolean herror;
	boolean error;
	Color	color;


	Cell() {
		mark = false;
		vsum = hsum = value = 0;
		verror = herror = error = false;
		color = Color.black;
	}


	void setMark( int v, int h ) {
		mark = true;
		vsum = (byte)v;
		hsum = (byte)h;
	}


	void setValue( int v, boolean ishint ) {
		value = (byte)v;
		error = false;
		color = ishint ? Color.pink : Color.black;
	}


	void clear() {
		value = 0;
		verror = herror = error = false;
	}


	String convtoString() {
		String	str = "mk";
		if( ! mark ) {
			str  = (color==Color.pink) ? "t" : "f";
			str += value;
		}
		return	str;
	}

	void setfromString( String str ) {
		if( "mk".equals( str ) ) {
			return;
		}
		int	v = 0;
		try {
			v = (byte)Integer.valueOf(str.substring(1)).intValue();
		} catch( NoSuchElementException e ) {}
		boolean	h = "t".equals( str.substring(0,1) );
		setValue( v, h );
	}
}


class  ControlButton extends Button {
	CrossSum	app;
	String		name;


	ControlButton( CrossSum p, String str ) {
		super();
		app = p;
		name = str;
		setLabel( str );
		setFont( app.boldfont );
	}

	public boolean action( Event evt, Object obj ) {
		if( name.equals("Clear") ) {
			app.playPanel.clear();
			return true;
		}
		if( name.equals("Check") ) {
			app.playPanel.check();
			return true;
		}
		return true;
	}
}


class	DatabaseIF {
	Frame		parentFrame;
	CrossSum	applet;
	DatabaseAccess	db = null;
	URL		docbase;
	
	DatabaseIF( CrossSum app, Frame parent ) {
		applet = app;
		parentFrame = parent;
		docbase = applet.getCodeBase();
		applet.db = db= new DatabaseAccess( this, parentFrame );
	}


	private	void stepHistory( String step, boolean isforward ) { 
		if( step == null )
			return;

		StringTokenizer tk = new StringTokenizer( step );
		int	n = tk.countTokens();
		if( n < 5 )
			return;

		int x = Integer.parseInt( String.valueOf(tk.nextToken()) );
		int y = Integer.parseInt( String.valueOf(tk.nextToken()) );
		String ss = tk.nextToken();
		String es = tk.nextToken();
		int t = Integer.parseInt( String.valueOf(tk.nextToken()) );
		String str = isforward ? es : ss;

		int	v = 0;
		boolean	h = false;
		try {
			h = str.charAt(0) == 't';
			v = Integer.parseInt( String.valueOf( str.substring(1) ) );
		} catch( StringIndexOutOfBoundsException e ) {}

		Cell oc = applet.playPanel.cell();
		if( oc != null ) {
			applet.playPanel.canvas.paintCell( applet.playPanel.curX,
							   applet.playPanel.curY, false );
		}

		applet.playPanel.curX = x;
		applet.playPanel.curY = y;
		Cell c = applet.playPanel.table[x][y];
		c.setValue( v, h );
		applet.playPanel.canvas.paintCell( x, y, true );
		applet.playPanel.canvas.paint( applet.playPanel.canvas.getGraphics() );
	}


	void clearBoard() {
		applet.playPanel.curX = -1;
		applet.playPanel.curY = -1;
		applet.playPanel.clearview();
	}


	void forwardHistory( String s ) {
		stepHistory( s, true );
	}


	void backHistory( String s ) {
		stepHistory( s, false );
	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -