資源描述:
《java 單擊按鈕實現(xiàn)縮放還原》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、實現(xiàn)圖形的縮放與還原需求:做一個繪圖程序,繪制矩形,制作三個按鈕,控制圖形的縮放和還原分析:1.創(chuàng)建窗體,設(shè)置窗體大小,背景色,布局2.創(chuàng)建面板,實現(xiàn)繪圖與縮放還原3.創(chuàng)建三個按鈕,實現(xiàn)事件監(jiān)聽,控制圖形縮放實現(xiàn):代碼如下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;//參數(shù)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);//窗體背景色設(shè)為橘色ZoomSharpPanelzPanel=newZoomSharpPanel();//建面板zPanel.setBounds(100,50,200,200);//面板尺寸與位置add(zPanel);JButtonj1=newJButton("放大");//按鈕和按鈕監(jiān)聽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