魔方图像化初试

接上一篇,图像化初步尝试
代码没有优化
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;
}
}
}

[img]http://andyjojo.iteye.com/upload/picture/pic/31067/8bd14157-9c84-377d-aef2-602f8acb67e4.bmp[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值