JRotateButton

Status
Nicht offen für weitere Antworten.
grüß euch alle


kann mir jemand sagen wie ich einen JButton rotieren kann (90°ccw für ne Matrix)

hab dazu zuwar schon div threads gelesen aber nicht wirklich schlau draus geworden.


bisher hab ich ne neue Klasse generiert, die von JButton abgeleitet, paint überschrieben
border und background per super.wahtever() zeichnen lassen.

Code:
class JRotateButton extends JButton{
...
  private String Text; // Schaltflächenbeschriftung
...
  public void paint(Graphics g){
    //keep std-appearance
        super.paintBorder(g);   
        g.setColor(this.getBackground());
        g.fillRect(2,2,16,76);
    //now add rotated Text 
     ...
  }
...
}

bisher easy und funktionsfähig

jetzt muss noch Text auf den Button, und zwar eben um 90° ccw gedreht,
womit wir schon beim problem dieses Lösungsansatzes wären



kennt jemand eine möglichkeit einen JButton zu rotieren?
weis wer wie ich aus einem Text ein Image genereieren kann?

oder weis wer sonst einen ansatz


grüße I.B.J.F.
 
nicht haun => schon gelöst


Code:
    public void paint(Graphics g) {
        super.paint(g);
        String Text = "BeispielText";
        Dimension d = this.getSize();
        FontMetrics fm = this.getFontMetrics(this.getFont());
        int w = fm.stringWidth(Text);
        int h = fm.getHeight();
        Image rotatedLabelImage = null;
        try {
            rotatedLabelImage = createRotatedImage(g, Text, fm, getFont(),
                    getBackground(), getForeground());
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        if (rotatedLabelImage != null)
            g.drawImage(rotatedLabelImage,
                        (d.width - rotatedLabelImage.getWidth(this)) / 2,
                        (d.height - rotatedLabelImage.getHeight(this)) / 2, this);

    }

und

Code:
    public Image createRotatedImage(Graphics fg,String s, FontMetrics fm, Font font,Color backgroundColor, Color foregroundColor) throws InterruptedException {

      int w = fm.stringWidth(s) + 12;
      int h = fm.getMaxAscent() + fm.getMaxDescent() + 2;
      Image img=null;
      Graphics g;

        img = createImage(w, h);

        g = img.getGraphics();
        g.setFont(font);
        g.drawString(s, 10, fm.getMaxAscent() + 1);
        g.dispose();
        int[] pixels = new int[w * h];
        PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
        pg.grabPixels();
        int[] newPixels = new int[w * h];
        for (int y = 0; y < h; y++) {
          for (int x = 0; x < w; x++) {
            newPixels[y + (w - x - 1) * h] = pixels[x + y * w];
          }
        }

        MemoryImageSource imageSource = new MemoryImageSource(h, w,ColorModel.getRGBdefault(), newPixels, 0, h);

        Image myImage = null;
        myImage = createImage(imageSource);

        MediaTracker mt = new MediaTracker(this);
        mt.addImage(myImage, 0);
        mt.waitForAll();

        return myImage;
    }

vielleicht kennt ja jemand was effektiver arbeitendes.

lg
 

thE_29

Top Contributor
Code:
  public class RButton extends JButton
  {
    private int rotateAngle = 90;
    public RButton(String str, int rotate)
    {
      super(str);
      this.rotateAngle = rotate;
    }
  
    public void paintComponent(Graphics g)
    {
      Graphics2D g2 = (Graphics2D) g;
      if(rotateAngle % 90 != 0) //für irgendwelche Winkel ist antialiasing besser
      {
        RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      }
      AffineTransform a = (AffineTransform) g2.getTransform().clone();
      a.rotate(Math.toRadians(rotateAngle), this.getWidth() / 2, this.getHeight() / 2);
      g2.setTransform(a);
      super.paintComponent(g);
    }
  }
 
<center>vernichtend</center>
<center>:shock:</center>
<center>dank' dir für die doch erheblich kürzere Lösung</center>
<center>:toll:</center>
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben