java画图板保存图片_用java写的简易画图板(可以用作对图片签名,呵呵)

这段程序是我的一个项目里的一段,原本设计的目的是实现对图片进行个性化手写签名,实现这样的方式有很多,但是由于这个项目是全触摸屏设备使用,没有鼠标和键盘,所以就这样设计了。基本思想是将图片作为背景,然后以手写板的方式直接绘制,然后生成新的图片,呵呵!可能有点小题大作,如果大家有什么别的方式实现,欢迎留言指教,本人虚心学习中。。。

程序代码如下:

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class TestBackground implements MouseMotionListener,ActionListener {

/**

* 项目笔记

*/

static final int r=3;

static int ox, oy;

static int flag=1;//颜色控制

public static void main(String[] args) {

JFrame f = new JFrame();

JPanel panel = new JPanel() {

ImageIcon  img = new ImageIcon("edit.jpg");

public void paintComponent(Graphics g) {

g.drawImage(img.getImage(), 0,40, this);

super.paintComponent(g);

}

};

panel.setLayout(new FlowLayout());

JButton jButton1 = new JButton("红");

JButton jButton2 = new JButton("黑");

JButton jButton3 = new JButton("绿");

JButton jButton4 = new JButton("保存");

JButton jButton5 = new JButton("退出");

panel.setOpaque(false);

panel.add(jButton1);

panel.add(jButton2);

panel.add(jButton3);

panel.add(jButton4);

panel.add(jButton5);

jButton1.addActionListener(new  TestBackground());

jButton2.addActionListener(new  TestBackground());

jButton3.addActionListener(new  TestBackground());

jButton4.addActionListener(new  TestBackground());

panel.addMouseMotionListener(new  TestBackground());

f.setContentPane(panel);

f.setSize(800, 600);

f.setVisible(true);

}

public void mouseDragged(MouseEvent e) { Container c=(Container)e.getSource();    Graphics g=c.getGraphics();     if (oy>=40) {     if(flag==1){      g.setColor(new Color(168,0,255));      g.fillRect(ox, oy, 3, 3);}     else if(flag==2){      g.setColor(new Color(0,0,0));     g.fillRect(ox, oy, 3, 3);     }     else{      g.setColor(new Color(0,255,0));       g.fillRect(ox, oy, 3, 3);    }}    ox=e.getX();oy=e.getY(); }public void mouseMoved(MouseEvent e) {    ox=-1;oy=-1; }public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("红")){      flag=1; }     else if (e.getActionCommand().equals("黑")){      flag=2;}     else if (e.getActionCommand().equals("绿")){      flag=3;}     else if (e.getActionCommand().equals("退出"))      System.exit(0);}}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值