浏览 2000 次
锁定老帖子 主题:魔方图像化初试
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-11
最后修改:2009-02-11
代码没有优化 package com.onezero; import java.awt.*; import javax.swing.*; public class CubePaint extends JFrame{ private static double[][] points={}; private MagicCube mc; CubePaint(){ super("魔方游戏"); mc = new MagicCube(CubeNode.getOriginNode()); this.setSize(600,620); final CubeCanvas cc = new CubeCanvas(this); this.add(cc); this.setVisible(true); new Thread(){ public void run(){ try{ do{ mc.rotate(1, 0); cc.repaint(); Thread.sleep(5000); if(mc.equals(MagicCube.ORIGINCUBE))break; mc.rotate(2, 0); cc.repaint(); Thread.sleep(5000); if(mc.equals(MagicCube.ORIGINCUBE))break; mc.rotate(-1, 2); cc.repaint(); Thread.sleep(5000); if(mc.equals(MagicCube.ORIGINCUBE))break; mc.rotate(-2, 2); cc.repaint(); Thread.sleep(5000); if(mc.equals(MagicCube.ORIGINCUBE))break; }while(true); }catch(Exception e){ } } }.start(); } public MagicCube getMc() { return mc; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new CubePaint(); } } class CubeCanvas extends JComponent { static int wall = 20; static int space = 3; private MagicCube mc; private CubePaint cp; private int width, height; private int wspace, hspace; private int len; CubeCanvas(CubePaint cp){ this.cp = cp; this.mc = cp.getMc(); width = cp.getWidth(); height = cp.getHeight()-20; len = (Math.min(width, height) - 2 * wall) / 9; wspace = (width - 9*len)/2; hspace = (height - 9*len)/2; } public void paint(Graphics g) { width = cp.getWidth(); height = cp.getHeight()-20; len = (Math.min(width, height) - 2 * wall) / 9; wspace = (width - 9*len)/2; hspace = (height - 9*len)/2; for(int i=0;i<27;i++){ if(i==13)continue; draw(g,mc.getCubeNode(i)); } } private void draw(Graphics g, CubeNode node){ if(node==null)return; int[] xyz = node.location.xyz; if(xyz[Point3D.Y]==0){ //前面 int[] xPoints = getxPoints(xyz,1); int[] yPoints = getyPoints(xyz,1); g.setColor(getColor(node.nodecolor.getColorId(1))); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.BLACK); g.drawPolygon(xPoints, yPoints, 4); } if(xyz[Point3D.X]==2){ //右面 int[] xPoints = getxPoints(xyz,2); int[] yPoints = getyPoints(xyz,2); g.setColor(getColor(node.nodecolor.getColorId(2))); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.BLACK); g.drawPolygon(xPoints, yPoints, 4); } if(xyz[Point3D.Z]==2){ //上层 int[] xPoints = getxPoints(xyz,5); int[] yPoints = getyPoints(xyz,5); g.setColor(getColor(node.nodecolor.getColorId(5))); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.BLACK); g.drawPolygon(xPoints, yPoints, 4); } } private int[] getxPoints(int[] xyz, int face){ int[] xPoints = new int[4]; switch(face){ case 1: xPoints[0] = wspace + 2*xyz[0]*len; xPoints[1] = xPoints[0]; xPoints[2] = xPoints[1] + 2*len; xPoints[3] = xPoints[2]; xPoints[0] += space; xPoints[1] += space; break; case 2: xPoints[0] = wspace + xyz[1]*len + 6*len; xPoints[1] = xPoints[0]; xPoints[2] = xPoints[1] + len; xPoints[3] = xPoints[2]; xPoints[0] += space; xPoints[1] += space; break; case 5: xPoints[0] = wspace + (2*xyz[0] + xyz[1])*len; xPoints[1] = xPoints[0] + len; xPoints[3] = xPoints[1] + len; xPoints[2] = xPoints[3] + len; xPoints[0] += space; xPoints[1] += space/2; xPoints[2] -= space/2; } return xPoints; } private int[] getyPoints(int[] xyz, int face){ int[] yPoints = new int[4]; switch(face){ case 1: yPoints[0] = hspace + 2*(3 - xyz[2])*len + 3*len; yPoints[1] = yPoints[0] - 2*len; yPoints[2] = yPoints[1]; yPoints[3] = yPoints[0]; yPoints[1] += space; yPoints[2] += space; break; case 2: yPoints[0] = hspace + 2*(3 - xyz[2])*len + (3-xyz[1])*len; yPoints[3] = yPoints[0] - len; yPoints[1] = yPoints[3] - len; yPoints[2] = yPoints[1] - len; yPoints[0] -= space; yPoints[1] += space/2; yPoints[2] += space + space/2; break; case 5: yPoints[0] = hspace + (3 - xyz[1])*len; yPoints[1] = yPoints[0] - len; yPoints[2] = yPoints[1]; yPoints[3] = yPoints[0]; yPoints[1] += space; yPoints[2] += space; } return yPoints; } private Color getColor(int colorId){ switch(colorId){ case 0: return Color.RED; case 1: return Color.BLUE; case 2: return Color.GREEN; case 3: return Color.YELLOW; case 4: return Color.PINK; default: return Color.WHITE; } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-02-12
哥们MagicCube类也放上来
|
|
返回顶楼 | |
发表时间:2009-02-13
|
|
返回顶楼 | |