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

          swing

          Posted on 2007-04-26 11:54 G_G 閱讀(520) 評論(0)  編輯  收藏 所屬分類: Swing
          ?圖象拖動(簡單) ???
          代碼 上 哈? 解釋不多 !!!?? ??
          ??? ^_^??
          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();
          ????
          //當前?拖動?圖象
          ????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)?{
          ????
          ????}

          ????
          //?拖動選取
          ????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)?{
          ????}

          ????
          //關鍵?拖動?***********************
          ????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());
          ????}

          ????

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 甘南县| 阿克陶县| 东山县| 丰顺县| 股票| 英山县| 垫江县| 疏附县| 旅游| 富蕴县| 皮山县| 浪卡子县| 合山市| 东兰县| 固始县| 鸡西市| 无棣县| 沁源县| 玛多县| 长沙县| 长乐市| 南木林县| 岳阳市| 汽车| 修水县| 吴旗县| 栖霞市| 乐都县| 寻乌县| 古蔺县| 措美县| 正镶白旗| 禹州市| 大埔区| 伊宁市| 精河县| 麻江县| 岳池县| 犍为县| 正阳县| 无棣县|