Java實(shí)現(xiàn)簡(jiǎn)單畫畫畫板
用Java實(shí)現(xiàn)簡(jiǎ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ù)組的方式添加按鈕。好處在更改代碼的時(shí)候,可以直接添加,十分方便 ? ? String [] Shape={"直線","曲線","圓","噴槍","橡皮擦","矩形","橢圓","圓角矩形","弧線","圖形"}; ?? ? ? ?? ?for(int i=0;i<Shape.length;i++){ ? ? ?? ??? ?JButton button=new JButton(Shape[i]); ? ? ?? ??? ?button.addActionListener(this); ? ?//添加事件監(jiān)聽機(jī)制 ?類(this)應(yīng)該是有實(shí)現(xiàn)了ActionListener這個(gè)接口的吧; ?? ??? ??? ?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)聽機(jī)制 ? ? ?? ??? ?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); ? ? // 注意:這里鼠標(biāo)移動(dòng)和鼠標(biāo)拖動(dòng)的事件,是作用在p1的面板上面。。。類(this)應(yīng)該是有實(shí)現(xiàn)了MouseListener,MouseMotionListener ? ? p1.addMouseListener(this); ? ? p1.addMouseMotionListener(this); ?? ?} ? ? // 當(dāng)類實(shí)現(xiàn)接口的時(shí)候,類要實(shí)現(xiàn)接口中所有的方法。否則,類必須聲明為抽象的類。對(duì)應(yīng)ActionListener接口 ? ? public void actionPerformed(ActionEvent e){ ?? ? ? ?if(e.getActionCommand().equals("")){ ? ? ?//如果沒有信息,那就是顏色按鈕 ?? ? ? ??? ?JButton button = (JButton) e.getSource(); ?// getSource()事件最初發(fā)生的對(duì)象, ?? ??? ??? ?color = button.getBackground(); ?? ?? ??? ??? ?System.out.println("color = " + color); ?? ? ? ?}else{ ?? ? ? ??? ?JButton button = (JButton) e.getSource(); ? ?? ??? ??? ?shape = button.getActionCommand(); ?? ?? ??? ??? ?System.out.println("String = " + shape); ?? ? ? ?} ?? ?} ?? ? // 當(dāng)類實(shí)現(xiàn)接口的時(shí)候,類要實(shí)現(xiàn)接口中所有的方法。否則,類必須聲明為抽象的類。 ? ? ?// 在組件上按下鼠標(biāo)按鈕時(shí)調(diào)用。 ?? ? public void mousePressed(MouseEvent e) { ?? ??? ? g=(Graphics2D)p1.getGraphics(); // g = p1.getGraphics(); ?? ??? ? g.setColor(color); ?? ??? ? x1=e.getX(); ?// 返回事件相對(duì)于源組件的水平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é)束角度) ?? ??? ? } // 如果想使用這個(gè)圖形,下面的new File("這里要添加自己電腦上的圖片路徑")? ?? ??? ? /*else if (shape.equals("圖形")){ ?? ??? ? ? ? try{ ?? ??? ??? ? ? ? img=ImageIO.read(new File("F:\\學(xué)習(xí)知識(shí)\\Java\\畫畫面板\\imager\\太陽1.bmp")); ?? ?? ??? ? ? ? }? ?? ??? ? ? ? catch(IOException e1){ ?? ??? ??? ? ? ? System.out.println(e.toString()); ?? ??? ? ? ? } ? ? ? ? ? ? ?// drawImage繪制當(dāng)前可用的指定圖像的大小。 該圖像在其圖形上下文的坐標(biāo)空間中的左上角( x , y ,寬,高)處繪制。 ?? ??? ? ? ? g.drawImage(img,x1,y1,150,150,null); ?? ??? ? ? ? }*/ ?? ??? ? System.out.println("x1 = " + x1 +" ? y1 = " + y1); ?? ? } ? ? ?// 在組件上單擊(按下并釋放)鼠標(biāo)按鈕時(shí)調(diào)用。 ?? ? public void mouseClicked(MouseEvent e){ ?? ? } ?? ? // 當(dāng)鼠標(biāo)進(jìn)入組件時(shí)調(diào)用。 ? ? ?public void mouseEntered(MouseEvent e){ ?? ? } ?? ? // 當(dāng)鼠標(biāo)退出組件時(shí)調(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方法在兩個(gè)點(diǎn)之間連一條直線(gr是畫筆) ?? ??? ? } ?? ? } ? ? ?// 在組件上按下鼠標(biāo)按鈕然后拖動(dòng)時(shí)調(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));? ?? ??? ??? ??? ?// 好像是渲染,應(yīng)該就是給涂掉 ?? ??? ??? ??? ?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); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ? } ? ? ?// 當(dāng)鼠標(biāo)光標(biāo)移動(dòng)到組件上但沒有按鈕被按下時(shí)調(diào)用。(就是光標(biāo)放到上面) ?? ? public void mouseMoved(MouseEvent e) { ?? ? } }
二、展示效果
強(qiáng)行解釋:
左上角的那個(gè)太陽是我插入的圖片,就是“圖形”這個(gè)按鈕。但是我把上面的代碼給注釋掉了。各位看官要是想使用“圖形”的話可以直接把注釋去掉。
其實(shí)這個(gè)“”圖形”還可以改進(jìn),搞成點(diǎn)擊“圖形”之后會(huì)專門讓你選擇想要添加的圖片,不過不想搞了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ResultSet如何動(dòng)態(tài)獲取列名和值
這篇文章主要介紹了ResultSet如何動(dòng)態(tài)獲取列名和值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12JVM 的 noverify 啟動(dòng)參數(shù)問題解析
這篇文章主要介紹了JVM 的 noverify 啟動(dòng)參數(shù)問題解析,從 JDK 13 開始及其后續(xù)版本中,不建議繼續(xù)使用?-Xverify:none?和-noverify?參數(shù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05SpringBoot實(shí)現(xiàn)讀取YML,yaml,properties文件
yml,yaml,properties三種文件都是用來存放配置的文件,一些靜態(tài)數(shù)據(jù),配置的數(shù)據(jù)都會(huì)存放到里邊。本文主要為大家整理了SpringBoot實(shí)現(xiàn)讀取YML,yaml,properties文件的方法,需要的可以參考一下2023-04-04基于Java利用static實(shí)現(xiàn)單例模式
這篇文章主要介紹了基于Java利用static實(shí)現(xiàn)單例模式,當(dāng)在多個(gè)線程同時(shí)觸發(fā)類的初始化過程的時(shí)候static不會(huì)被多次執(zhí)行,下面我們一起進(jìn)入文章看看具體要的原因2022-01-01spring mvc4中相關(guān)注解的詳細(xì)講解教程
這篇文章主要給大家介紹了關(guān)于spring mvc4中相關(guān)注解的相關(guān)資料,其中詳細(xì)介紹了關(guān)于@Controller、@RequestMapping、@RathVariable、@RequestParam及@RequestBody等等注解的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06解析MapStruct轉(zhuǎn)換javaBean時(shí)出現(xiàn)的詭異事件
在項(xiàng)目中用到了MapStruct,對(duì)其可以轉(zhuǎn)換JavaBean特別好奇,今天小編給大家分享一個(gè)demo給大家講解MapStruct轉(zhuǎn)換javaBean時(shí)出現(xiàn)的詭異事件,感興趣的朋友一起看看吧2021-09-09spring?boot?validation參數(shù)校驗(yàn)與分組嵌套各種類型及使用小結(jié)
參數(shù)校驗(yàn)基本上是controller必做的事情,畢竟前端傳過來的一切都不可信,validation可以簡(jiǎn)化這一操作,這篇文章主要介紹了spring?boot?validation參數(shù)校驗(yàn)分組嵌套各種類型及使用小結(jié),需要的朋友可以參考下2023-09-09SpringBoot + SpringSecurity 環(huán)境搭建的步驟
這篇文章主要介紹了SpringBoot + SpringSecurity 環(huán)境搭建的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05