在此谨论绘制简单的3D图形和简单调试

对于想画图的类都需要创建画布,一般都建议直接继承JFrame,若想进行控制画出不同的图案,可以在最初界面上添加按钮或其他并绑上监听器。

import javax.swing.*;
import java.awt.*;

public class Paint3D extends JFrame {
    DrawAction drawAction=new DrawAction();
    public void Paint3d(){
        setTitle("绘画晶体");
        setSize(1100,850);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        FlowLayout fl=new FlowLayout();
        setLayout(fl);
        JButton btn1=new JButton("正四面体");
        JButton btn2=new JButton("正八面体");
        add(btn1);
        add(btn2);
        ButtonGroup group=new ButtonGroup();
        setVisible(true);

        btn1.addActionListener(drawAction);
        btn2.addActionListener(drawAction);
        Graphics g=getGraphics();
        drawAction.g=g;
        drawAction.ui=this;
    }





    public static void main(String[] args){
        Paint3D paint3D=new Paint3D();
        paint3D.Paint3d();
    }
}

在监听类中编写画图代码,这里为封装性只加了控制与对鼠标的判断,具体的画图部分创建了对应的新类。

public class DrawAction implements ActionListener {
    public Paint3D ui;
    Graphics g;

    @Override
    public void actionPerformed(ActionEvent e) {
        String btnStr = e.getActionCommand();
        JButton btn = (JButton) e.getSource();
        if (btnStr.equals("正四面体")) {
            int x1 = 250, y1 = 350;
            FourPlane.fourPlane(g,x1,y1);
        } else if (btnStr.equals("正八面体")) {
            int x2=600,y2=350;
                EightPlane.eightPlane(g,x2,y2);
        }
    }
}

以四面体为例,根据输入的点在对应位置画图,取其一个方向观察,只需涂色三面,计算其各点的位置,再进行涂色。其代码不长,若直接加在监听类中也不难观察,但单独创建方便调试。

public class FourPlane {

    public static void fourPlane(Graphics g,int x1,int y1){


        //four points
        double p=0.0;
        int r1 = 40;
        double p1 = Math.sqrt(3);
        double x2 = x1 + r1 * p1, y2 = y1 - r1;
        double x3 = x1 + p1 * r1, y3 = y1 - r1 * 3;
        double x4 = x1 + r1 * 2 * p1, y4 = y1;
        double pi=3.1415926;
        for (;;p=p+1.0) {
            double p2x=(x2-x1)*Math.cos(pi/180.0*p)-(y2-y1)*Math.sin(pi/180.0*p)+x1;
            double p2y=(x2-x1)*Math.sin(pi/180.0*p)+(y2-y1)*Math.cos(pi/180.0*p)+y1;
            double p3x=(x3-x1)*Math.cos(pi/180.0*p)-(y3-y1)*Math.sin(pi/180.0*p)+x1;
            double p3y=(x3-x1)*Math.sin(pi/180.0*p)+(y3-y1)*Math.cos(pi/180.0*p)+y1;
            double p4x=(x4-x1)*Math.cos(pi/180.0*p)-(y4-y1)*Math.sin(pi/180.0*p)+x1;
            double p4y=(x4-x1)*Math.sin(pi/180.0*p)+(y4-y1)*Math.cos(pi/180.0*p)+y1;
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            g.setColor(Color.DARK_GRAY);
           g.fillRect(0,100,800,800);
            //the first plane
            java.awt.Polygon po1 =new Polygon();
            po1.addPoint(x1, y1);
            po1.addPoint((int) p2x, (int) p2y);
            po1.addPoint((int) p3x, (int) p3y);
            g.setColor(new Color(255, 150, 55));
            g.fillPolygon(po1);

            //the second plane
            java.awt.Polygon po2 = new Polygon();
            po2.addPoint((int) p2x, (int) p2y);
            po2.addPoint((int) p3x, (int) p3y);
            po2.addPoint((int) p4x, (int) p4y);
            g.setColor(new Color(200, 150, 110));
            g.fillPolygon(po2);

            //the third plane
            java.awt.Polygon po3 = new Polygon();
            po3.addPoint(x1, y1);
            po3.addPoint((int) p2x, (int) p2y);
            po3.addPoint((int) p4x, (int) p4y);
            g.setColor(new Color(145, 150, 165));
            g.fillPolygon(po3);

        }
    }
}

最后是正八面体的效果图。

正八面体效果图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值