简体   繁体   English

如何实现Jpanel的缩放?

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

I'm doing a project for school in witch I have to simulate an ant colony and also show it in a graphics user interface. 我正在为女巫做学校项目我必须模拟一个蚁群并在图形用户界面中显示它。

The whole project is almost complete but I neet to implement a zoom feature for my jPanel. 整个项目已基本完成,但我需要为我的jPanel实现缩放功能。

I've found a thread on this site with basically what I need.Here's the link: Zooming in and zooming out within a panel 我在这个网站上发现了一个基本上我需要的线程。这是链接: 在面板中放大和缩小

What Thanasis made in that thread is what I basically need but I have no Idea how to implement it inside my code with the other classes. Thanasis在该线程中所做的是我基本上需要的但我不知道如何在我的代码中使用其他类来实现它。

I am a newbie in Graphic User Interface and we are basically learning and understanding it by doing this project so forgive me if the answer is Super easy and I'm asking for the answer. 我是图形用户界面的新手,我们基本上是通过这个项目来学习和理解它,所以请原谅我,如果答案是超级简单的,我正在寻求答案。

I can provide code for the Pannel and Window classes. 我可以为Pannel和Window类提供代码。

I've allready tried launching it without anything thinking that it will work directly on my jpanel but it didn't of course.also tried to call it in my main but that didn't work either. 我已经尝试启动它而没有任何想法它会直接在我的jpanel上工作,但它当然没有。也试图在我的主要调用它,但这也没有用。 Here's my paintComponent from my panel . 这是我面板中的paintComponent。 I basically do this for everything that shows(ants, colony, food). 我基本上都是这样做的(蚂蚁,殖民地,食物)。

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);
        }
      }
    }
}

The answer of Andreas Holstenson in the link you provided is better than Thanasis' because you shouldn't override paint but paintComponent as you correctly did, and Thanasis doesn't overwrite the transformation but tries to be dumb-clever about not progressively updating the transform. Andreas Holstenson在你提供的链接中的答案比Thanasis更好,因为你不应该像正确的那样覆盖paint而不是paintComponent ,并且Thanasis不会覆盖转换但是试图愚蠢地关于不逐步更新转换。 If you lost me, just forget that answer altogether. 如果你失去了我,那就完全忘掉那个答案吧。

Otherwise, the choice of whether to use AffineTransform or not does not matter as the result is the same. 否则,选择是否使用AffineTransform无关紧要,因为结果是相同的。 Arguably, AffineTransform is easier to use. 可以说, AffineTransform更易于使用。

To answer your question, put the additional scale/translate code at the top of that method and all graphics drawn after that should be zoomed (even if if you use g instead of g2 ). 要回答您的问题,请将额外的比例/翻译代码放在该方法的顶部,之后绘制的所有图形都应缩放(即使您使用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