欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)的簡單畫圖板示例

 更新時間:2018年08月24日 10:52:57   作者:SX_csu2016sw  
這篇文章主要介紹了Java實現(xiàn)的簡單畫圖板,涉及java使用swing組件進(jìn)行圖形繪制相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Java實現(xiàn)的簡單畫圖板。分享給大家供大家參考,具體如下:

這個畫圖板是我好久之前做的,之后浙大的同學(xué)需要做課設(shè)然后就花了一點時間將它改了一下,變得簡單些能夠方便擴充功能,同時學(xué)習(xí)java基礎(chǔ)

先截圖一下吧,就可以知道有哪些功能了~

三個分區(qū),上面選擇圖形,下面選擇顏色,立體圓就是一個分形,也先放著不需要的同學(xué)可以注釋了它

代碼很簡單,就是JPanel進(jìn)行分區(qū),得到畫筆,同時使用畫圖的函數(shù)就可以做到了

貼代碼應(yīng)該很快就會了~

主類

package awtDemo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawMain extends JPanel {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DrawMain Draw = new DrawMain();
        Draw.InitUI();
    }
    public void InitUI() {
        JFrame jf = new JFrame();
        jf.setSize(1000, 780);
        jf.setTitle("簡單畫板");
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setLayout(new BorderLayout());
        // 實例化事件監(jiān)聽類
        DrawListener dl = new DrawListener(this);
        // 實現(xiàn)中間面板
        this.setBackground(Color.WHITE);
        jf.add(this, BorderLayout.CENTER);
        // 實現(xiàn)性狀面板
        JPanel ShapePanel = new JPanel();
        ShapePanel.setBackground(Color.black);
        ShapePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        ShapePanel.setBackground(Color.gray);
        ;
        String[] Shape = { "直線", "曲線", "圓", "噴槍", "橡皮擦", "矩形", "橢圓", "圓角矩形",
                "弧線", "多邊形", "圖形", "三角形", "立體圓", };
        for (int i = 0; i < Shape.length; i++) {
            JButton button = new JButton(Shape[i]);
            button.setBackground(Color.WHITE);
            button.addActionListener(dl); // 添加事件監(jiān)聽機制
            ShapePanel.add(button);
        }
        jf.add(ShapePanel, BorderLayout.NORTH);
        // 實現(xiàn)顏色面板
        JPanel ColorPanel = new JPanel();
        ColorPanel.setBackground(Color.black);
        ColorPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        ColorPanel.setBackground(Color.gray);
        ;
        Color[] color = { Color.BLACK, Color.blue, Color.white, Color.gray,
                Color.red, Color.CYAN, Color.green, Color.darkGray, Color.pink };
        for (int i = 0; i < color.length; i++) {
            JButton button = new JButton();
            button.addActionListener(dl); // 添加事件監(jiān)聽機制
            button.setPreferredSize(new Dimension(30, 30));
            button.setBackground(color[i]);
            ColorPanel.add(button);
        }
        jf.add(ColorPanel, BorderLayout.SOUTH);
        jf.setVisible(true);
        this.addMouseListener(dl);
        this.addMouseMotionListener(dl);
    }
}

監(jiān)聽輔助類

package awtDemo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
public class DrawListener extends MouseAdapter implements ActionListener {
    private int x1, y1, x2, y2;
    private int newx1, newy1, newx2, newy2;
    private Graphics2D g;
    private DrawMain df;
    private boolean flag = false;
    String shape = "直線";
    Color color;
    private int[] arrx = new int[4];
    private int[] arry = new int[4];
    private int temp = 0;
    DrawListener(DrawMain d) {
        df = d;
    }
    // 獲取形狀和顏色
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("")) {
            JButton button = (JButton) e.getSource();
            color = button.getBackground();
            System.out.println("color = " + color);
        } else {
            JButton button = (JButton) e.getSource();
            shape = button.getActionCommand();
            System.out.println("String = " + shape);
        }
    }
    // 實現(xiàn)畫筆
    public void mousePressed(MouseEvent e) {
        g = (Graphics2D) df.getGraphics();
        g.setColor(color);
        x1 = e.getX();
        y1 = e.getY();
    }
    public void mouseReleased(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        if (shape.equals("直線")) {
            g.drawLine(x1, y1, x2, y2);
        } else if (shape.equals("弧線")) {
            g.drawArc(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1), 0, 180);
        } else if (shape.equals("多邊形") && !flag) {
            g.drawLine(x1, y1, x2, y2);
            newx1 = x1;
            newy1 = y1;
            newx2 = x2;
            newy2 = y2;
            flag = true;
        } else if (shape.equals("圓")) {
            g.drawOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
        } else if (shape.equals("矩形")) {
            g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
        } else if (shape.equals("圓角矩形")) {
            g.drawRoundRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1), 2, 10);
        } else if (shape.equals("橢圓")) {
            g.drawOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
        }
    }
    public void mouseClicked(MouseEvent e) {
        if (shape.equals("多邊形") && flag) {
            x2 = e.getX();
            y2 = e.getY();
            if (e.getClickCount() == 2) {
                g.drawLine(newx1, newy1, newx2, newy2);
                flag = false;
            }
            g.drawLine(newx2, newy2, x2, y2);
            newx2 = x2;
            newy2 = y2;
        } else if (shape.equals("圖形")) {
            arrx[temp] = e.getX();
            arry[temp] = e.getY();
            temp++;
            if (temp == 4) {
                int x = arrx[3];
                int y = arry[3];
                for (int i = 0; i <= 10000; i++) {
                    Random ran = new Random();
                    int k = ran.nextInt(3);
                    x = (x + arrx[k]) / 2;
                    y = (y + arry[k]) / 2;
                    g.drawLine(x, y, x, y);
                }
                temp = 0;
            }
        } else if (shape.equals("立體圓")) {
            // double a=-2,b=-2,c=-1.2,d=2;
            double a = 1.40, b = 1.56, c = 1.40, d = -6.56;
            double x = 0, xo = 0;
            double y = 0, yo = 0;
            Color[] Col = { Color.BLUE, Color.cyan, Color.green, Color.magenta,
                    Color.red, Color.yellow };
            for (int i = 0; i <= 90000; i++) {
                Random r = new Random(); // 增加顏色
                int R = r.nextInt(Col.length);
                g.setColor(Col[R]);
                // x=Math.sin(a*yo)-Math.cos(b*xo);
                // y=Math.sin(c*xo)-Math.cos(d*yo);
                x = d * Math.sin(a * xo) - Math.sin(b * yo);
                y = c * Math.cos(a * xo) + Math.cos(b * yo);
                int temp_x = (int) (x * 50);
                int temp_y = (int) (y * 50);
                g.drawLine(temp_x + 500, temp_y + 300, temp_x + 500,
                        temp_y + 300);
                xo = x;
                yo = y;
            }
        } else if (shape.equals("三角形")) {
            double a = -2, b = -2, c = -1.2, d = 2;
            double x = 0, xo = 0;
            double y = 0, yo = 0;
            Color[] Col = { Color.BLUE, Color.cyan, Color.green, Color.magenta,
                    Color.red, Color.yellow };
            for (int i = 0; i <= 90000; i++) {
                Random r = new Random(); // 增加顏色
                int R = r.nextInt(Col.length);
                g.setColor(Col[R]);
                x = Math.sin(a * yo) - Math.cos(b * xo);
                y = Math.sin(c * xo) - Math.cos(d * yo);
                int temp_x = (int) (x * 50);
                int temp_y = (int) (y * 50);
                g.drawLine(temp_x + 500, temp_y + 300, temp_x + 500,
                        temp_y + 300);
                xo = x;
                yo = y;
            }
        }
    }
    public void mouseDragged(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        if (shape.equals("曲線")) {
            // g.setStroke(new BasicStroke(10));
            // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            // RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
        } else if (shape.equals("橡皮擦")) {
            g.setStroke(new BasicStroke(80));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(Color.WHITE);
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
        } else if (shape.equals("噴槍")) {
            // g.setStroke(new BasicStroke(2)); //不用加粗
            // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            // RenderingHints.VALUE_ANTIALIAS_ON);
            for (int k = 0; k < 20; k++) {
                Random i = new Random();
                int a = i.nextInt(8);
                int b = i.nextInt(10);
                g.drawLine(x2 + a, y2 + b, x2 + a, y2 + b);
            }
        }
    }
}

代碼量也還是挺小的,因為是簡單畫板嘛~~

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • SpringBoot文件上傳功能的實現(xiàn)方法

    SpringBoot文件上傳功能的實現(xiàn)方法

    這篇文章主要介紹了SpringBoot文件上傳功能的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Spring中的異步方法@Async失效的原因詳解

    Spring中的異步方法@Async失效的原因詳解

    這篇文章主要介紹了Spring中的異步方法@Async失效的原因詳解,@Async屬于異步注解,@Async放在方法上標(biāo)識該方法為異步方法,異步是指進(jìn)程不需要一直等待下去,而是繼續(xù)執(zhí)行下面的操作,不管其他進(jìn)程的狀態(tài),需要的朋友可以參考下
    2024-01-01
  • Java class文件格式總結(jié)_動力節(jié)點Java學(xué)院整理

    Java class文件格式總結(jié)_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了Java class文件格式總結(jié)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-06-06
  • mybatis-plus報錯net.sf.jsqlparser.statement.select.SelectBody的問題解決

    mybatis-plus報錯net.sf.jsqlparser.statement.select.SelectBody的

    本文主要介紹了mybatis-plus報錯net.sf.jsqlparser.statement.select.SelectBody的問題解決,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • spring-retry簡單使用方法

    spring-retry簡單使用方法

    這篇文章主要介紹了spring-retry簡單使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Java多線程ThreadPoolExecutor詳解

    Java多線程ThreadPoolExecutor詳解

    這篇文章主要介紹了Java多線程ThreadPoolExecutor詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • springboot jpa 延遲加載問題的2種解決

    springboot jpa 延遲加載問題的2種解決

    這篇文章主要介紹了springboot jpa 延遲加載問題的2種解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot+SpringSecurity+jwt實現(xiàn)驗證

    SpringBoot+SpringSecurity+jwt實現(xiàn)驗證

    本文主要介紹了SpringBoot+SpringSecurity+jwt實現(xiàn)驗證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Spring AOP失效的常見場景分析

    Spring AOP失效的常見場景分析

    Spring的AOP(面向切面編程)是一種強大的技術(shù),用于在應(yīng)用程序中實現(xiàn)橫切關(guān)注點的模塊化,雖然Spring的AOP在大多數(shù)情況下都是有效的,但在某些場景下可能會失效,下面來分析Spring AOP失效的常見場景,需要的朋友可以參考下
    2024-01-01
  • 詳解Java中Iterable與Iterator用法

    詳解Java中Iterable與Iterator用法

    在本文中小編給大家分享了關(guān)于Java中Iterable與Iterator的用法知識點內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2018-10-10

最新評論