有没有更好的方法来绘制这样的空心立方体?

发布于 2024-10-22 13:35:09 字数 1844 浏览 2 评论 0原文

这是我使用两个正方形绘制空心立方体的代码。有更好的主意吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-10-29 13:35:09

更好的方法:

    //Draw two hollow squares
    g.drawRect(WLen, HLen, WLen, HLen);
    g.drawRect(Ex(WLen), Ex(HLen), WLen, HLen);
    //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);

Better way :

    //Draw two hollow squares
    g.drawRect(WLen, HLen, WLen, HLen);
    g.drawRect(Ex(WLen), Ex(HLen), WLen, HLen);
    //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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文