<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Picses' sky

    Picses' sky
    posts - 43, comments - 29, trackbacks - 0, articles - 24

    在Swing中繪制二維圖[zz]

    Posted on 2007-07-23 09:30 Matthew Chen 閱讀(566) 評(píng)論(5)  編輯  收藏 所屬分類: Swing and AWT
    作者: sitinspring  
    主頁(yè): http://m.tkk7.com/sitinspring/

    在Swing中繪制二維圖,并有拖曳,縮放,鏈接,輸出功能,主要有幾大技術(shù)要點(diǎn)
    1.繪畫時(shí)的閃爍問題,解決方法是先將所有圖元畫到一張后臺(tái)圖上,在將后臺(tái)圖一次性畫到面板上,請(qǐng)見public void paint(Graphics g)的處理.
    2.鼠標(biāo)響應(yīng):解決方法是添加面板的鼠標(biāo)點(diǎn)擊,鼠標(biāo)拖曳處理,以鼠標(biāo)的位置來確定圖元的處理,請(qǐng)見函數(shù)public void mousePressed(MouseEvent e),public void mouseDragged(MouseEvent e)和public void mouseClicked(MouseEvent e).
    3.單個(gè)圖元處理:每個(gè)圖元繼承Actor2D類,它需要記住當(dāng)前圖元的起始位置和縮放比例.
    4.多個(gè)圖元處理:當(dāng)大圖元移動(dòng)時(shí),直線/折線圖元需要根據(jù)大圖元確定自己的位置,這時(shí)需要訪問圖元列表,以便知道那條直線在自身上.
    5.輸出圖片:這個(gè)相對(duì)簡(jiǎn)單,看函數(shù)public boolean saveImage(String imagePath)的處理就可以了.
    先寫道這里,細(xì)節(jié)以后再整理,大家有興趣先看看代碼吧.

    public class PaintBoard extends JPanel implements MouseListener,
        MouseMotionListener, KeyListener 
    {

      
    // 背景圖
      private Image bgImage;
      
    private Graphics bg;

      
    // 畫板上的二維圖元列表
      private ArrayList drawList=new ArrayList();

      
    // 畫板幾何尺寸
      private int myWidth;
      
    private int myHeight;
      
      
    // 繪畫圖元時(shí)的偏移尺寸
      private int xOffset, yOffset;

     
    // 構(gòu)造函數(shù) 
     public PaintBoard(MakeSqlToolbar toolbar) {
        
    super();
        
    this.toolbar = toolbar;
        
    this.addMouseListener(this);
        
    this.addMouseMotionListener(this);
        
    this.addKeyListener(this);
      }


     
    // 調(diào)整畫面大小時(shí)的處理
      private void resizePaintBoard() {
        
    if (myWidth != this.getSize().width
            
    || myHeight != this.getSize().height) {
          bgImage 
    = null;
          myWidth 
    = this.getSize().width;
          myHeight 
    = this.getSize().height;
        }

      }


     
    // 重新調(diào)整背景 
     private void reArrangeBg() {
        
    if (bgImage == null{
          bgImage 
    = this.createImage(myWidth, myHeight);
          bg 
    = bgImage.getGraphics();
        }

      }


      
    // 繪圖的關(guān)鍵函數(shù)
      public void paint(Graphics g) {
        resizePaintBoard();
        reArrangeBg();

        
    // 設(shè)置背景
        bg.setColor(Color.white);
        bg.fillRect(
    00, myWidth, myHeight);

       
    // 在背景圖繪畫圖元 
       if (drawList != null{
          
    for (Iterator it = drawList.iterator(); it.hasNext();) {
            Actable actor 
    = (Actable) it.next();
            actor.paint(bg);
          }

        }


        
    // 將背景圖畫在面板上
        g.drawImage(bgImage, 00this);
      }


      
    private boolean mousePressActorTest(Actor2D actor, int x, int y) {
        
    if (actor.isInRect(x, y)) {
          actor.setStatus(Actor2D.Status_Active);
          xOffset 
    = x - actor.getLeft();
          yOffset 
    = y - actor.getTop();

          
    if(!(actor instanceof ActorTable)){
            
    return true;
          }

          ActorTable actorTable
    =(ActorTable)actor;
          
          
    for (Iterator it = drawList.iterator(); it.hasNext();) {
            Actor2D actorTmp 
    = (Actor2D) it.next();

            
    if (actorTmp instanceof ActorLine) {
              ActorLine actorLine 
    = (ActorLine) actorTmp;
              ActorPoint currPoint;
              
              currPoint
    =actorLine.getStartPt();
              currPoint.setInner(actorTable.isInColumns(currPoint.getLeft(),currPoint.getTop()));
              
              currPoint
    =actorLine.getEndPt();
              currPoint.setInner(actorTable.isInColumns(currPoint.getLeft(),currPoint.getTop()));
            }

          }


          
    return true;
        }
     
        
        
    return false;   
      }


      
    public void mousePressed(MouseEvent e) {
        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            actorLine.setStatus(Actor2D.Status_Sleep);
          }
     else {
            actor.setStatus(Actor2D.Status_Sleep);
          }

        }


        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            
    if (mousePressActorTest(actorLine.getStartPt(), e.getX(), e
                .getY()))
              
    break;
            
    if (mousePressActorTest(actorLine.getEndPt(), e.getX(), e
                .getY()))
              
    break;
          }
     else {
            
    if (mousePressActorTest(actor, e.getX(), e.getY()))
              
    break;
          }

        }


        repaint();
      }


      
    private boolean mouseClickActorTest(Actor2D actor, int x, int y) {
        
    if (actor.isInRect(x, y)) {
          actor.setStatus(Actor2D.Status_Active);
          
    return true;
        }
     else {
          
    return false;
        }

      }


      
    public void mouseClicked(MouseEvent e) {
        
    this.requestFocusInWindow();

        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            actorLine.setStatus(Actor2D.Status_Sleep);
          }
     else {
            actor.setStatus(Actor2D.Status_Sleep);
          }

        }


        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            
    if (mouseClickActorTest(actorLine.getStartPt(), e.getX(), e
                .getY()))
              
    break;
            
    if (mouseClickActorTest(actorLine.getEndPt(), e.getX(), e
                .getY()))
              
    break;
          }
     else {
            
    if (mouseClickActorTest(actor, e.getX(), e.getY()))
              
    break;
          }

        }


        repaint();
      }


      
    private void mouseDragActorTest(Actor2D actor, int x, int y) {
        
    if (!actor.isActive()) return;
        
        Actor2D actorTest
    =new Actor2D(actor.getLeft(),actor.getTop(),actor.getWidth(),actor.getHeight());
        actorTest.setLeft(x 
    - xOffset);
        actorTest.setTop(y 
    - yOffset);      
        makeActorInBound(actorTest);
        
        
    int xChanged=actor.getLeft()-actorTest.getLeft();
        
    int yChanged=actor.getTop()-actorTest.getTop();
        
        
    if(actor instanceof ActorTable){
          ActorTable actorTable 
    = (ActorTable) actor;
          
          
    for (Iterator it = drawList.iterator(); it.hasNext();) {
            Actor2D actorTmp 
    = (Actor2D) it.next();
            
            
    if(actorTmp instanceof ActorLine){
              ActorLine actorLine
    =(ActorLine)actorTmp;
              ActorPoint currPoint;
              
              currPoint
    =actorLine.getStartPt();
              
    if(actorTable.isInColumns(currPoint.getLeft(),currPoint.getTop()) && currPoint.isInner()){
                currPoint.setLeft(currPoint.getLeft()
    -xChanged);
                currPoint.setTop(currPoint.getTop()
    -yChanged);
              }

              
              currPoint
    =actorLine.getEndPt();
              
    if(actorTable.isInColumns(currPoint.getLeft(),currPoint.getTop()) && currPoint.isInner()){
                currPoint.setLeft(currPoint.getLeft()
    -xChanged);
                currPoint.setTop(currPoint.getTop()
    -yChanged);
              }

            }

          }

        }
       
        
        actor.setLeft(actor.getLeft()
    -xChanged);
        actor.setTop(actor.getTop()
    -yChanged);    
      }


      
    public void mouseDragged(MouseEvent e) {
        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            mouseDragActorTest(actorLine.getStartPt(), e.getX(), e.getY());
            mouseDragActorTest(actorLine.getEndPt(), e.getX(), e.getY());
          }
     else {        
            mouseDragActorTest(actor, e.getX(), e.getY());
          }

        }


        repaint();
      }


      
    private void keyPressedActorTest(Actor2D actor, int x, int y) {
        
    if (actor.isActive()) {
          actor.setLeft(actor.getLeft() 
    + x);
          actor.setTop(actor.getTop() 
    + y);
          makeActorInBound(actor);
        }

      }


      
    public void keyPressed(KeyEvent e) {
        
    int keyCode = e.getKeyCode();

        
    int xMicroOffset = 1, yMicroOffset = 1;

        
    if (keyCode == KeyEvent.VK_RIGHT) {
          yMicroOffset 
    = 0;
        }
     else if (keyCode == KeyEvent.VK_LEFT) {
          xMicroOffset 
    = -xMicroOffset;
          yMicroOffset 
    = 0;
        }
     else if (keyCode == KeyEvent.VK_UP) {
          yMicroOffset 
    = -yMicroOffset;
          xMicroOffset 
    = 0;
        }
     else if (keyCode == KeyEvent.VK_DOWN) {
          xMicroOffset 
    = 0;
        }


        
    for (Iterator it = drawList.iterator(); it.hasNext();) {
          Actor2D actor 
    = (Actor2D) it.next();

          
    if (actor instanceof ActorLine) {
            ActorLine actorLine 
    = (ActorLine) actor;
            keyPressedActorTest(actorLine.getStartPt(), xMicroOffset,
                yMicroOffset);
            keyPressedActorTest(actorLine.getEndPt(), xMicroOffset,
                yMicroOffset);
          }
     else {
            keyPressedActorTest(actor, xMicroOffset, yMicroOffset);
          }

        }


        
    if (keyCode == KeyEvent.VK_DELETE) {
          
    for (int i = 0; i < drawList.size(); i++{
            Actor2D actor 
    = (Actor2D) drawList.get(i);

            
    if (actor instanceof ActorLine) {
              ActorLine actorLine 
    = (ActorLine) actor;

              
    if (actorLine.getStartPt().isActive()) {
                
    if (ComDlgUtils
                    .popupConfirmCancelDialog(
    "Do you wanna remove the Line:"
                        
    + actor.getName() + "?"== true{
                  drawList.remove(i);
                }

              }


              
    if (actorLine.getEndPt().isActive()) {
                
    if (ComDlgUtils
                    .popupConfirmCancelDialog(
    "Do you wanna remove the Line:"
                        
    + actor.getName() + "?"== true{
                  drawList.remove(i);
                }

              }

            }
     else {
              
    if (actor.isActive()) {
                
    if (ComDlgUtils
                    .popupConfirmCancelDialog(
    "Do you wanna remove the table:"
                        
    + actor.getName() + "?"== true{
                  drawList.remove(i);
                }

              }

            }

          }

        }


        repaint();
      }


      
    private void makeActorInBound(Actor2D Actor) {
        
    if (Actor.getLeft() < 0{
          Actor.setLeft(
    0);
        }


        
    if (Actor.getTop() < 0{
          Actor.setTop(
    0);
        }


        
    if (Actor.getRight() > myWidth) {
          Actor.setLeft(myWidth 
    - Actor.getWidth());
        }


        
    if (Actor.getBottom() > myHeight) {
          Actor.setTop(myHeight 
    - Actor.getHeight());
        }

      }


      
    public void mouseMoved(MouseEvent e) {
        toolbar.setMousePos(e.getX(), e.getY());
      }


      
    public boolean saveImage(String imagePath) {
        
    try {
          FileOutputStream out 
    = new FileOutputStream(imagePath);
          JPEGImageEncoder encoder 
    = JPEGCodec.createJPEGEncoder(out);
          BufferedImage tag 
    = new BufferedImage(myWidth, myHeight,
              BufferedImage.TYPE_INT_RGB);
          tag.getGraphics().drawImage(bgImage, 
    00, myWidth, myHeight, null);
          encoder.encode(tag);
          out.close();
          
    return true;
        }
     catch (Exception e) {
          ComDlgUtils.popupErrorDialog(e.getMessage());
          
    return false;
        }

      }

      
      
    public ArrayList getDrawList() {
        
    return drawList;
      }


      
    public void setDrawList(ArrayList drawList) {
        
    this.drawList = drawList;
      }


      
    public void keyTyped(KeyEvent e) {
      }


      
    public void keyReleased(KeyEvent e) {

      }


      
    public void mouseEntered(MouseEvent e) {
      }


      
    public void mouseExited(MouseEvent e) {
      }


      
    public void mouseReleased(MouseEvent e) {

      }

    }

    Feedback

    # re: 在Swing中繪制二維圖[zz]  回復(fù)  更多評(píng)論   

    2007-07-23 12:46 by sitinspring
    我的東西發(fā)表在http://m.tkk7.com/junglesong上又給轉(zhuǎn)到這了.

    # re: 在Swing中繪制二維圖[zz]  回復(fù)  更多評(píng)論   

    2007-07-24 08:56 by Matthew Chen
    sorry,平時(shí)很少上,剛看到你的評(píng)論,已經(jīng)加上出處了。

    # re: 在Swing中繪制二維圖[zz]  回復(fù)  更多評(píng)論   

    2007-07-25 08:44 by sitinspring
    感謝樓主轉(zhuǎn)載.

    # re: 在Swing中繪制二維圖[zz]  回復(fù)  更多評(píng)論   

    2007-11-16 01:43 by oeoele
    不好意思,有完整可以執(zhí)行的代碼嗎?謝謝

    # re: 在Swing中繪制二維圖[zz]  回復(fù)  更多評(píng)論   

    2012-03-23 23:23 by 李俊
    而且這個(gè)耗內(nèi)存,無論內(nèi)存多大都是不能容忍的。

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 欧美大尺寸SUV免费| 成人精品国产亚洲欧洲| 免费看片在线观看| 4480yy私人影院亚洲| 8x8x华人永久免费视频| 亚洲成人免费在线| 免费国产黄网站在线观看可以下载 | 国产亚洲人成网站观看| 国产一级片免费看| 久久夜色精品国产嚕嚕亚洲av| 18禁在线无遮挡免费观看网站| 亚洲av永久无码精品国产精品| 免费人成毛片动漫在线播放| 亚洲人成依人成综合网| 亚洲人成在线免费观看| 亚洲在成人网在线看| 国产成人免费爽爽爽视频| 亚洲熟妇无码一区二区三区导航| 四虎免费大片aⅴ入口| 国产精品亚洲精品日韩动图 | 免费观看黄网站在线播放| 日韩亚洲国产高清免费视频| 国产精品国产午夜免费福利看| 国产精品亚洲va在线观看| 亚洲国产精品无码久久九九| 成人免费av一区二区三区| 亚洲国产精品成人精品无码区 | 久久精品国产99国产精品亚洲| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲三级高清免费| 亚洲日本在线电影| 亚洲第一视频在线观看免费| 久久国产乱子伦精品免费午夜| 亚洲av无码成人黄网站在线观看| 4虎1515hh永久免费| 亚洲成_人网站图片| 全黄性性激高免费视频| 女人隐私秘视频黄www免费| 亚洲视频2020| 成年人视频在线观看免费| 一边摸一边桶一边脱免费视频|