21xrx.com
2024-11-10 00:58:07 Sunday
登录
文章检索 我的文章 写文章
我是一名Java程序员
2023-06-11 09:22:13 深夜i     --     --

我是一名Java程序员,最近在学习Java绘图方面的知识,于是我编写了一个简单的Java画图程序,代码可以展示如下。

关键词一:Java绘图

Java是一门强大的编程语言,其绘图库也非常丰富。Java提供了Graphics类和Graphics2D类用于绘制基本图形,如直线、矩形、圆形等。同时,Java还支持使用BufferedImage处理图像,从而实现更高级的图像处理操作。

关键词二:简单画图板

我的Java画图程序是一个简单的画图板,可以支持绘制直线、矩形、圆形等基本图形,同时也可以支持图形的填充、缩放和移动。虽然功能不是很复杂,但是代码实现起来还是需要一些技巧和耐心的。

关键词三:完整代码

下面是我的Java画图程序的完整代码,供大家参考学习。


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Line2D;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class JavaDraw extends JPanel {

  BufferedImage image;

  Graphics2D graphics2D;

  Rectangle2D.Float rectangle = new Rectangle2D.Float();

  Ellipse2D.Float ellipse = new Ellipse2D.Float();

  Line2D.Float line = new Line2D.Float();

  Point start = new Point(0, 0);

  Point end = new Point(0, 0);

  boolean drawingRectangle = false;

  boolean drawingEllipse = false;

  boolean drawingLine = false;

  boolean filling = false;

  public JavaDraw() {

    setBackground(Color.white);

    addMouseListener(new MouseListener() {

      public void mouseReleased(MouseEvent e) {

        if (drawingRectangle == true) {

          rectangle.setFrameFromDiagonal(start, end);

          graphics2D.draw(rectangle);

          if (filling) {

            graphics2D.fill(rectangle);

          }

          drawingRectangle = false;

        }

        if (drawingEllipse == true) {

          ellipse.setFrameFromDiagonal(start, end);

          graphics2D.draw(ellipse);

          if (filling) {

            graphics2D.fill(ellipse);

          }

          drawingEllipse = false;

        }

        if (drawingLine == true) {

          line.setLine(start, end);

          graphics2D.draw(line);

          drawingLine = false;

        }

      }

      public void mousePressed(MouseEvent e) {

        start = e.getPoint();

        end = e.getPoint();

        if (drawingRectangle == false && drawingEllipse == false && drawingLine == false) {

          graphics2D.fillOval(start.x, start.y, 2, 2);

        }

        if (drawingRectangle == true) {

          rectangle.setFrameFromDiagonal(start, end);

        }

        if (drawingEllipse == true) {

          ellipse.setFrameFromDiagonal(start, end);

        }

        if (drawingLine == true) {

          line.setLine(start, end);

        }

      }

      public void mouseExited(MouseEvent e)

      

      public void mouseEntered(MouseEvent e)

      

      public void mouseClicked(MouseEvent e)

      

    });

    addMouseMotionListener(new MouseMotionListener() {

      public void mouseMoved(MouseEvent e)

      

      public void mouseDragged(MouseEvent e) {

        end = e.getPoint();

        if (drawingRectangle == true) {

          rectangle.setFrameFromDiagonal(start, end);

        }

        if (drawingEllipse == true) {

          ellipse.setFrameFromDiagonal(start, end);

        }

        if (drawingLine == true) {

          line.setLine(start, end);

        }

        repaint();

      }

    });

  }

  public void paintComponent(Graphics g) {

    super.paintComponent(g);

    graphics2D = (Graphics2D) g;

    graphics2D.setColor(Color.black);

    if (drawingRectangle == true) {

      graphics2D.draw(rectangle);

    }

    if (drawingEllipse == true) {

      graphics2D.draw(ellipse);

    }

    if (drawingLine == true) {

      graphics2D.draw(line);

    }

  }

  public void setDrawingRectangle(boolean value)

    drawingRectangle = value;

  

  public void setDrawingEllipse(boolean value)

    drawingEllipse = value;

  

  public void setDrawingLine(boolean value)

    drawingLine = value;

  

  public void setFilling(boolean value)

    filling = value;

  

  private static void createAndShowGUI() {

    JFrame frame = new JFrame("Java Draw");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JavaDraw javaDraw = new JavaDraw();

    frame.add(javaDraw);

    frame.setSize(600, 600);

    frame.setVisible(true);

  }

  public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

      public void run() {

        createAndShowGUI();

      }

    });

  }

}

通过学习上述代码,整个Java画图的原理也就清晰明了。当然,该程序可能有一些不足之处,欢迎大家指出。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复