图形图像处理(学习记录)

一、实现功能

1: 实现一些基础图形: 直线 矩形 圆形 三角形 多边形 立体 等等
2: 实现迭代 递归分形
3: 实现保存 打开 绘制图片 撤回
4: 图像处理:滤镜 缩放 旋转
5: 视频处理 - 多线程

一.1 基础图形实现的主要方法工具与类

1 : 界面开发 JFrame
2:  监听器: 监听窗体上的鼠标操作 MouseListener
       鼠标事件: 可以获取鼠标按下的坐标 MouseEvent e
       e.getX() e.getY()
3:  画笔 渲染工具 : Graphics 直线 矩形 圆形 图片 字符串 多边形
       1: 从窗体对象上获取,只能窗体内绘制 Graphics g= jf.getGraphics();
       2: 必须在可视化之后获取,窗体对象是在可视化之后再创建 Graphics 渲染对象的
       3:  绘制图形: g.drawLine(x1,y1,x2,y2); g.fillOval();
       4: 同时实现动作监听器 ,监听界面上的按钮 ,通过获取按钮上的文本 来选择绘制什么图形

一.2 基础图形实现代码 

   UI界面实现

package Lyjm;

import javax.swing.*;
import java.awt.*;
public class GraphicsUI{


 ShapeListener sl = new ShapeListener();
    public void showUI(){
        JFrame jf = new JFrame ("图形图像处理V1.0");
        jf.setSize(800,600);
        jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        jf.setLayout (new FlowLayout ());
        String[] btnStrs = {"直线","矩形","圆形","实心矩形","实心圆形","等腰三角形","立方体","球形"};
        for(int i = 0; i < btnStrs.length; i++){
            JButton btn = new JButton (btnStrs[i]);
            jf.add (btn);
            btn.addActionListener (sl);
        }
        jf.setVisible (true);
        jf.setLocationRelativeTo(null);
// 获取 画笔对象
        Graphics g = jf.getGraphics();
        sl.g=g;// 将 g中获取的画笔对象的地址 赋值给 sl中的画笔变量名
// 窗体对象加载监听器
        jf.addMouseListener(sl);
}
        public static void main(String[] args){
            GraphicsUI ui = new GraphicsUI();
            ui.showUI();
        }
    }

交互画笔渲染

package Lyjm;
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 ShapeListener implements MouseListener, ActionListener{
    Graphics g;// 创建一个画笔对象变量名 存储对象的地址
    int x1, y1, x2, y2;// 定义两个点的坐标
    String type = "直线";
    @Override
    public void actionPerformed(ActionEvent e){
        System.out.println ("按钮被点击了");
        String btnText = e.getActionCommand ();
        System.out.println ("btnText = " + btnText);
        type = btnText;
    }
    @Override
    public void mouseClicked(MouseEvent e){
        System.out.println ("mouseClicked");
    }
    @Override
    public void mousePressed(MouseEvent e){
        System.out.print ("mousePressed");
        int x = e.getX ();
        int y = e.getY ();
        System.out.println (" x = " + x + ", y = " + y);
// 赋值给类全局变量 x1 y1
        x1 = x;
        y1 = y;
        if(type.equals ("球形")){
            for(int i = 0; i < 255; i++){
                Color color = new Color (0 + i, 0 + i, 0 + i);//0-255
                g.setColor (color);
                g.fillOval (x1 + i / 3, y1 + i / 3, 255 - i, 255 - i);
            }
            g.setColor (Color.BLACK);
        }
    }
    @Override
    public void mouseReleased(MouseEvent e){
        System.out.print ("mouseReleased ");
        int x = e.getX ();
        int y = e.getY ();
        System.out.println (" x = " + x + ", y = " + y);
// 赋值给类全局变量 x2 y2
        x2 = x;
        y2 = y;
        if(type.equals ("直线")){
// 绘制直线
            g.drawLine (x1, y1, x2, y2);
        }else if(type.equals ("矩形")){
// 绘制矩形
            g.drawRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
                    Math.abs (y2 - y1));
        }else if(type.equals ("圆形")){
// 绘制圆形 外切矩形左上角的坐标 宽度 高度
            g.drawOval (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
                    Math.abs (y2 - y1));
        }else if(type.equals ("实心矩形")){
            g.fillRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
                    Math.abs (y2 - y1));
        }else if(type.equals ("实心圆形")){
            g.fillOval (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1),
                    Math.abs (y2 - y1));
        }else if(type.equals ("立方体")){
// 球
// g.fillOval (Math.min (x1,x2), Math.min (y1,y2), Math.abs (x2 - x1), Math.abs(y2 - y1));
            g.drawRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1), Math.abs (y2 - y1));
            g.drawRect (Math.min (x1, x2) + 100, Math.min (y1, y2) - 150, Math.abs (x2 - x1), Math.abs (y2 - y1));
            g.drawLine (Math.min (x1, x2), Math.min (y1, y2), Math.min (x1, x2) + 100, Math.min (y1, y2) - 150);
            g.drawLine (Math.max (x1, x2), Math.min (y1, y2), Math.max (x1, x2) + 100, Math.min (y1, y2) - 150);
            g.drawLine (Math.max (x1, x2), Math.max (y1, y2), Math.max (x1, x2) + 100, Math.max (y1, y2) - 150);
            g.drawLine (Math.min (x1, x2), Math.max (y1, y2), Math.min (x1, x2) + 100, Math.max (y1, y2) - 150);
        } else if (type.equals ("等腰三角形")) {
            g.drawLine (x1, y1, x2, y2);
            double d = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
            double x_mid = (x1 + x2) / 2;
            double y_mid = (y1 + y2) / 2;
            // 计算两点之间的斜率
            double k = (y2 - y1) / (x2 - x1);
            // 计算垂直于两点连线的斜率
            double k_perpendicular = -1 / k;
            // 找到新点的坐标
            int x3 = (int) (x_mid + Math.sqrt(Math.pow((d / 2), 2) / (1 + Math.pow(k_perpendicular, 2))));
            int y3 = (int) (y_mid + k_perpendicular * (x3 - x_mid));
            g.drawLine (x1, y1, x3, y3);
            g.drawLine (x3, y3, x2, y2);
        } // 等腰三角形


// 平行四边形
// 立方体
    }
    @Override
    public void mouseEntered(MouseEvent e){
        System.out.println ("mouseEntered");
    }
    @Override
    public void mouseExited(MouseEvent e){
        System.out.println ("mouseExited");
    }
}
实现结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值