繁体   English   中英

如何实现Jpanel的缩放?

[英]How do I implement zooming for a Jpanel?

我正在为女巫做学校项目我必须模拟一个蚁群并在图形用户界面中显示它。

整个项目已基本完成,但我需要为我的jPanel实现缩放功能。

我在这个网站上发现了一个基本上我需要的线程。这是链接: 在面板中放大和缩小

Thanasis在该线程中所做的是我基本上需要的但我不知道如何在我的代码中使用其他类来实现它。

我是图形用户界面的新手,我们基本上是通过这个项目来学习和理解它,所以请原谅我,如果答案是超级简单的,我正在寻求答案。

我可以为Pannel和Window类提供代码。

我已经尝试启动它而没有任何想法它会直接在我的jpanel上工作,但它当然没有。也试图在我的主要调用它,但这也没有用。 这是我面板中的paintComponent。 我基本上都是这样做的(蚂蚁,殖民地,食物)。

public void paintComponent(Graphics g){
    int tailletab = this.gri.getTab().length;
    //On récupère le tableau de la Grille
    int[][] gril = this.gri.getTab();
    int taillecarre;
    int xcol = this.colo.getPos().getX();
    int ycol = this.colo.getPos().getY();
    int xsou = this.source.getPos().getX();
    int ysou = this.source.getPos().getY();
    if(tailletab<=50){
      taillecarre = tailletab/4+2;
    }else{
      if(tailletab<60){
        taillecarre = tailletab/5+1;
      }else{
        if(tailletab<70){
          taillecarre = tailletab/7+1;
        }else{
          if(tailletab<80){
            taillecarre = tailletab/8;
          }else{
            if(tailletab<90){
              taillecarre = tailletab/10;
            }else{
              taillecarre = tailletab/13;
            }
          }
        }
      }
    }
    for(int i=0; i<tailletab; i++){
      for(int j=0; j<tailletab; j++){
        if(gril[j][i]==0){
          if(j==xcol && i==ycol){
            g.setColor(new Color(102, 51, 0));
            g.fillRect(xcol*taillecarre, ycol*taillecarre,taillecarre,taillecarre);
            g.setColor(Color.BLACK);
            g.drawRect(xcol*taillecarre, ycol*taillecarre,taillecarre,taillecarre);
          }else{
            if(j==xsou && i==ysou){
              g.setColor(Color.RED);
              g.fillRect(xsou*taillecarre, ysou*taillecarre,taillecarre,taillecarre);
              g.setColor(Color.BLACK);
              g.drawRect(xsou*taillecarre, ysou*taillecarre,taillecarre,taillecarre);
            }else{
              g.setColor(Color.BLACK);
              g.drawRect(j*taillecarre, i*taillecarre, taillecarre, taillecarre);
            }
          }
        }else{
          g.setColor(Color.BLACK);
          g.drawRect(j*taillecarre, i*taillecarre, taillecarre, taillecarre);
          g.fillRect(j*taillecarre, i*taillecarre, taillecarre, taillecarre);
        }
      }
    }
}

Andreas Holstenson在你提供的链接中的答案比Thanasis更好,因为你不应该像正确的那样覆盖paint而不是paintComponent ,并且Thanasis不会覆盖转换但是试图愚蠢地关于不逐步更新转换。 如果你失去了我,那就完全忘掉那个答案吧。

否则,选择是否使用AffineTransform无关紧要,因为结果是相同的。 可以说, AffineTransform更易于使用。

要回答您的问题,请将额外的比例/翻译代码放在该方法的顶部,之后绘制的所有图形都应缩放(即使您使用g而不是g2 )。

@Override
protected void paintComponent(Graphics g) { // No need to widen it to public
    Graphics2D g2 = (Graphics2D)g;

    AffineTransform oldTransform = g2.getTransform();
    try {
        float zoom = 0.5f; // shrink
        // Note that transforms are in reverse order
        // because of how matrix multiplications work.
        AffineTransform newTransform = new AffineTransform();
        // 2nd transform: re-center
        newTransform.translate(getWidth()  * (1 - zoom) / 2,
                               getHeight() * (1 - zoom) / 2);
        // 1st transform: zoom (relative to upper-left corner)
        newTransform.scale(zoom, zoom);
        g2.transform(newTransform);

        // Draw here
    } finally {
        // Try-finally is probably not required, but ensures that the transform
        // gets restored even if an exception is thrown during drawing.
        g2.setTransform(oldTransform);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM