黑马程序员 ----java基础----自制一个画图板GUI

自制画图板,欢迎各位大神给点意见。

 

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.*;

class hua extends JFrame implements MouseMotionListener, ActionListener{

    

private JButton red;

private JButton green;

private JButton black;

private JButton eraser;

    private Container con;

    private JButton undo;

    private JButton redo;

    private int x1,y1,x2,y2,line=0,times=0;

private int rubber[] = new int[2];

    hua(){

        super("画图");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        con=this.getContentPane();

        con.setLayout(new FlowLayout());

        this.setBounds(50, 50, 500, 500);

        this.setBackground(Color.yellow);

        inta();       //初始化

        this.addMouseMotionListener(this);

        this.setVisible(true);

    }

    public void inta(){

black = new JButton("黑色");

red = new JButton("红色");

green = new JButton("绿色");

eraser = new JButton("橡皮");

undo = new JButton("Undo");

      redo = new JButton("Redo");

black.addActionListener(this);

green.addActionListener(this);

eraser.addActionListener(this);

red.addActionListener(this);

con.add(red);

con.add(black);

con.add(green);

con.add(eraser);

}

    public void mouseDragged(MouseEvent e){

 

rubber[0] = x2 = e.getX();

rubber[1] = y2 = e.getY();

        repaint();

    }

    public void mouseMoved(MouseEvent e){

        x1=x2=e.getX();

        y1=y2=e.getY();

 

    }

    public void paint(Graphics g){

if (line == 0) {

g.setColor(Color.black);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if (line == 1)

{

g.setColor(Color.red);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if (line == 2){

g.setColor(Color.green);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if(line == 4){

g.setColor(Color.yellow);

g.fillRect(rubber[0] - 3, rubber[1] - 3, 10, 10);

}

    }

    public void actionPerformed(ActionEvent a){

if (black == a.getSource())

line = 0;

else if (red == a.getSource())

line = 1;

else if (green == a.getSource())

line = 2;

else if (eraser == a.getSource())

line = 4;

    }

}

class LoginFrame extends JFrame implements ActionListener{

private Container cont;

private JButton drawa;

private JLabel testa;

public LoginFrame(){

this.setTitle("绘图登陆界面");

this.setSize(300, 300);

this.setLocation(200, 200);

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

addCompent();

this.setVisible(true);

}

public void addCompent(){

cont = this.getContentPane();

        cont.setLayout(null);

        testa = new JLabel("点击进入绘图");

        drawa = new JButton("开始绘图");

        testa.setBounds(90,0,80,100);

        drawa.setBounds(90,100,100,30);

        drawa.addActionListener(this);

        cont.add(testa);

        cont.add(drawa);

}

public void actionPerformed(ActionEvent a){

if(drawa == a.getSource()){

new hua();

this.dispose();

}

}

}

 class drawline{

    public static void main(String[] args){

    new LoginFrame();   

    }

}  

 

 

 

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.*;

class DrawCurves extends JFrame implements MouseMotionListener, ActionListener{

    

private JButton btnRed;

private JButton black;

private JButton eraser;

    private Container cont;

 

    private int x1,y1,x2,y2,line=0,times=0;

private int rubber[] = new int[2];

    DrawCurves(){

        super("画图");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        cont=this.getContentPane();

        cont.setLayout(new FlowLayout());

        this.setBounds(50, 50, 500, 500);

        this.setBackground(Color.yellow);

        inta();       //初始化

        this.addMouseMotionListener(this);

        this.setVisible(true);

    }

    public void inta(){

black = new JButton("黑笔");

btnRed = new JButton("红笔");

 

eraser = new JButton("橡皮擦");

 

black.addActionListener(this);

 

eraser.addActionListener(this);

btnRed.addActionListener(this);

cont.add(black);

cont.add(btnRed);

 

cont.add(eraser);

}

    public void mouseDragged(MouseEvent e){

 

rubber[0] = x2 = e.getX();

rubber[1] = y2 = e.getY();

        repaint();

    }

    public void mouseMoved(MouseEvent e){

        x1=x2=e.getX();

        y1=y2=e.getY();

 

    }

    public void paint(Graphics g){

if (line == 0) {

g.setColor(Color.black);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if (line == 1)

{

g.setColor(Color.red);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if (line == 2){

g.setColor(Color.green);

g.drawLine(x1, y1, x2, y2);

x1 = x2;

y1 = y2;

}

else if(line == 3){

g.setColor(Color.yellow);

g.fillRect(rubber[0] - 3, rubber[1] - 3, 10, 10);

}

    }

    public void actionPerformed(ActionEvent a){

if (black == a.getSource())

line = 0;

else if (btnRed == a.getSource())

line = 1;

 

else if (eraser == a.getSource())

line = 3;

    }

}

class CurvesFrame extends JFrame implements ActionListener{

private Container cont;

private JButton drawa;

 

public CurvesFrame(){

this.setTitle("绘图登陆界面");

this.setSize(300, 300);

this.setLocation(200, 200);

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

addCompent();

this.setVisible(true);

}

public void addCompent(){

cont = this.getContentPane();

        cont.setLayout(null);

          drawa = new JButton("进入绘图");

      

        drawa.setBounds(90,100,100,30);

        drawa.addActionListener(this);

     

        cont.add(drawa);

}

public void actionPerformed(ActionEvent a){

if(drawa == a.getSource()){

new hua();

this.dispose();

}

}

}

 class drawline{

    public static void main(String[] args){

    new CurvesFrame();   

    }

}  

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值