2012-11-18 13 views
5

Ho visto che nel testo di Photoshop può essere facilmente ridimensionato semplicemente trascinandoli. Come possiamo fare la stessa cosa in Java? Qualche idea su come ridimensionare il testo in java? Aggiunto un'istantanea della lettera "A" ridimensionata in PhotoshopCome ridimensionare il testo in java

enter image description here


Per favore fatemi sapere che cosa c'è di sbagliato in questo codice?

public class ResizeImage extends JFrame { 

    public ResizeImage(){ 
     JPanel panel = new JPanel(){ 
      public void paintComponent(Graphics g) { 
       // In your paint(Graphics g) method 
       // Create a buffered image for use as text layer 
       BufferedImage textLayer = new BufferedImage(240, 240, 
               BufferedImage.TYPE_INT_RGB); 

       // Get the graphics instance of the buffered image 
      Graphics2D gBuffImg = textLayer.createGraphics(); 

       // Draw the string 
       gBuffImg.drawString("Hello World", 10, 10); 

       // Rescale the string the way you want it 
       gBuffImg.scale(200, 50); 

       // Draw the buffered image on the output's graphics object 
       g.drawImage(textLayer, 0, 0, null); 
       gBuffImg.dispose(); 
      } 
     }; 
     add(panel); 
    } 

    public static void main(String [] args){ 
     ResizeImage frame = new ResizeImage(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300, 300); 
     frame.setVisible(true); 
    } 
} 
+0

È possibile eseguire il rendering del carattere su un 'BufferedImage' e ridimensionarlo nel modo desiderato. – Spoike

+2

Non è tempo che tu abbia accettato *** una risposta o spiegando perché le risposte offerte non erano adatte? –

risposta

3

u può definire il tipo di carattere

esempio

Font f = new Font("SansSerif", Font.BOLD, 40) 
+0

La dimensione del carattere di 40 aumenterebbe sia la larghezza che l'altezza di una lettera. Ma come puoi vedere nell'immagine qui sopra, viene aumentata solo la larghezza. Come raggiungerlo? – user001

11

Un modo è quello di utilizzare un AffineTransform (questa variante svanisce anche il colore).

Stretch (& fade) using a Serif font for the text

import java.awt.*; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import java.io.File; 
import javax.imageio.ImageIO; 

public class StretchText { 

    public static void main(String[] args) throws Exception { 
     // used to stretch the graphics instance sideways 
     AffineTransform stretch = new AffineTransform(); 
     int w = 640; // image width 
     int h = 200; // image height 
     int f = 21; // Font size in px 
     String s = "The quick brown fox jumps over the lazy dog."; 

     final BufferedImage bi = new BufferedImage(
       w,h,BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = bi.createGraphics(); 
     g.setFont(new Font("Serif",Font.PLAIN,f)); 
     g.setRenderingHint(
       RenderingHints.KEY_TEXT_ANTIALIASING, 
       RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 

     // paint BG 
     g.setColor(Color.WHITE); 
     g.fillRect(0, 0, w, h); 
     g.setColor(Color.BLACK); 

     for (int i=0; (i*f)+f<=h; i++) { 
      g.drawString(s, 0, (i*f)+f); 
      // stretch 
      stretch.concatenate(
        AffineTransform.getScaleInstance(1.18, 1d)); 
      g.setTransform(stretch); 

      // fade 
      Color c = g.getColor(); 
      g.setColor(new Color (
        c.getRed(), 
        c.getGreen(), 
        c.getBlue(), 
        (int)(c.getAlpha()*.75))); 
     } 

     g.dispose(); 

     ImageIO.write(bi, "png", new File(
       new File(System.getProperty("user.home")), 
       "StretchText.png")); 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       JLabel gui = new JLabel(new ImageIcon(bi)); 
       JOptionPane.showMessageDialog(null, gui); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
1

Purtroppo l'API Java non hanno una scala a forma libera nativa/trasformano i caratteri di metodo.

È tuttavia possibile ridimensionare un oggetto BufferedImage o Graphics con scale(x, y) method. Quindi puoi provare un approccio con "layers". Cioè disegnare oggetti, come il testo, nel proprio layer (cioè una BufferedImage) e poi nell'output grafico reale.

Quindi disegna il testo come al solito su una BufferedImage e ridimensionalo nel modo desiderato. Ecco un semplice codice di esempio per iniziare.

// In your paint(Graphics g) method 

// Create a buffered image for use as text layer 
BufferedImage textLayer = new BufferedImage(240, 240, 
            BufferedImage.TYPE_INT_ARGB); 

// Get the graphics instance of the buffered image 
Graphics2D gBuffImg = buffImg.createGraphics(); 

// Draw the string 
gBuffImg.drawString("Hello World", 0, 0); 

// Rescale the string the way you want it 
gBuffImg.scale(240, 120); 

// Draw the buffered image on the output's graphics object 
g.drawImage(gBuffImg, 0, 0, null); 

La dimensione reale del livello di testo potrebbe essere determinato con l'aiuto dei FontMetrics oggetto ma lascio che come un esercizio per l'OP.

4

È possibile utilizzare TextLayout per ottenere la geometria, come mostrato here. L'esempio ridimensiona l'immagine per riempire la cornice. JInternalFrame potrebbe essere una buona scelta per sfruttare la funzione di ridimensionamento del frame. Alternativa, l'esempio citato here mostra come fare clic e trascinare selezioni multiple.

image

+0

Ciao Questo è quello che stavo cercando. Lo verificherò e cercherò di vedere se posso renderlo dinamico .. Grazie ancora – user001

+1

Basta ridimensionare la finestra. – trashgod

+0

Forse aggiungi un 'Timer' che lo allunga dalla posizione per piattaforma (dimensione predefinita) alla dimensione dello schermo, pixel per pixel? Anche se un titolo di "Stretch Me" potrebbe anche farlo. ;) –

0

questo può essere fatto ai Graphics livello utilizzando Graphics.setTransform(). Comunque credo che sia più ovvio farlo a livello di Font usando il meno conosciuto Font.deriveFont(transform). Ad esempio

// create transform 
AffineTransform affineTransform = new AffineTransform(); 
affineTransform.scale(1d, 3d); 

// create font using that transform 
Font stretchedFont = g.getFont().deriveFont(affineTransform); 

// set font as normal 
g.setFont(stretchedFont); 

// render as normal 
g.drawString("Stretched", 23, 45);