锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*;
public class mouse implements ActionListener, WindowListener, MouseMotionListener, MouseListener
{
Frame f;
TextField t=new TextField();
int j;
int x,y;
Graphics g,g_in;
int x1,y1;
BufferedImage i;
Panel p1=new Panel(new GridLayout(2,3));
Button b1=new Button("綰㈣壊");
Button b2=new Button("榛勮壊");
Button b3=new Button("钃濊壊");
Button b4=new Button("緇胯壊");
Button b5=new Button("鐭╁艦");
Button b6=new Button("鍦嗗艦");
mouse()
{
t.setEnabled(false);
f=new Frame("鐢誨浘錛?);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
f.add(t,BorderLayout.NORTH);
f.add(p1,BorderLayout.EAST);
f.setSize(400,400);
int w=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int h=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
f.setLocation((w-400)/2,(h-400)/2);
f.setBackground(Color.ORANGE);
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(this);
f.addMouseListener(this);
f.addMouseMotionListener(this);
this.getG();
}
public void getG()
{
f.addMouseListener(this);
this.g=f.getGraphics();
i=new BufferedImage(f.getWidth(),f.getHeight(),BufferedImage.TYPE_INT_RGB);
g_in=i.getGraphics();
g_in.setColor(Color.white);
g_in.fill3DRect(0,0,i.getWidth(),i.getHeight(),true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
g.setColor(Color.red);
g_in.setColor(Color.red);
}
else if(e.getSource()==b2)
{
g.setColor(Color.yellow);
g_in.setColor(Color.yellow);
}
else if(e.getSource()==b3)
{
g.setColor(Color.blue);
g_in.setColor(Color.blue);
}
else if(e.getSource()==b4)
{
g.setColor(Color.green);
g_in.setColor(Color.green);
}
else if(e.getSource()==b5)
{
j=0;
}
else if(e.getSource()==b6)
{
j=1;
}
}
public static void main(String args[])
{
mouse my=new mouse();
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
try
{
ByteArrayOutputStream is=new ByteArrayOutputStream();//鍒涘緩涓涓瓧鑺傜被鍨嬫暟鎹殑鏁版嵁鍖?BR>
JPEGImageEncoder coder=JPEGCodec.createJPEGEncoder(is);
//澹版槑姝ゆ暟鎹尯鍙兘淇濆瓨JPG鏍煎紡鐨勬暟鎹?BR>
coder.encode(i);
//浠庡唴瀛榠鍥劇墖涓彁鍙朖PG鏍煎紡鏁版嵁錛屼繚瀛樺埌鏁版嵁鍖?BR>
FileOutputStream fo=new FileOutputStream("a.jpg");
byte[] b=is.toByteArray();//灝嗘暟鎹彁鍙栨垚瀛楄妭鏁扮粍
fo.write(b);//灝嗘暟鎹啓鍏.jpg鏂囦歡涓?BR> fo.close();//鍏抽棴鏂囦歡鐨勫啓鍏?BR>
f.dispose();
JOptionPane.showMessageDialog(null,"鍥劇墖宸蹭繚瀛樹負(fù)a.jpg錛岃瀵熺湅錛?);
}catch(Exception el){}
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void mouseDragged(MouseEvent e) {
// TODO: Add your code here
}
public void mouseMoved(MouseEvent e) {
t.setText("X:"+e.getX()+"Y:"+e.getY());
}
public void mouseClicked(MouseEvent e) {
// TODO: Add your code here
}
public void mousePressed(MouseEvent e) {
x=e.getX();y=e.getY();
}
public void mouseReleased(MouseEvent e) {
x1=e.getX();y1=e.getY();
if (j==1)
{
if(x1>=x)
{
if(y1>=y)
{
g.fillOval(x,y,x1-x,y1-y);
g_in.fillOval(x,y,x1-x,y1-y);
}
else if(y1<y)
{
g.fillOval(x,y1,x1-x,y-y1);
g_in.fillOval(x,y1,x1-x,y-y1);
}
}
else if(x1<x)
{
if(y1<y)
{
g_in.fillOval(x1,y1,x-x1,y-y1);
g.fillOval(x1,y1,x-x1,y-y1);
}
else if(y1>=y)
{
g.fillOval(x1,y,x-x1,y1-y);
g_in.fillOval(x1,y,x-x1,y1-y);
}
}}
else if(j==0)
{
if(x1>=x)
{
if(y1>=y)
{
g.fillRect(x,y,x1-x,y1-y);
g_in.fillRect(x,y,x1-x,y1-y);
}
else if(y1<y)
{
g.fillRect(x,y1,x1-x,y-y1);
g_in.fillRect(x,y1,x1-x,y-y1);
}
}
else if(x1<x)
{
if(y1<y)
{
g.fillRect(x1,y1,x-x1,y-y1);
g_in.fillRect(x1,y1,x-x1,y-y1);
}
else if(y1>=y)
{
g.fillRect(x1,y,x-x1,y1-y);
g_in.fillRect(x1,y,x-x1,y1-y);
}
}
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
// TODO: Add your code here
}
}