lesson 2 简单画板
这几节课龙哥介绍了简单的画图板的实现,主要是一些按钮的监听器的设置和添加,以及传值的理解和应用,然后就是在最后任务中,要求用平面图形画出一些立体图形,比如长方体,金字塔,圆柱之类的,好像数学不太好,只能画出几个,还要多与同学交流获得灵感。
首先要求做出个登录界面,要求像QQ那样点击登录然后销毁登录框弹出一个新的画板界面。
public class MyDrawIn {private JFrame jf;//主方法public static void main(String[] args) {MyDrawIn md = new MyDrawIn();md.showDraw();}//显示登录主界面public void showDraw() {jf = new JFrame("登录我的画板");//初始化界面jf.setSize(200, 200);jf.setResizable(false);FlowLayout fl = new FlowLayout();//创建一个登录按钮JButton bu = new JButton("登录");bu.setActionCommand("登录");//设置这个鼠标的监听器对象,并且加到这个按钮上ActionListener al = new MyActionListener(jf);//将监听器加载到bu上bu.addActionListener(al);jf.add(bu);jf.setLayout(fl);//设置退出关闭程序,显示界面jf.setDefaultCloseOperation(3);jf.setVisible(true);}}
public class MyActionListener implements ActionListener{private JFrame jf1;public MyActionListener(JFrame jf) {jf1 = jf;} public void actionPerformed(ActionEvent e){ jf1.dispose(); MyDraw md = new MyDraw(); md.showMyDraw(); }}
public class MyDraw {//显示我的画板界面public void showMyDraw() {JFrame jf = new JFrame();jf.setVisible(false);jf = new JFrame("我的画板");//初始化界面jf.setSize(500, 500);jf.setResizable(false);FlowLayout fl = new FlowLayout();jf.setLayout(fl);//创建选择图形的按钮JButton bu_line = new JButton("line");JButton bu_rect = new JButton("rect");JButton bu_oval = new JButton("oval");bu_line.setActionCommand("Line");bu_rect.setActionCommand("Rect");bu_oval.setActionCommand("Oval");//ShapeActionListener al = new ShapeActionListener();bu_line.addActionListener(al);bu_rect.addActionListener(al);bu_oval.addActionListener(al);jf.add(bu_oval);jf.add(bu_rect);jf.add(bu_line);jf.setDefaultCloseOperation(3);jf.setVisible(true);Graphics g = jf.getGraphics();//设置鼠标监听器MouseListener ml = new MyMouseListener(g, al);jf.addMouseListener(ml);}
public class ShapeActionListener implements ActionListener{public int shape = 0; public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("Line")){ shape = 0; }else if(e.getActionCommand().equals("Rect")){ shape = 1; }else if(e.getActionCommand().equals("Oval")){ shape = 2; } }}
public class MyMouseListener implements MouseListener {public int x1, y1, x2, y2;private Graphics g;private ShapeActionListener al;public MyMouseListener(Graphics g, ShapeActionListener al) {this.g = g;this.al = al;}// 当鼠标释放时记录第二个点,并画图public void mouseReleased(MouseEvent e) {x2 = e.getX();y2 = e.getY();// 画if (al.shape == 0){g.drawLine(x1, y1, x2, y2);}else if(al.shape==1)/*立体矩形 * for(int i=0;i<256;i++){g.setColor(new Color(i,i,i));g.drawRect(x1+i, y1-i, x2-x1, y2-y1);}*/for(int i=0;i<100;i++){g.setColor(new Color(255-i,255-i,255-i));g.drawRect(x1, y1, x2-x1, y2-y1);x1--;x2++;y1++;y2++;}else if (al.shape==2){g.drawOval(x1, y1++, x2-x1, y2-y1);}/**立体圆柱 * for(int i=0;w>0&&h>0;i++){g.setColor(new Color(i,i,i));g.drawOval(x1, y1++, w, h);} */}// 当鼠标按下时记录第一个点public void mousePressed(MouseEvent e) {x1 = e.getX();y1 = e.getY();};public void mouseClicked(MouseEvent e) {};public void mouseEntered(MouseEvent e) {};public void mouseExited(MouseEvent e) {};}