Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板
用Java實(shí)現(xiàn)簡(jiǎn)單的畫(huà)畫(huà)畫(huà)板,供大家參考,具體內(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("畫(huà)畫(huà)面板");
?? ?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)聽(tīng)機(jī)制 ?類(lèi)(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)聽(tīng)機(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的面板上面。。。類(lèi)(this)應(yīng)該是有實(shí)現(xiàn)了MouseListener,MouseMotionListener
? ? p1.addMouseListener(this);
? ? p1.addMouseMotionListener(this);
?? ?}
? ? // 當(dāng)類(lèi)實(shí)現(xiàn)接口的時(shí)候,類(lèi)要實(shí)現(xiàn)接口中所有的方法。否則,類(lèi)必須聲明為抽象的類(lèi)。對(duì)應(yīng)ActionListener接口
? ? public void actionPerformed(ActionEvent e){
?? ? ? ?if(e.getActionCommand().equals("")){ ? ? ?//如果沒(méi)有信息,那就是顏色按鈕
?? ? ? ??? ?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)類(lèi)實(shí)現(xiàn)接口的時(shí)候,類(lèi)要實(shí)現(xiàn)接口中所有的方法。否則,類(lèi)必須聲明為抽象的類(lèi)。
? ? ?// 在組件上按下鼠標(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\\畫(huà)畫(huà)面板\\imager\\太陽(yáng)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){
?? ? }?
? ? ?// 松開(kāi)。搭配前面的按下,就可以畫(huà)出直線
?? ? public void mouseReleased(MouseEvent e){
?? ??? ? g.setColor(color);
?? ??? ? if(shape.equals("直線")){
?? ??? ??? ? x2 = e.getX();
?? ??? ? ? ? y2 = e.getY();
?? ??? ??? ? g.drawLine(x1, y1, x2, y2); ? //通過(guò)drawLine方法在兩個(gè)點(diǎn)之間連一條直線(gr是畫(huà)筆)
?? ??? ? }
?? ? }
? ? ?// 在組件上按下鼠標(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)到組件上但沒(méi)有按鈕被按下時(shí)調(diào)用。(就是光標(biāo)放到上面)
?? ? public void mouseMoved(MouseEvent e) {
?? ? }
}二、展示效果

強(qiáng)行解釋?zhuān)?/p>
左上角的那個(gè)太陽(yáng)是我插入的圖片,就是“圖形”這個(gè)按鈕。但是我把上面的代碼給注釋掉了。各位看官要是想使用“圖形”的話可以直接把注釋去掉。
其實(shí)這個(gè)“”圖形”還可以改進(jìn),搞成點(diǎn)擊“圖形”之后會(huì)專(zhuān)門(mén)讓你選擇想要添加的圖片,不過(guò)不想搞了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成JWT令牌詳細(xì)說(shuō)明
這篇文章主要介紹了SpringBoot集成JWT令牌詳細(xì)說(shuō)明,JWT方式校驗(yàn)方式更加簡(jiǎn)單便捷化,無(wú)需通過(guò)redis緩存,而是直接根據(jù)token取出保存的用戶(hù)信息,以及對(duì)token可用性校驗(yàn),單點(diǎn)登錄,驗(yàn)證token更為簡(jiǎn)單,需要的朋友可以參考下2023-10-10
Javaweb開(kāi)發(fā)環(huán)境Myeclipse6.5 JDK1.6 Tomcat6.0 SVN1.8配置教程
這篇文章主要介紹了Javaweb開(kāi)發(fā)環(huán)境Myeclipse6.5 JDK1.6 Tomcat6.0 SVN1.8配置教程,感興趣的小伙伴們可以參考一下2016-06-06
在Eclipse安裝Spring boot插件的步驟(圖文)
這篇文章主要介紹了在Eclipse安裝Spring boot插件的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
詳解mybatis通過(guò)mapper接口加載映射文件
本篇文章主要介紹了mybatis通過(guò)mapper接口加載映射文件 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Jackson反序列化@JsonFormat 不生效的解決方案
這篇文章主要介紹了Jackson反序列化@JsonFormat 不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot實(shí)戰(zhàn)權(quán)限管理功能圖文步驟附含源碼
這篇文章主要為大家介紹了springboot實(shí)戰(zhàn)權(quán)限管理功能圖文步驟及示例源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

