简体   繁体   中英

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.

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.

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.

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. Here's my paintComponent from my panel . 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. 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. Arguably, AffineTransform is easier to use.

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 ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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