Java實現(xiàn)簡單畫畫畫板
用Java實現(xiàn)簡單的畫畫畫板,供大家參考,具體內(nèi)容如下
一、代碼
先直接上代碼吧,備注大部分都在代碼中。
import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; import javax.swing.event.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.awt.Graphics; public class DrawDraw extends JFrame implements ActionListener,MouseListener,MouseMotionListener{ ?? ?public static void main(String[] args) { ?? ??? ?new DrawDraw(); ?? ?} ? ? // 屬性 ?? ?JPanel p0,p1,p2; ?? ?Color color; ?? ?String shape; ?? ?int x1,y1,x2,y2,newx1,newy1,newx2,newy2; ?? ?Graphics2D g; ?? ?BufferedImage img; ?? ?boolean flag; ?? ?DrawDraw(){ ?? ?p0 = new JPanel(); ?? ?p1 = new JPanel(); ?? ?p2 = new JPanel(); ? ? setTitle("畫畫面板"); ?? ?this.setSize(1400,900); ?? ?this.setLocation(100,100); ?? ?// 圖形按鈕,采用數(shù)組的方式添加按鈕。好處在更改代碼的時候,可以直接添加,十分方便 ? ? String [] Shape={"直線","曲線","圓","噴槍","橡皮擦","矩形","橢圓","圓角矩形","弧線","圖形"}; ?? ? ? ?? ?for(int i=0;i<Shape.length;i++){ ? ? ?? ??? ?JButton button=new JButton(Shape[i]); ? ? ?? ??? ?button.addActionListener(this); ? ?//添加事件監(jiān)聽機制 ?類(this)應該是有實現(xiàn)了ActionListener這個接口的吧; ?? ??? ??? ?p0.add(button); ? ? ?? ?} ? ? // 顏色按鈕 ?? ?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(this); ? ? //添加事件監(jiān)聽機制 ? ? ?? ??? ?button.setPreferredSize(new Dimension(40,40)); ?// 設(shè)置按鈕的大小 ? ? ?? ??? ?button.setBackground(color[i]); ? ? // 設(shè)置顏色選擇按鈕的顏色 ? ?? ? ? ?? ??? ?p2.add(button); ? ? ?? ?} ? ? // 設(shè)置背景顏色 ?? ?p0.setBackground(Color.gray);? ?? ?p1.setBackground(Color.white); ?? ?p2.setBackground(Color.yellow);? ?? ?// 把p0,p1,p2 上-中-下的方法分配 ?? ?this.add(p0,BorderLayout.NORTH); ?? ?this.add(p1,BorderLayout.CENTER); ?? ?this.add(p2,BorderLayout.SOUTH); ?? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);? ?? ?this.setVisible(true); ? ? // 注意:這里鼠標移動和鼠標拖動的事件,是作用在p1的面板上面。。。類(this)應該是有實現(xiàn)了MouseListener,MouseMotionListener ? ? p1.addMouseListener(this); ? ? p1.addMouseMotionListener(this); ?? ?} ? ? // 當類實現(xiàn)接口的時候,類要實現(xiàn)接口中所有的方法。否則,類必須聲明為抽象的類。對應ActionListener接口 ? ? public void actionPerformed(ActionEvent e){ ?? ? ? ?if(e.getActionCommand().equals("")){ ? ? ?//如果沒有信息,那就是顏色按鈕 ?? ? ? ??? ?JButton button = (JButton) e.getSource(); ?// getSource()事件最初發(fā)生的對象, ?? ??? ??? ?color = button.getBackground(); ?? ?? ??? ??? ?System.out.println("color = " + color); ?? ? ? ?}else{ ?? ? ? ??? ?JButton button = (JButton) e.getSource(); ? ?? ??? ??? ?shape = button.getActionCommand(); ?? ?? ??? ??? ?System.out.println("String = " + shape); ?? ? ? ?} ?? ?} ?? ? // 當類實現(xiàn)接口的時候,類要實現(xiàn)接口中所有的方法。否則,類必須聲明為抽象的類。 ? ? ?// 在組件上按下鼠標按鈕時調(diào)用。 ?? ? public void mousePressed(MouseEvent e) { ?? ??? ? g=(Graphics2D)p1.getGraphics(); // g = p1.getGraphics(); ?? ??? ? g.setColor(color); ?? ??? ? x1=e.getX(); ?// 返回事件相對于源組件的水平x位置。 ?? ??? ? y1=e.getY(); ?? ??? ?if(shape.equals("圓")){ ?? ??? ??? ? g.drawOval(x1, y1, 30, 30); ?? ??? ? }else if(shape.equals("矩形")){ ?? ??? ??? ? g.drawRect(x1, y1, 30, 40); ?? ??? ? }else if(shape.equals("圓角矩形")){ ?? ??? ??? ? g.drawRoundRect(x1, y1, 30, 40, 5, 10); ?? ??? ? }else if(shape.equals("橢圓")){ ?? ??? ??? ? g.drawOval(x1, y1, 30, 20); ?? ??? ? }else if(shape.equals("弧線")){ ?? ??? ??? ? g.drawArc(x1, y1, 100, 80, 10, 180); ?//(x,y,寬,高,起始角度,結(jié)束角度) ?? ??? ? } // 如果想使用這個圖形,下面的new File("這里要添加自己電腦上的圖片路徑")? ?? ??? ? /*else if (shape.equals("圖形")){ ?? ??? ? ? ? try{ ?? ??? ??? ? ? ? img=ImageIO.read(new File("F:\\學習知識\\Java\\畫畫面板\\imager\\太陽1.bmp")); ?? ?? ??? ? ? ? }? ?? ??? ? ? ? catch(IOException e1){ ?? ??? ??? ? ? ? System.out.println(e.toString()); ?? ??? ? ? ? } ? ? ? ? ? ? ?// drawImage繪制當前可用的指定圖像的大小。 該圖像在其圖形上下文的坐標空間中的左上角( x , y ,寬,高)處繪制。 ?? ??? ? ? ? g.drawImage(img,x1,y1,150,150,null); ?? ??? ? ? ? }*/ ?? ??? ? System.out.println("x1 = " + x1 +" ? y1 = " + y1); ?? ? } ? ? ?// 在組件上單擊(按下并釋放)鼠標按鈕時調(diào)用。 ?? ? public void mouseClicked(MouseEvent e){ ?? ? } ?? ? // 當鼠標進入組件時調(diào)用。 ? ? ?public void mouseEntered(MouseEvent e){ ?? ? } ?? ? // 當鼠標退出組件時調(diào)用。 ?? ? public void mouseExited(MouseEvent e){ ?? ? }? ? ? ?// 松開。搭配前面的按下,就可以畫出直線 ?? ? public void mouseReleased(MouseEvent e){ ?? ??? ? g.setColor(color); ?? ??? ? if(shape.equals("直線")){ ?? ??? ??? ? x2 = e.getX(); ?? ??? ? ? ? y2 = e.getY(); ?? ??? ??? ? g.drawLine(x1, y1, x2, y2); ? //通過drawLine方法在兩個點之間連一條直線(gr是畫筆) ?? ??? ? } ?? ? } ? ? ?// 在組件上按下鼠標按鈕然后拖動時調(diào)用。 ?? ? public void mouseDragged(MouseEvent e){ ? ? ? ? ? ? x2 = e.getX(); ?? ??? ??? ?y2 = e.getY(); ?? ??? ??? ?if (shape.equals("曲線")) { ?? ??? ??? ??? ?g.drawLine(x1, y1, x2, y2); ?? ??? ??? ??? ?x1 = x2; ?? ??? ??? ??? ?y1 = y2; ?? ??? ??? ?}else if(shape.equals("橡皮擦")){ ?? ??? ??? ??? ?// Graphics2D中的方法。BasicStroke(float width)--->指的是寬度 ?? ??? ??? ??? ?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("噴槍")){ ?? ??? ??? ??? ?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); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ? } ? ? ?// 當鼠標光標移動到組件上但沒有按鈕被按下時調(diào)用。(就是光標放到上面) ?? ? public void mouseMoved(MouseEvent e) { ?? ? } }
二、展示效果
強行解釋:
左上角的那個太陽是我插入的圖片,就是“圖形”這個按鈕。但是我把上面的代碼給注釋掉了。各位看官要是想使用“圖形”的話可以直接把注釋去掉。
其實這個“”圖形”還可以改進,搞成點擊“圖形”之后會專門讓你選擇想要添加的圖片,不過不想搞了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實現(xiàn)讀取YML,yaml,properties文件
yml,yaml,properties三種文件都是用來存放配置的文件,一些靜態(tài)數(shù)據(jù),配置的數(shù)據(jù)都會存放到里邊。本文主要為大家整理了SpringBoot實現(xiàn)讀取YML,yaml,properties文件的方法,需要的可以參考一下2023-04-04解析MapStruct轉(zhuǎn)換javaBean時出現(xiàn)的詭異事件
在項目中用到了MapStruct,對其可以轉(zhuǎn)換JavaBean特別好奇,今天小編給大家分享一個demo給大家講解MapStruct轉(zhuǎn)換javaBean時出現(xiàn)的詭異事件,感興趣的朋友一起看看吧2021-09-09spring?boot?validation參數(shù)校驗與分組嵌套各種類型及使用小結(jié)
參數(shù)校驗基本上是controller必做的事情,畢竟前端傳過來的一切都不可信,validation可以簡化這一操作,這篇文章主要介紹了spring?boot?validation參數(shù)校驗分組嵌套各種類型及使用小結(jié),需要的朋友可以參考下2023-09-09SpringBoot + SpringSecurity 環(huán)境搭建的步驟
這篇文章主要介紹了SpringBoot + SpringSecurity 環(huán)境搭建的步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05