小菜樂園

          生活就是一杯茶

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            8 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

          import java.awt.Color;
          import java.awt.Cursor;
          import java.awt.Dimension;
          import java.awt.Graphics;
          import java.awt.event.MouseEvent;
          import java.awt.event.MouseListener;
          import java.awt.event.MouseMotionListener;

          import javax.swing.ImageIcon;
          import javax.swing.JApplet;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          import javax.swing.border.LineBorder;

          public class ImageApplet extends JApplet {

           private JPanel jContentPane = null;

           private JPanel jPanel = null;

           

           ImageIcon imageIcon = new ImageIcon("images/icon/common/det_dis.gif");

           /**
            * This is the default constructor
            */
           public ImageApplet() {
            super();
           }

           /**
            * This method initializes this
            *
            * @return void
            */
           public void init() {
            this.setSize(640, 480);
            this.setContentPane(getJContentPane());
           }

           /**
            * This method initializes jContentPane
            *
            * @return javax.swing.JPanel
            */
           private JPanel getJContentPane() {
            if (jContentPane == null) {
             jContentPane = new JPanel();
             jContentPane.setLayout(null);
             jContentPane.add(new MainPanel(imageIcon), null);
            }
            return jContentPane;
           }

           /**
            * This method initializes jPanel
            *
            * @return javax.swing.JPanel
            */
           class MainPanel extends JPanel implements MouseListener,
             MouseMotionListener {
            int x1, y1;
          //用來表示向東、南、西、北、東、南、東、北、西南、西北八個方向縮放點
            private JLabel northWest = null;

            private JLabel north = null;

            private JLabel northEast = null;

            private JLabel east = null;

            private JLabel southEast = null;

            private JLabel south = null;

            private JLabel southWest = null;

            private JLabel west = null;
          //圖片
            ImageIcon imageIcon = null;
          //是否可縮放標記  
            boolean isSelected = false;
            
            public MainPanel(ImageIcon imageIcon) {
             this.imageIcon = imageIcon;
             this.setBounds(256,107,40,30);
             x1 = this.getWidth();
             y1 = this.getHeight();
             //初始化東、南、西、北、東、南、東、北、西南、西北八個方向縮放點
             northWest = new JLabel();
             northWest.setText("");
             northWest.setBorder(new LineBorder(Color.WHITE, 1));
             northWest.addMouseListener(this);
             northWest.addMouseMotionListener(this);
             northWest.setVisible(false);
             
             north = new JLabel();
             north.setText("");
             north.setBorder(new LineBorder(Color.WHITE, 1));
             north.addMouseListener(this);
             north.addMouseMotionListener(this);
             north.setVisible(false);
             
             northEast = new JLabel();
             northEast.setText("");
             northEast.setBorder(new LineBorder(Color.WHITE, 1));
             northEast.addMouseListener(this);
             northEast.addMouseMotionListener(this);
             northEast.setVisible(false);

             east = new JLabel();
             east.setText("");
             east.setBorder(new LineBorder(Color.WHITE, 1));
             east.addMouseListener(this);
             east.addMouseMotionListener(this);
             east.setVisible(false);

             southEast = new JLabel();
             southEast.setText("");
             southEast.setBorder(new LineBorder(Color.WHITE, 1));
             southEast.addMouseListener(this);
             southEast.addMouseMotionListener(this);
             southEast.setVisible(false);

             south = new JLabel();
             south.setText("");
             south.setBorder(new LineBorder(Color.WHITE, 1));
             south.addMouseListener(this);
             south.addMouseMotionListener(this);
             south.setVisible(false);

             southWest = new JLabel();
             southWest.setText("");
             southWest.setBorder(new LineBorder(Color.WHITE, 1));
             southWest.addMouseListener(this);
             southWest.addMouseMotionListener(this);
             southWest.setVisible(false);

             west = new JLabel();
             west.setText("");
             west.setBorder(new LineBorder(Color.WHITE, 1));
             west.addMouseListener(this);
             west.addMouseMotionListener(this);
             west.setVisible(false);
          //東、南、西、北、東、南、東、北、西南、西北八個方向縮放點的坐標
             northWest.setBounds(0, 0, 5, 5);
             north.setBounds((x1 - 3) / 2, 0, 5, 5);
             northEast.setBounds(x1 - 5, 0, 5, 5);
             east.setBounds(x1 - 5, (y1 - 3) / 2, 5, 5);
             southEast.setBounds(x1 - 5, y1 - 5, 5, 5);
             south.setBounds((x1 - 3) / 2, y1 - 5, 5, 5);
             southWest.setBounds(0, y1 - 5, 5, 5);
             west.setBounds(0, (y1 - 3) / 2, 5, 5);

             this.setLayout(null);
             this.setMinimumSize(new Dimension(20, 20));
             this.addMouseListener(this);
             this.addMouseMotionListener(this);
          //加入縮放點到jpanel中   
             this.add(northWest, null);
             this.add(north, null);
             this.add(northEast, null);
             this.add(east, null);
             this.add(southEast, null);
             this.add(south, null);
             this.add(southWest, null);
             this.add(west, null);
            }
          //單擊后可以進行縮放處理
            public void mouseClicked(MouseEvent e) {
             isSelected = true;
             this.setBorder(new LineBorder(Color.BLACK, 4));
             northWest.setVisible(true);
             north.setVisible(true);
             northEast.setVisible(true);
             east.setVisible(true);
             southEast.setVisible(true);
             south.setVisible(true);
             southWest.setVisible(true);
             west.setVisible(true);
             this.repaint();
            }

            public void mousePressed(MouseEvent e) {
             // TODO Auto-generated method stub

            }
          //防止鼠標在縮放時移出了方向點區域,圖片的縮放不能進行
            public void mouseReleased(MouseEvent e) {
             if(isSelected){
              if(getCursor().getType()!=Cursor.DEFAULT_CURSOR){
               setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
              }
             }

            }
          //鼠標進入哪個縮放點區域
            public void mouseEntered(MouseEvent e) {   
             if (isSelected) {
              if (e.getComponent() == northWest) {
               setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
              } else if (e.getComponent() == north) {
               setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
              } else if (e.getComponent() == northEast) {
               setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
              } else if (e.getComponent() == east) {
               setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
              } else if (e.getComponent() == southEast) {
               setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
              } else if (e.getComponent() == south) {
               setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
              } else if (e.getComponent() == southWest) {
               setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
              } else if (e.getComponent() == west) {
               setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
              } /*else if(e.getComponent() == this){     
               setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
              }*/
             }
            }

            public void mouseExited(MouseEvent e) {
            }
          //縮放及拖動的操作
            public void mouseDragged(MouseEvent e) {
             if (isSelected) {
              int type = getCursor().getType();
              int x, y, w, h;
              x = this.getX();
              y = this.getY();    
              w = this.getWidth();
              h = this.getHeight();
              switch (type) {
              case Cursor.NW_RESIZE_CURSOR://西北方向縮放
               if(w - e.getX() >20&& h - e.getY()>20){
               w = w - e.getX();
               h = h - e.getY();
                 this.setBounds(x + e.getX(), y+ e.getY(), w, h);
               }    
               break;
              case Cursor.N_RESIZE_CURSOR://北
               if(h-e.getY()>20){
               h = h - e.getY();
               this.setBounds(x, y + e.getY(),w, h);
               }
               break;
              case Cursor.NE_RESIZE_CURSOR://東北
               if(w + e.getX()>20 && h - e.getY()>20){
               w = w + e.getX();
               h = h - e.getY();
               this.setBounds(x, y + e.getY(),w, h);
               }
               break;
              case Cursor.E_RESIZE_CURSOR://東
               if(w + e.getX()>20){
               w = w + e.getX();
               this.setBounds(x, y, w, h);
               }
               break;
              case Cursor.SE_RESIZE_CURSOR://東南
               if(w + e.getX()>20&& h + e.getY()>20){
               w = w + e.getX();
               h = h + e.getY();
               this.setBounds(x, y, w, h);
               }
               break;
              case Cursor.S_RESIZE_CURSOR://南
               if(h+e.getY()>20){
               h = h + e.getY();
               this.setBounds(x, y, w, h);
               }
               break;
              case Cursor.SW_RESIZE_CURSOR://西南
               if(w-e.getX()>20&& h+e.getY()>20){
               w = w - e.getX();
               h = h + e.getY();
               this.setBounds(x + e.getX(), y,w, h);
               }
               break;
              case Cursor.W_RESIZE_CURSOR://西
               if(w-e.getX()>20){
               w = w - e.getX();
               this.setBounds(x + e.getX(), y,w, h);
               }
               break;
              case Cursor.DEFAULT_CURSOR://拖動操作
               if(e.getComponent()==this){
               this.setBounds(x+e.getX(),y+e.getY(),w,h);
               }
              default:     
               break;
              }
              //重定位各縮放點坐標位置
              northWest.setBounds(0, 0, 5, 5);
              north.setBounds((w - 3) / 2, 0, 5, 5);
              northEast.setBounds(w - 5, 0, 5, 5);
              east.setBounds(w - 5, (h - 3) / 2,5, 5);
              southEast.setBounds(w - 5, h - 5, 5, 5);
              south.setBounds((w - 3) / 2, h - 5,5, 5);
              southWest.setBounds(0, h - 5, 5, 5);
              west.setBounds(0, (h - 3) / 2, 5, 5);
              //重繪
              this.repaint();    
             }
            }

            public void mouseMoved(MouseEvent e) {
             // TODO Auto-generated method stub

            }

            public void paint(Graphics g) {
             super.paint(g);
             //繪制圖片在中央
             g.drawImage(this.imageIcon.getImage(), 5, 5, this.getWidth() - 10,
               this.getHeight() - 10, this);
            }
           }
          }

          posted on 2007-08-17 20:19 小菜 閱讀(1619) 評論(0)  編輯  收藏 所屬分類: Java圖形圖像
          主站蜘蛛池模板: 乃东县| 含山县| 清流县| 武鸣县| 长岛县| 同心县| 四会市| 皮山县| 博兴县| 平乐县| 怀集县| 九龙坡区| 扬州市| 阳新县| 揭东县| 津市市| 郁南县| 三门峡市| 梧州市| 内乡县| 句容市| 图片| 澄江县| 邵东县| 包头市| 舟曲县| 宜城市| 临江市| 喜德县| 仪征市| 岑溪市| 基隆市| 合肥市| 台江县| 张北县| 桂阳县| 大足县| 商洛市| 海城市| 武夷山市| 博罗县|