资源描述:
《java 单击按钮实现缩放还原》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实现图形的缩放与还原需求:做一个绘图程序,绘制矩形,制作三个按钮,控制图形的缩放和还原分析:1.创建窗体,设置窗体大小,背景色,布局2.创建面板,实现绘图与缩放还原3.创建三个按钮,实现事件监听,控制图形缩放实现:代码如下packagecs.itcast_sharp;importjava.awt.BasicStroke;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.event.
2、ActionEvent;importjava.awt.event.ActionListener;importjava.awt.geom.Rectangle2D;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.Border;publicclassZoomSharpFrameextendsJFrame{privatestaticintflag=0;//参数privateZoomSharpFrame(){ini
3、tComponent();}privatevoidinitComponent(){//TODOAuto-generatedmethodstub//setVisible(false);setSize(400,400);//窗体尺寸setLocationRelativeTo(null);//窗体放置位置setDefaultCloseOperation(DISPOSE_ON_CLOSE);//close操作//setBackground(Color.ORANGE);setLayout(null);getContentPane().setBackground(Co
4、lor.ORANGE);//窗体背景色设为橘色ZoomSharpPanelzPanel=newZoomSharpPanel();//建面板zPanel.setBounds(100,50,200,200);//面板尺寸与位置add(zPanel);JButtonj1=newJButton("放大");//按钮和按钮监听j1.setBounds(30,300,80,30);j1.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){//TOD
5、OAuto-generatedmethodstubflag=1;zPanel.repaint();//重绘System.out.println("flag:"+flag);}});add(j1);JButtonj2=newJButton("缩小");j2.setBounds(150,300,80,30);j2.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubflag=2;z
6、Panel.repaint();System.out.println("flag:"+flag);}});add(j2);JButtonj3=newJButton("还原");j3.setBounds(270,300,80,30);j3.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubflag=3;zPanel.repaint();System.out.println("f
7、lag:"+flag);}});add(j3);}publicclassZoomSharpPanelextendsJPanel{publicvoidpaint(Graphicsg){Graphics2Dc=(Graphics2D)g;Rectangle2D.Floatrec=newRectangle2D.Float(50,50,80,50);c.setColor(Color.RED);c.setBackground(Color.BLACK);c.setStroke(newBasicStroke(4));c.draw(rec);if(flag==1){c.c
8、learRect(0,0,220,220);c.scale(1.5