有没有更好的方法来绘制这样的空心立方体?
这是我使用两个正方形绘制空心立方体的代码。有更好的主意吗?
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CanvasUnit extends Canvas {
String SOutput;
public CanvasUnit(String TextToPrint) {
//SOutput = TextToPrint;
SOutput = "This is a hollow cube";
}
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
int WLen = width / 4;
int HLen = height / 4;
int WPosA = (WLen * 2);
int HPosA = (HLen * 2);
int WPosB = (WPosA+WLen/2);
int HPosB = (HPosA+HLen/2);
//Clear the screen
g.setColor(0,0,0);
g.fillRect(0, 0, width, height);
g.setColor(255,255,255);
g.drawString(SOutput,0,0,Graphics.TOP|Graphics.LEFT);
//Object1
g.drawLine(WLen, HLen, WPosA, HLen);
g.drawLine(WLen, HLen, WLen, HPosA);
g.drawLine(WLen, HPosA, WPosA, HPosA);
g.drawLine(WPosA, HLen, WPosA, HPosA);
//--------------------------------------------
//Object2
g.drawLine(Ex(WLen), Ex(HLen), WPosB, Ex(HLen));
g.drawLine(Ex(WLen), Ex(HLen), Ex(WLen), HPosB);
g.drawLine(Ex(WLen), HPosB, WPosB, HPosB);
g.drawLine(WPosB, Ex(HLen), WPosB, HPosB);
//--------------------------------------------
//Connector Lines
g.drawLine(WLen, HLen, Ex(WLen), Ex(HLen));
g.drawLine(WPosA, HLen, WPosB, Ex(HLen));
g.drawLine(WLen, HPosA, Ex(WLen), HPosB);
g.drawLine(WPosA, HPosA, WPosB, HPosB);
}
private int Ex(int value){
return value + (value/2); }
}
输出 : 输出:http://up.iranblog.com/images/ttfhinrbao6hvlkko8uk.png
This is my code to paint a hollow cube using two squares . any better idea ?
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CanvasUnit extends Canvas {
String SOutput;
public CanvasUnit(String TextToPrint) {
//SOutput = TextToPrint;
SOutput = "This is a hollow cube";
}
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
int WLen = width / 4;
int HLen = height / 4;
int WPosA = (WLen * 2);
int HPosA = (HLen * 2);
int WPosB = (WPosA+WLen/2);
int HPosB = (HPosA+HLen/2);
//Clear the screen
g.setColor(0,0,0);
g.fillRect(0, 0, width, height);
g.setColor(255,255,255);
g.drawString(SOutput,0,0,Graphics.TOP|Graphics.LEFT);
//Object1
g.drawLine(WLen, HLen, WPosA, HLen);
g.drawLine(WLen, HLen, WLen, HPosA);
g.drawLine(WLen, HPosA, WPosA, HPosA);
g.drawLine(WPosA, HLen, WPosA, HPosA);
//--------------------------------------------
//Object2
g.drawLine(Ex(WLen), Ex(HLen), WPosB, Ex(HLen));
g.drawLine(Ex(WLen), Ex(HLen), Ex(WLen), HPosB);
g.drawLine(Ex(WLen), HPosB, WPosB, HPosB);
g.drawLine(WPosB, Ex(HLen), WPosB, HPosB);
//--------------------------------------------
//Connector Lines
g.drawLine(WLen, HLen, Ex(WLen), Ex(HLen));
g.drawLine(WPosA, HLen, WPosB, Ex(HLen));
g.drawLine(WLen, HPosA, Ex(WLen), HPosB);
g.drawLine(WPosA, HPosA, WPosB, HPosB);
}
private int Ex(int value){
return value + (value/2); }
}
Output :
Output : http://up.iranblog.com/images/ttfhinrbao6hvlkko8uk.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法:
Better way :