posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          swing

          Posted on 2007-04-26 11:54 G_G 閱讀(527) 評(píng)論(0)  編輯  收藏 所屬分類: Swing
          ?圖象拖動(dòng)(簡單) ???
          代碼 上 哈? 解釋不多 !!!?? ??
          ??? ^_^??
          package?src.jframe;

          import?javax.swing.JFrame;
          import?javax.swing.JOptionPane;

          import?java.awt.Event;
          import?java.awt.Graphics;
          import?java.awt.Point;
          import?java.awt.Rectangle;
          import?java.awt.event.MouseEvent;
          import?java.awt.event.MouseListener;
          import?java.awt.event.MouseMotionListener;
          import?java.awt.event.WindowEvent;
          import?java.awt.event.WindowListener;
          import?java.util.ArrayList;

          public?class?MyFrame?implements?MouseListener,MouseMotionListener?{
          ????
          //圖象集合
          ????private?ArrayList?list?=?new?ArrayList();
          ????
          //當(dāng)前?拖動(dòng)?圖象
          ????private?Rectangle?re?=?null?;
          ????
          ????
          ????
          private??JFrame?jf?;
          ????{?
          ????????jf?
          =?new?JFrame(){
          ????????????
          public?void?paint(Graphics?g){
          ????????????????
          super.paint(g);
          ????????????????
          for(int?i=0;i<list.size();i++){
          ????????????????????Rectangle?ne?
          =????(Rectangle)list.get(i);
          ????????????????????
          this.getGraphics().drawRect(ne.x,ne.y,ne.height,ne.width);
          ????????????????????System.out.println(
          "ok");
          ????????????????}
          ????????????}
          ????????};
          ????????jf.addWindowListener(
          new?WindowListener(){

          ????????????
          public?void?windowActivated(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}

          ????????????
          public?void?windowClosed(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}

          ????????????
          public?void?windowClosing(WindowEvent?e)?{
          ????????????????System.exit(
          0);
          ????????????????
          ????????????}

          ????????????
          public?void?windowDeactivated(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}

          ????????????
          public?void?windowDeiconified(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}

          ????????????
          public?void?windowIconified(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}

          ????????????
          public?void?windowOpened(WindowEvent?e)?{
          ????????????????
          //?TODO?Auto-generated?method?stub
          ????????????????
          ????????????}
          ????????????
          ????????});
          ????????jf.setBounds(
          400,400,400,400);
          ????????jf.addMouseListener(
          this);
          ????????jf.addMouseMotionListener(
          this);
          ????????jf.setVisible(
          true);
          ????}
          ????
          ?????
          public?JFrame?getFrame(){
          ?????????
          return?this.jf?;
          ?????}
          ????
          ????
          //雙擊?花出圖象
          ????public?void?mouseClicked(MouseEvent?e)?{
          ????????
          if(e.getClickCount()?==?2){
          ????????Point?po?
          =?e.getPoint()?;
          ????????Rectangle?ne?
          =?new?Rectangle();
          ????????ne.setBounds((
          int)po.getX(),(int)po.getY(),20,20);
          ????????System.out.println(
          "ko1");
          ????????addList(ne);??
          //添加入?集合
          ????????}
          ????}
          ????
          private?void?addList(Rectangle?ne)?{
          ????????list.add(ne);
          ????????
          this.jf.repaint();
          ????}

          ????
          ????
          public?void?mouseEntered(MouseEvent?e)?{

          ????}

          ????
          public?void?mouseExited(MouseEvent?e)?{
          ????
          ????}

          ????
          //?拖動(dòng)選取
          ????public?void?mousePressed(MouseEvent?e)?{
          ????????
          this.re?=?null?;
          ????????
          for(int?i=0;i<this.list.size();i++){
          ????????????Rectangle?re?
          =?(Rectangle)list.get(i);
          ????????????
          if(?re.contains((int)e.getX(),(int)e.getY())?){
          ????????????????
          this.re?=?re;
          ????????????}
          ????????}
          ????}

          ????
          public?void?mouseReleased(MouseEvent?e)?{
          ????}

          ????
          //關(guān)鍵?拖動(dòng)?***********************
          ????public?void?mouseDragged(MouseEvent?e)?{
          ????????????
          if(this.re?!=?null){
          ????????????????
          this.re.setBounds((int)e.getX(),(int)e.getY(),20,20);
          ????????????????
          this.jf.repaint();
          ????????????}

          ????}

          ????
          public?void?mouseMoved(MouseEvent?e)?{
          ????}
          ????

          ????
          public?static?void?main(String[]args){
          ????????
          ????????JFrame?jf?
          =?(new?MyFrame()).getFrame();

          ????}
          }

          圖象的縮放Graphics2D *******

          ????protected?void?paintComponent(Graphics?g)?{

          ????????Graphics2D?g2d?
          =?(Graphics2D)g;
          ????????
          double?bh?=????fh?/?1200?;
          ????????
          double?bw?=???fw?/?600;
          ????????g2d.scale(bh,bw);
          ????????getDisplayUpdate().draw(g2d,?
          this);
          ????????
          this.setBackground(this.getBg());
          ????}

          ????

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 金塔县| 安义县| 内黄县| 林州市| 宁都县| 扎赉特旗| 五原县| 阳朔县| 斗六市| 横山县| 金山区| 珠海市| 湘潭县| 韶山市| 泽普县| 平谷区| 澳门| 宜城市| 松阳县| 深圳市| 新巴尔虎左旗| 鄂州市| 汶上县| 乌拉特后旗| 金山区| 旬阳县| 宿迁市| 海淀区| 光泽县| 滦平县| 大兴区| 宁化县| 平顶山市| 英吉沙县| 如皋市| 两当县| 普洱| 大丰市| 东兰县| 泾川县| 三穗县|