Java编写的画图板,功能非常齐全,完整代码 附详细设计报告

今天为大家分享一个java语言编写的图书管理程序-003,目前系统功能已经很全面,后续会进一步完善。整个系统界面漂亮,有完整得源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以,数据为MySQL。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。

主要功能

Java编写的画图板,功能非常齐全,代码可以执行运行运行。

本次课程设计是利用Java 语言来进行开发画图面板,主要是利用Java语言中自带的Graphics2D强大的画图功能来进行画图的。

Graphics2D继承自Graphics,它扩展了Graphics的绘图功能,拥有更强大的二维图形处理能力,提供对几何形状、坐标转换、颜色管理以及文字布局等更精确的控制。

Graphics2D定义了几种方法,用于添加或改变图形的状态属性。可以通过设定和修改状态属性,指定画笔宽度和画笔的连接方式,设定平移、旋转、缩放或修剪变换图形,以及设定填充图形的颜色和图案等。本次画图面板的实现逻辑如下:

步骤1 定一个基类Shape,继承MouseMotionListener,里面记录了

/** 画笔大小 */

protected float stroke;

/** 画笔颜色 */

protected Color color;

/** 图形类型 */

protected String type;//要画的图形类型

protected int startX = 0;//起始点X坐标

protected int startY = 0;//起始点X坐标

protected int endX = 0;//终止点X坐标

protected int endY = 0;//终止点Y坐标

protected int currentX = 0;//当前X坐标

protected int currentY = 0;//当前Y坐标

protected int currentD = 0;//移动距离

public List<Point> pointList=new ArrayList<Point>();//中途所有经过点的坐标

步骤2 所有类型的图形(文字,直线),都继承Shape

步骤3 当点击不同图标的时候,调用不同的类型的Draw方法,

(1)画直线的时候调用

graphics2d.drawLine(startX, startY, endX, endY);

(2) 画矩形时候调用

xS[0] = currentX;

yS[0] = currentY;

xS[1] = currentX + currentD;

yS[1] = currentY;

xS[2] = currentX+ currentD / 2;

yS[2] = (int)(currentY - currentD / 2 * 1.732);

graphics2d.setColor(color);

graphics2d.setStroke(new BasicStroke(stroke));

graphics2d.drawRect(currentX, currentY, currentD, currentD);

  1. 画圆形的时候调用

graphics2d.drawOval(currentX, currentY, currentD, currentD);

(4) 画笔画的时候

int[] arrayx = new int[getPointList().size()];

int[] arrayy = new int[getPointList().size()];

for (int i = 0; i < getPointList().size(); i++) {

Point point = (Point)getPointList().get(i);

arrayx[i]=point.getX();

arrayy[i]=point.getY();

}

graphics2d.setColor(color);

graphics2d.setStroke(new BasicStroke(stroke));

graphics2d.drawPolyline(arrayx,arrayy,getPointList().size());

  1. 画橡皮檫的时候

graphics2d.setColor(Color.WHITE);

graphics2d.setStroke(new BasicStroke(8.5f));

int[] arrayx = new int[getPointList().size()];

int[] arrayy = new int[getPointList().size()];

for (int i = 0; i < getPointList().size(); i++) {

Point point = (Point)getPointList().get(i);

arrayx[i]=point.getX();

arrayy[i]=point.getY();

}

graphics2d.setColor(color);

graphics2d.setStroke(new BasicStroke(stroke));

graphics2d.drawPolyline(arrayx,arrayy,getPointList().size());

步骤4 所有类型的图形(文字,直线),都继承Shape,当鼠标按键,或者拖动的时候,在面板上进行重绘,把存储在List中的Shape,重新绘制一遍。得到我们要画的最终的图形,保存图片即可。

1 画直线和文字

2画曲线

3 画矩形

4 画椭圆

5 橡皮擦

6 撤销和重做功能

7 从画图板导入图片,并且对图片进行保存

8 设置画图的颜色和像素大小

这个项目涵盖了java 窗体编程的各种知识,包括UI界面设计、时间处理、文件操作等。通过这个项目能快速提升java 窗体编程,是非常好的一个项目。代码可以直接运行,没有任何bug。有详细的操作手册

实现效果

1 主界面

2 画直线和圆圈

3 设置属性(颜色、字体大小)

4 保存结果

点击顶部边工具栏上的保存图标,出现保存图片的对话框,选择保存路径,完成图片的保存。

关键代码实现

package com.huatu.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JLabel;

/**

 */
public abstract class Shape extends JLabel implements MouseMotionListener {

    private static final long serialVersionUID = 1L;

    /** 画笔大小 */
    protected float stroke;
    /** 画笔颜色 */
    protected Color color;
    /** 图形类型 */
    protected String type;

    protected int startX = 0;
    protected int startY = 0;
    protected int endX = 0;
    protected int endY = 0;
    protected int currentX = 0;
    protected int currentY = 0;
    protected int currentD = 0;

    protected Shape(Color color1, float stroke1, String type1, int x, int y) {
        type = type1;
        color = color1;
        stroke = stroke1;
        startX = endX = currentX = x;
        startY = endY = currentY = y;

        addMouseMotionListener(this);
    }

    public void mouseDragged(MouseEvent mouseevent) {
        // 变成十字架拖放的时候,需要计算圆形和矩形
        if (DrawingBoard.cursor == 1) {
            endX = mouseevent.getX();
            endY = mouseevent.getY();
            currentD = Math.abs(startX - endX);

            if (startX > endX) {
                currentX = endX;
            }
            if (startY > endY) {
                currentY = endY;
            }
        } else {// 拖放完毕之后再次点击只需要控制位置即可
            currentX = mouseevent.getX();
            currentY = mouseevent.getY();
        }
    }

    public void mouseMoved(MouseEvent mouseevent) {
        // 鼠标移动
    }

    /**
     * 调用画板来画图
     * @param graphics2d 画板的graphics
     */
    public abstract void draw(Graphics2D graphics2d);

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 可以使用 Swing 来创建 GUI 应用程序,因此我们可以使用 Swing 来创建一个多功能图板程序。下面是一个简单的实现示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawingBoard extends JFrame implements ActionListener, MouseListener, MouseMotionListener { private JPanel canvas; private JMenuBar menuBar; private JMenu fileMenu, shapeMenu, colorMenu; private JMenuItem newMenuItem, saveMenuItem, exitMenuItem; private JMenuItem lineMenuItem, rectMenuItem, ovalMenuItem; private JMenuItem redMenuItem, greenMenuItem, blueMenuItem; private Color currentColor = Color.BLACK; private int shapeType = 0; // 0 for line, 1 for rectangle, 2 for oval private Point startPoint, endPoint; public DrawingBoard() { super("Drawing Board"); setSize(800, 600); canvas = new JPanel(); canvas.setBackground(Color.WHITE); canvas.addMouseListener(this); canvas.addMouseMotionListener(this); add(canvas); menuBar = new JMenuBar(); fileMenu = new JMenu("File"); newMenuItem = new JMenuItem("New"); saveMenuItem = new JMenuItem("Save"); exitMenuItem = new JMenuItem("Exit"); newMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); fileMenu.add(newMenuItem); fileMenu.add(saveMenuItem); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); shapeMenu = new JMenu("Shape"); lineMenuItem = new JMenuItem("Line"); rectMenuItem = new JMenuItem("Rectangle"); ovalMenuItem = new JMenuItem("Oval"); lineMenuItem.addActionListener(this); rectMenuItem.addActionListener(this); ovalMenuItem.addActionListener(this); shapeMenu.add(lineMenuItem); shapeMenu.add(rectMenuItem); shapeMenu.add(ovalMenuItem); menuBar.add(shapeMenu); colorMenu = new JMenu("Color"); redMenuItem = new JMenuItem("Red"); greenMenuItem = new JMenuItem("Green"); blueMenuItem = new JMenuItem("Blue"); redMenuItem.addActionListener(this); greenMenuItem.addActionListener(this); blueMenuItem.addActionListener(this); colorMenu.add(redMenuItem); colorMenu.add(greenMenuItem); colorMenu.add(blueMenuItem); menuBar.add(colorMenu); setJMenuBar(menuBar); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == newMenuItem) { canvas.removeAll(); canvas.repaint(); } else if (e.getSource() == saveMenuItem) { // TODO: save the drawing to a file } else if (e.getSource() == exitMenuItem) { System.exit(0); } else if (e.getSource() == lineMenuItem) { shapeType = 0; } else if (e.getSource() == rectMenuItem) { shapeType = 1; } else if (e.getSource() == ovalMenuItem) { shapeType = 2; } else if (e.getSource() == redMenuItem) { currentColor = Color.RED; } else if (e.getSource() == greenMenuItem) { currentColor = Color.GREEN; } else if (e.getSource() == blueMenuItem) { currentColor = Color.BLUE; } } public void mousePressed(MouseEvent e) { startPoint = e.getPoint(); } public void mouseReleased(MouseEvent e) { endPoint = e.getPoint(); Graphics g = canvas.getGraphics(); g.setColor(currentColor); if (shapeType == 0) { g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y); } else if (shapeType == 1) { int x = Math.min(startPoint.x, endPoint.x); int y = Math.min(startPoint.y, endPoint.y); int width = Math.abs(startPoint.x - endPoint.x); int height = Math.abs(startPoint.y - endPoint.y); g.drawRect(x, y, width, height); } else if (shapeType == 2) { int x = Math.min(startPoint.x, endPoint.x); int y = Math.min(startPoint.y, endPoint.y); int width = Math.abs(startPoint.x - endPoint.x); int height = Math.abs(startPoint.y - endPoint.y); g.drawOval(x, y, width, height); } } public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public static void main(String[] args) { new DrawingBoard(); } } ``` 这个程序使用了 JPanel 来创建布,使用 JMenuBar 来创建菜单栏。菜单栏包括文件菜单、形状菜单和颜色菜单,可以选择新建、保存、退出、直线、矩形、椭圆和选择颜色。根据用户的选择,可以在布上出不同形状和颜色的图形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值