分形之美——简单的图形绘制与画笔

文章介绍了使用JavaSwing库实现的一个名为`drawtower`的类,它扩展了JFrame,包含画金字塔、画方形和画笔功能,通过MouseListener和ActionListener处理鼠标事件。
摘要由CSDN通过智能技术生成
package draw;

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

public class drawtower extends JFrame {
    DrawAction gol=new DrawAction();

    public void initUI() {
        this.setTitle("金字塔");
        this.setDefaultCloseOperation(3);
        this.setSize(800, 800);
        JButton anniu1 = new JButton("画金字塔");
        this.add(anniu1);
        JButton anniu2=new JButton("画方形");
        this.add(anniu2);
        JButton anniu3=new JButton("画笔");
        this.add(anniu3);
        this.setVisible(true);
        FlowLayout fl = new FlowLayout();
        Graphics g=this.getGraphics();
        this.setLayout(fl);
        DrawAction UI = new DrawAction(g);
        anniu1.addActionListener(UI);
        anniu2.addActionListener(UI);
        anniu3.addActionListener(UI);
        this.addMouseListener(gol);
        penDraw pan=new penDraw(g);
        this.addMouseMotionListener(pan);

    }

    public static void main(String[] args) {
        drawtower hua = new drawtower();
        hua.initUI();


    }
}
package draw;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class DrawAction implements MouseListener, ActionListener {


    private Graphics g = null;

    public DrawAction(Graphics g) {
        this.g = g;
    }

    public DrawAction() {

    }


    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println(x);
        System.out.println(y);


    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        g.setColor(new Color(255, 255, 255, 255));
        g.fillRect(0, 100, 2000, 1000);
        if (cmd.equals("画金字塔")) {
            System.out.println("1");
            int a = 200, A = 300;
            int p1x = 400, p1y = 200;
            int p2x = p1x - 200, p2y = p1y + 300;
            int p3x = p1x - 50, p3y = p1y + 450;
            int p4x = p1x + 200, p4y = p1y + 300;
            int a1x = p1x - 70, a1y = p1y + 105;
            int a2x = p1x - 150, a2y = p1y + 225;
            int b1x = a1x + 53, b1y = a1y + 53;
            int b2x = a2x + 113, b2y = a2y + 113;
            int c1x = b1x + 87, c1y = b1y - 53;
            int c2x = b2x + 186, c2y = b2y - 112;


            java.awt.Polygon po1 = new Polygon();
            po1.addPoint(p1x, p1y);
            po1.addPoint(p2x, p2y);
            po1.addPoint(p3x, p3y);
            g.setColor(new Color(148, 101, 0));
            g.fillPolygon(po1);

            java.awt.Polygon po2 = new Polygon();
            po2.addPoint(p1x, p1y);
            po2.addPoint(p4x, p4y);
            po2.addPoint(p3x, p3y);
            g.setColor(new Color(250, 192, 4));
            g.fillPolygon(po2);

            g.setColor(Color.black);
            g.drawLine(p1x, p1y, p2x, p2y);
            g.drawLine(p2x, p2y, p3x, p3y);
            g.drawLine(p4x, p4y, p3x, p3y);
            g.drawLine(p1x, p1y, p3x, p3y);
            g.drawLine(p1x, p1y, p4x, p4y);
            g.drawLine(p1x + 1, p1y + 1, p2x + 1, p2y + 1);
            g.drawLine(p2x + 1, p2y + 1, p3x + 1, p3y + 1);
            g.drawLine(p4x + 1, p4y + 1, p3x + 1, p3y + 1);
            g.drawLine(p1x + 1, p1y + 1, p3x + 1, p3y + 1);
            g.drawLine(p1x + 1, p1y + 1, p4x + 1, p4y + 1);
            g.drawLine(a1x, a1y, b1x, b1y);
            g.drawLine(a2x, a2y, b2x, b2y);
            g.drawLine(b1x, b1y, c1x, c1y);
            g.drawLine(b2x, b2y, c2x, c2y);
            g.drawLine(a1x + 1, a1y + 1, b1x + 1, b1y + 1);
            g.drawLine(a2x + 1, a2y + 1, b2x + 1, b2y + 1);
            g.drawLine(b1x + 1, b1y + 1, c1x + 1, c1y + 1);
            g.drawLine(b2x + 1, b2y + 1, c2x + 1, c2y + 1);
            g.drawLine(p1x, p1y, 353, 328);
            g.drawLine(341, 315, 270, 444);
            g.drawLine(370, 344, 330, 505);
            g.drawLine(300, 475, 268, 568);
            g.drawLine(p1x, p1y, 433, 329);
            g.drawLine(410, 341, 422, 505);
            g.drawLine(454, 317, 515, 447);
            g.drawLine(470, 474, 496, 564);
        } else if (cmd.equals("画方形")) {
            int w = 200, h = 150;
            int wx = 80, wy = 100;

            int p1x = 200, p1y = 500;
            int p2x = p1x + w, p2y = p1y;
            int p3x = p2x, p3y = p2y - h;
            int p4x = p1x, p4y = p1y - h;

            int p5x = p4x + wx, p5y = p4y - wy;
            int p6x = p3x + wx, p6y = p3y - wy;
            int p7x = p2x + wx, p7y = p2y - wy;

            g.setColor(Color.black);
            g.drawLine(p1x, p1y, p2x, p2y);
            g.drawLine(p1x, p1y, p4x, p4y);
            g.drawLine(p2x, p2y, p3x, p3y);
            g.drawLine(p3x, p3y, p4x, p4y);
            g.drawLine(p3x, p3y, p6x, p6y);
            g.drawLine(p4x, p4y, p5x, p5y);
            g.drawLine(p5x, p5y, p6x, p6y);
            g.drawLine(p6x, p6y, p7x, p7y);
            g.drawLine(p2x, p2y, p7x, p7y);
            Polygon p1 = new Polygon();
            p1.addPoint(p1x, p1y);
            p1.addPoint(p2x, p2y);
            p1.addPoint(p3x, p3y);
            p1.addPoint(p4x, p4y);
            g.setColor(new Color(246, 81, 10));
            g.fillPolygon(p1);

            Polygon p2 = new Polygon();
            p2.addPoint(p3x, p3y);
            p2.addPoint(p4x, p4y);
            p2.addPoint(p5x, p5y);
            p2.addPoint(p6x, p6y);
            g.setColor(new Color(252, 133, 68));
            g.fillPolygon(p2);

            Polygon p3 = new Polygon();
            p3.addPoint(p2x, p2y);
            p3.addPoint(p3x, p3y);
            p3.addPoint(p6x, p6y);
            p3.addPoint(p7x, p7y);
            g.setColor(new Color(255, 157, 99));
            g.fillPolygon(p3);
        } else if (cmd.equals("画笔")) {
            g.fillRect(0, 100, 1000, 1000);
        }


    }

    public void mouseClicked(MouseEvent e) {
    }

    ;


    public void mouseReleased(MouseEvent e) {
    }

    ;

    public void mouseEntered(MouseEvent e) {
    }

    ;


    public void mouseExited(MouseEvent e) {
    }

    ;

}
package draw;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class penDraw implements java.awt.event.MouseMotionListener{
    private Graphics g;

    public penDraw(Graphics g) {
        this.g = g;
    }

    public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("横坐标;"+x+"纵坐标;"+y);
        g.setColor(Color.black);
        g.fillOval(x-25,y-25,50,50);

    }

    ;


    public void mouseMoved(MouseEvent e) {
    }

    ;

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值