java简单绘图

package cn;

import java.io.Serializable;

public class Point implements Serializable{
    private int pointx;
    private int pointy;
    private int posationX;
    private int posationY;
    
	public Point(int pointx, int pointy, int posationX, int posationY) {
		this.pointx = pointx;
		this.pointy = pointy;
		this.posationX = posationX;
		this.posationY = posationY;
	}
	public int getPointx() {
		return pointx;
	}
	public void setPointx(int pointx) {
		this.pointx = pointx;
	}
	public int getPointy() {
		return pointy;
	}
	public void setPointy(int pointy) {
		this.pointy = pointy;
	}
	public int getPosationX() {
		return posationX;
	}
	public void setPosationX(int posationX) {
		this.posationX = posationX;
	}
	public int getPosationY() {
		return posationY;
	}
	public void setPosationY(int posationY) {
		this.posationY = posationY;
	}
}

package cn;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class GH extends JFrame{
      private JMenuBar bar;
      private JMenu menu;
      private JMenuItem itemSave,itemRead;
      private List<Point> point=null;
      private int x,y;
      public GH(){
    	  point=new ArrayList<Point>();
    	  this.setTitle("菜单项");
    	  bar=new JMenuBar();
    	  menu=new JMenu("文件");
    	  itemSave=new JMenuItem("保存");
    	  itemSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileOutputStream fos=null;
				ObjectOutputStream oos=null;
				try {
					fos=new FileOutputStream("e:\\a.bat");
					oos=new ObjectOutputStream(fos);
					oos.writeObject(point);
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}finally{
				    try {
						if(oos!=null){
							oos.close();
						}
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		  });
    	  itemRead=new JMenuItem("读取");
    	  itemRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileInputStream fis=null;
				ObjectInputStream ois=null;
				try {
					fis=new FileInputStream("e:\\a.bat");
					ois=new ObjectInputStream(fis);
					List<Point> p=(List<Point>)ois.readObject();
					point=p;
					repaint();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}finally{
				    try {
						if(ois!=null){
							ois.close();
						}
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		  });
    	  menu.add(itemSave);
    	  menu.add(itemRead);
    	  bar.add(menu);
    	  this.setJMenuBar(bar);
    	  
    	  this.addMouseMotionListener(new MouseAdapter() {
			public void mouseDragged(MouseEvent e) {
				x=e.getX();
				y=e.getY();
				Point p=new Point(x, y, 4, 4);
				point.add(p);
				repaint();
			}
		  });
    	  setBounds(300, 300, 600, 300);
    	  setVisible(true);
    	  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      @Override
      public void paint(Graphics g) {
    	for(int i=0;i<point.size();i++){
    		Point p=point.get(i);
    		g.fillOval(p.getPointx(), p.getPointy(), p.getPosationX(), p.getPosationY());
    	}
      }
      public static void main(String[] args){
    	  GH g=new GH();
      }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值