📄 lines.java
字号:
import java.applet.Applet;import java.awt.Button;import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.TextArea;import java.awt.event.MouseEvent;import java.awt.image.MemoryImageSource;import java.net.URL;import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.text.SimpleDateFormat;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;public class Lines extends Applet implements Runnable{ //资源信息 private ResourceBundle bundle = null; private boolean Code; private boolean getName; private Thread getX; private boolean contains; //程序结束标志true结束false运行,主循环标志变量 private Image Z; private Graphics drawImage; private Image getGreen; private Image setCursor; private Image clipRect[]; //新游戏按钮图形 private Image heroButton[]; //排行榜按钮图形 private Image getGraphics[]; //撤消按钮图形 private Image Serif; //记分牌图形 private Rectangle sqrt; //新游戏按钮所在矩形区域 private Rectangle hero; //排行榜按钮所在矩形区域 private Rectangle getFont; //撤消按钮所在矩形区域 private Rectangle getSize; //记分牌所在矩形区域 private int getFontMetrics; private int setFont; private int getHeight; private int black; private int setColor; private int width; //画布宽 private int height; //画布高 private int gameBestScore; //游戏最高分数 private int playerBestScore; //玩家最高分数 private int Score; //当前游戏分数 private String[] heroTableHead = {"名次","分数","玩家姓名","创建日期"}; private String heroTable[][]; private String PlayerName = new String("新玩家"); private int heroID = 10; private boolean heroIDflag = false; private static final int stringWidth[] = { 0, 255, 65280, 0xff0000, 0xffff00, 0xff00ff, 65535, 0xffffff }; //精灵颜色 private static final int drawString[] = { 10, 15, 20, 25, 30 }; private Image white[][]; //精灵图象 private Image create[]; //精灵图象0-7共8个 private int createImage; //操作指令 private int I[][]; private int getDescent[][]; private int dispose[][]; private int getY; private int getMaxAscent[][]; private int length; private int round; private int getBlue[][]; private Point trim; private Point toCharArray; private Point getRed; private int min; //空格数目 private Point x; private Color darker; //阴影色 private Color cellColor; //单元格颜色 private boolean drawLine; private boolean repaint; private boolean y; private Rectangle getMaxDescent; //超级链接矩形区域,用于判断鼠标点击超级连接 private int isPopupTrigger; private boolean fillRect; private boolean brighter; //是否进行新游戏 private boolean isPlain; //是否撤销 private boolean isShowHero; //是否显示排行榜 private boolean isHideHero; //是否隐藏排行榜 private boolean A; private Cursor B; private Cursor C; private String D[][] = { { "bColor", "0-6", "background color, 1-6 - pre-set values, 0,7.. random" } };//-------------------------------------------------------------------------------- private String driver=""; private String url=""; private String username=""; private String password=""; private TextArea output; private Button button; private int rowcount = 0; public Connection conn(){ try { Class.forName(driver); Connection con = DriverManager.getConnection(url,username,password); return con; }catch(SQLException sqle){ System.out.println("不能联接到数据库:"+sqle); return null; } catch (Exception e) { System.out.println("加载 JDBC.ODBC driver 失败."); return null; } } //------------------------------------------------------------------private void writeHeroTable() { Connection con = conn(); try{ if(con==null) return; //PreparedStatement pstmt = con.prepareStatement("UPDATE lines SET 分数 = ?, 玩家姓名 = ?, 创建日期 = ? WHERE 名次 = ?"); PreparedStatement pstmt = con.prepareStatement("UPDATE lines SET Score = ?, Name = ?, CDate = ? WHERE ID = ?"); for(int i=0;i<10;i++){ pstmt.setInt(1, Integer.parseInt(heroTable[1][i])); pstmt.setString(2, heroTable[2][i]); pstmt.setDate(3, Date.valueOf(heroTable[3][i])); pstmt.setInt(4, i+1); pstmt.executeUpdate(); } }catch(Exception e){ System.out.println("更新记录失败:"+e); return; }finally{ try{ con.close(); }catch(SQLException se){} }}//------------------------------------------------------------------ private void readHeroTable() { Connection con = conn(); String sql = "select * from lines"; try{ if(con==null) return; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); ResultSetMetaData rmeta = rs.getMetaData(); int numColumns = rmeta.getColumnCount();// for(int i=0;i<numColumns;i++) // heroTableHead[i] = rmeta.getColumnLabel(i+1); java.util.Date date; SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-mm-dd",Locale.getDefault()); System.out.println(username); int crow = 0; boolean getPlayerBestScore = false; while(rs.next()) { heroTable[0][crow] = rs.getString(1); heroTable[1][crow] = rs.getString(2); heroTable[2][crow] = rs.getString(3); heroTable[3][crow] = rs.getDate(4).toString(); if(!getPlayerBestScore) { if(rs.getString(3).equals(PlayerName)) { playerBestScore = rs.getInt(2); getPlayerBestScore = true; } } crow++; } gameBestScore = Integer.parseInt(heroTable[1][0]); }catch(Exception e){ System.out.println("查询错误:"+e); }finally{ try{ con.close(); }catch(SQLException se){} } } //------------------------------------------------------------------ private Image createHeroImage() { int w = 320,h =220; Color color1 = darker.darker(); Color color2 = darker.brighter(); Image image = createImage(w, h); Graphics g1 = image.getGraphics(); FontMetrics fontmetrics = g1.getFontMetrics(g1.getFont()); //画背景 g1.setColor(darker); g1.fillRect(0, 0, w, h); //画边框 g1.setColor(color1); g1.drawLine(0, w - 1, 0, 0); g1.drawLine(0, 0, w - 1, 0); g1.drawLine(4, h - 5, w - 5, h - 5); g1.drawLine(w - 5, h - 5, w - 5, 5); g1.setColor(color2); g1.drawLine(4, h - 5, 4, 4); g1.drawLine(4, 4, w - 5, 4); g1.drawLine(0, h - 1, w - 1, h - 1); g1.drawLine(w - 1, h - 1, w - 1, 1); //写信息 String str = "排行榜"; int len = fontmetrics.stringWidth(str); g1.setColor(Color.black); g1.drawString(str, (w - len) / 2, fontmetrics.getMaxAscent() + 4); g1.drawLine((w - len ) / 2 -10, 20, (w + len ) / 2+10, 20); g1.drawLine((w - len ) / 2 -10, 22, (w + len ) / 2+10, 22); int position = 10; for(int i=0;i<4;i++) { g1.drawString(heroTableHead[i], position , 40); position+=fontmetrics.stringWidth(heroTableHead[i]); position+=40; } g1.drawLine(10, 48, w-10, 48); int hpos = 10; int vpos = 64; for(int j=0;j<10;j++) { if(heroIDflag&&heroID==j) g1.setColor(Color.YELLOW); else g1.setColor(Color.black); for(int i=0;i<4;i++) { g1.drawString(heroTable[i][j], hpos , vpos); hpos+=fontmetrics.stringWidth(heroTableHead[i]); hpos+=40; } hpos=10; vpos+=16; } return image; }//------------------------------------------------------------------ private void showHeroTable() { //取得Applet画板,显示image Graphics g = getGraphics(); Image image = createHeroImage(); g.drawImage(image, (width-image.getWidth(this))/2, (height-image.getHeight(this))/2, this); return; }//------------------------------------------------------------------ private void changeCursor(int i1, int j1) { //如果鼠标进入超级连接区域,则改变鼠标光标形状 if(z()) return; if(getMaxDescent.contains(i1, j1)) //进入超级连接区,A为光标是否改过的标志 { if(!A) { if(B == null) B = new Cursor(12); setCursor(B); A = true; return; } } else if(A) //鼠标离开超级连接区,并且光标改过,则改回默认光标形状 { if(C == null) C = new Cursor(0); setCursor(C); A = false; } }//----------------------------------------------------------------- private void F(double ad[]) { try { double d1 = Math.sqrt(ad[0] * ad[0] + ad[1] * ad[1] + ad[2] * ad[2]); ad[0] /= d1; ad[1] /= d1; ad[2] /= d1; return; } catch(Exception _ex) { return; } }//----------------------------------------------- private void GameOver(String as[]) { //游戏结束时的提示信息 readHeroTable(); //读排行榜记录 if(isHero()) { //如果进入排行 sortHero(); //排行榜从新排序 writeHeroTable(); //写回记录 } //显示结果 heroIDflag = true; Image image = createHeroImage(); Graphics g1 = image.getGraphics(); heroIDflag = false; if(g1 == null) return; Font font = g1.getFont(); //取原始字体信息 //设置新字体 Font font1 = new Font("宋体", 1, font.getSize() * 2); FontMetrics fontmetrics = g1.getFontMetrics(font1); g1.setFont(font1); int w = image.getWidth(this); int h = image.getHeight(this); g1.setColor(Color.GRAY); for(int i=0;i<as.length;i++) { g1.drawString(as[i], (w -fontmetrics.stringWidth(as[i]) ) / 2, h /2+i*40); } g1.setColor(Color.RED); for(int i=0;i<as.length;i++) { g1.drawString(as[i], (w -fontmetrics.stringWidth(as[i]) ) / 2, h /2+i*38); } g1.setFont(font); //恢复原始字体 Graphics g = getGraphics(); g.drawImage(image, (width-image.getWidth(this))/2,(height-image.getHeight(this))/2, this); }//---------------------------------------------------------------- public String getState() { char ac[] = new char[100]; int i1 = 0; int j1 = 0; do { int k1 = 0; do { ac[i1] = j(I[j1][k1] & 0xf); i1++; } while(++k1 < 9); } while(++j1 < 9); int l1 = getFontMetrics; j1 = 0; do { ac[i1++] = j(l1 % 10); l1 /= 10; } while(++j1 < 7); l1 = getHeight; j1 = 0; do { ac[i1++] = j(l1 % 10); l1 /= 10; } while(++j1 < 7); l1 = black; j1 = 0; do { ac[i1++] = j(l1 % 10); l1 /= 10; } while(++j1 < 5); String s1 = new String(ac); return s1; }// ---------------------------------------------------------- public void setState(String s1) { if(getName) return; getName = true; if(s1 == null || s1.length() < 50) { T(); return; } f(); try { char ac[] = s1.trim().toCharArray(); int i2 = 0; int i1 = 0; do { int k1 = 0; do { int j2 = g(ac[i2++]); I[i1][k1] = j2; getDescent[i1][k1] = j2; } while(++k1 < 9); } while(++i1 < 9); int k2 = 0; int l2 = 1; i1 = 0; do { k2 += g(ac[i2++]) * l2; l2 *= 10; } while(++i1 < 7); getFontMetrics = k2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -