您的位置:首页 > 移动开发 > 微信开发

Java画图小程序

2009-06-20 00:57 204 查看
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;//这个类实现图像的打开和保存

public class Huatu extends JPanel
implements MouseListener, MouseMotionListener, ActionListener
{
private JFrame f,f1;
private int xBegin=0,yBegin=0,xEnd=0,yEnd=0;//开始和结束点坐标
private JButton butLine,butRect,butOval,butPen,butEraser;//线,方,圆
private JToolBar tb;
private int i = 0;//0,线;1,方;2,圆;3,笔;
//最后的图形要保存下来,使用缓冲图像
private BufferedImage bi;
private Graphics gg;//图像专用画笔
private JFileChooser jfc;
private JButton butSave,butOpen,butAbout;
private JButton butColor;//选择颜色
private Color myColor;
private Label label1;
public Huatu()
{
//BufferedImage.TYPE_INT_RGB 图像类型 标准的三基色
bi = new BufferedImage

(800,600,BufferedImage.TYPE_INT_RGB);
f = new JFrame("Yizero画图板");
butLine = new JButton("\");
butRect = new JButton("□");
butOval = new JButton("○");
butAbout = new JButton("关于");
butSave = new JButton("Save");
butOpen = new JButton("Open");
tb = new JToolBar();
jfc= new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
jfc.setFileFilter(filter);

butColor = new JButton(" ");
myColor = Color.BLACK;//默认颜色黑色
butColor.setBackground(Color.RED);
//因为缓冲图像是刚创建的,所以默认颜色是黑色
//得到图像专用画笔
gg = bi.getGraphics();
gg.setColor(Color.white);//将画笔调整为白色
gg.fillRect(0,0,800,600);//将图像涂白
gg.setColor(Color.BLACK);//将画笔从新调成黑色
tb.add(butLine);
tb.add(butRect);
tb.add(butOval);
tb.add(butOpen);
tb.add(butSave);
tb.add(butColor);
tb.add(butAbout);
butLine.addActionListener(this);
butRect.addActionListener(this);
butOval.addActionListener(this);
butAbout.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) { About_me(); }
});

butOpen.addActionListener(this);
butSave.addActionListener(this);
butColor.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(tb,BorderLayout.NORTH);
this.addMouseListener(this);
this.addMouseMotionListener(this);
f.add(this);
f.setBounds(100,100,800,600);
f.setVisible(true);

f1=new JFrame("关于作者");

setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
f1.add(new Label(" 此程序由Yizero编写, 如有任何问题欢迎与Yizero联系 邮箱:yizero@qq.com.com"));
f1.setLayout(new GridLayout(1,1));

f1.setFont(new Font("宋体",15,15));

f1.pack();
f1.setBounds(170,250,670,100);
f1.setResizable(false);
f1.show();
f1.setVisible(false);
f1.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e)

{
f1.setVisible(false);
}
});
}

public void About_me()
{
f1.setVisible(true);
}

public void paintComponent(Graphics g)
{
//设置画笔颜色
g.setColor(myColor);
//手工强制清空面板
super.paintComponent(g);
//为了能够看到最终效果,所以将缓冲图像也画到面板上
g.drawImage(bi,0,0,null);
if(i==0)//line
g.drawLine(xBegin,yBegin,xEnd,yEnd);
else //其他图形需要判断起始点和宽高
{
int x,y,width,height;
if(xBegin>xEnd) x = xEnd;
else x = xBegin;
if(yBegin>yEnd) y = yEnd;
else y = yBegin;
width = Math.abs(xBegin-xEnd);//abs取绝对值
height= Math.abs(yBegin-yEnd);
if(i==1)//rect
g.drawRect(x,y,width,height);
else if(i==2)//oval
g.drawOval(x,y,width,height);
}
}

public static void main(String args[])
{
new Huatu();


}
public void mouseClicked(MouseEvent e) {

}
public void mousePressed(MouseEvent e)
{//按下鼠标的时候,记录开始点的坐标
this.xBegin = e.getX();
this.yBegin = e.getY();
}
public void mouseReleased(MouseEvent e)
{//松开鼠标,确认最终的图形需要保存在缓冲图像上
if(i==0)
gg.drawLine(xBegin,yBegin,xEnd,yEnd);
else //其他图形需要判断起始点和宽高
{
int x,y,width,height;
if(xBegin>xEnd) x = xEnd;
else x = xBegin;
if(yBegin>yEnd) y = yEnd;
else y = yBegin;
width = Math.abs(xBegin-xEnd);//abs取绝对值
height= Math.abs(yBegin-yEnd);
if(i==1)//rect
gg.drawRect(x,y,width,height);
else if(i==2)//oval
gg.drawOval(x,y,width,height);
}
}
public void mouseDragged(MouseEvent e)
{//记录拖动的时候的结束点
this.xEnd = e.getX();
this.yEnd = e.getY();



//每次拖动,都需要重绘面板,显示最的图形
this.repaint();
}
public void mouseEntered(MouseEvent e) {

}
public void mouseExited(MouseEvent e) {

}

public void mouseMoved(MouseEvent e) {

}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==this.butLine)//线
{
i = 0;
}
else if(e.getSource()==this.butRect)//方
{
i = 1;
}
else if(e.getSource()==this.butOval)//圆
{
i = 2;
}

else if(e.getSource()==this.butOpen)//打开
{
int i = jfc.showOpenDialog(this.f);
if(i==JFileChooser.APPROVE_OPTION)
{
try
{//将文件图像加载到内存中
bi = ImageIO.read(jfc.getSelectedFile());
//更换图像以后,画笔也必须更换为新的画笔
gg = bi.getGraphics();
//画笔默认白色
gg.setColor(myColor);
//为了防止打开图片以后,出现一个乱图,处理一下
this.xBegin = 100;
this.yBegin = 0;
this.xEnd = 0;
this.yEnd = 0;
this.repaint();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else if(e.getSource()==this.butSave)//保存
{
int i = jfc.showSaveDialog(this.f);
if(i==JFileChooser.APPROVE_OPTION)
{
try
{//保存图像
//参数 缓冲图像 类型 文件所在位置
ImageIO.write(bi,"jpg",jfc.getSelectedFile());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else if(e.getSource()==butColor)//选择颜色
{
myColor = JColorChooser.showDialog(f,"选择你喜欢的颜色",myColor);
gg.setColor(myColor);
}
}
}







注:本Java小程序由"Music-战车"提供源码,经过我的修改,如有问题欢迎和我联系。邮箱:yizero@qq.com



see Yizero by yizero.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: