2012-11-18 7 views
5

में टेक्स्ट का आकार कैसे बदलें मैंने देखा है कि फ़ोटोशॉप टेक्स्ट में उन्हें आसानी से आकार देकर आसानी से बदला जा सकता है। जावा में हम वही काम कैसे कर सकते हैं? जावा में पाठ का आकार बदलने के बारे में कोई विचार? पत्र का एक स्नैपशॉट 'ए' में फ़ोटोशॉप आकार दिया गयाजावा

enter image description here


कृपया मुझे पता है कि इस कोड के साथ गलत कौन है?

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

आप 'बुफर्ड इमेज' पर फ़ॉन्ट प्रस्तुत कर सकते हैं और फिर इसे जिस तरह से चाहते हैं उसका आकार बदल सकते हैं। – Spoike

+2

क्या यह समय के बारे में नहीं है *** आपने *** इस पर एक उत्तर स्वीकार किया है या समझाया है कि प्रस्तावित उत्तर उपयुक्त क्यों नहीं थे? –

उत्तर

3

यू फ़ॉन्ट का

जैसे प्रकार परिभाषित कर सकते

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

40 का फ़ॉन्ट आकार एक पत्र की चौड़ाई और ऊंचाई दोनों को बढ़ाएगा। लेकिन जैसा कि आप उपरोक्त छवि में देख सकते हैं, केवल चौड़ाई बढ़ रही है। इसे कैसे प्राप्त करें? – user001

11

एक ही रास्ता (इस प्रकार भी रंग fades) एक AffineTransform उपयोग करने के लिए है।

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

दुर्भाग्य से जावा एपीआई एक देशी मुक्त रूप स्केलिंग नहीं है/विधि फोंट बदलना।

हालांकि आप scale(x, y) method के साथ बुफर्ड इमेज या ग्राफिक्स ऑब्जेक्ट को पुन: सहेज सकते हैं। तो आप इसके बजाय "परतें" के साथ एक दृष्टिकोण का प्रयास कर सकते हैं। अर्थात। टेक्स्ट, जैसे पाठ, अपनी परत में (यानी एक बुफर्ड इमेज) पहले और फिर वास्तविक ग्राफिक्स आउटपुट पर खींचें।

तो बुफर्ड इमेज पर सामान्य रूप से टेक्स्ट खींचें और जिस तरह से आप चाहते हैं उसे पुन: प्राप्त करें। शुरू करने के लिए यहां कुछ सरल नमूना कोड दिया गया है।

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

FontMetrics की मदद से निर्धारित किया जा सकता है पाठ परत के वास्तविक आकार आपत्ति लेकिन मैं ओ पी के लिए एक व्यायाम के रूप कि छोड़ देंगे।

4

आप here दिखाए गए अनुसार, ज्यामिति प्राप्त करने के लिए TextLayout का उपयोग कर सकते हैं। उदाहरण फ्रेम को भरने के लिए छवि को स्केल करता है। फ्रेम की आकार बदलने वाली सुविधा का लाभ उठाने के लिए JInternalFrame एक अच्छा विकल्प हो सकता है। वैकल्पिक, उदाहरण here उद्धृत उदाहरण दिखाता है कि एकाधिक चयनों को कैसे क्लिक करें और खींचें।

image

+0

हाय यह वही है जो मैं देख रहा था। मैं इसे देख लूंगा और यह देखने की कोशिश करता हूं कि क्या मैं इसे गतिशील बना सकता हूं .. धन्यवाद फिर से – user001

+1

बस विंडो का आकार बदलें। – trashgod

+0

शायद एक 'टाइमर' जोड़ें जो इसे प्लेटफ़ॉर्म (डिफ़ॉल्ट आकार) से स्क्रीन-आकार, पिक्सेल द्वारा पिक्सेल द्वारा स्थान से फैलाता है? हालांकि 'स्ट्रेच मी' का एक शीर्षक भी ऐसा कर सकता है। ;) –

0

यह ग्राफिक्स पर किया जा सकता है Graphics.setTransform() का उपयोग कर स्तर पर। हालांकि मुझे विश्वास है कि कम ज्ञात Font.deriveFont(transform) का उपयोग करके फ़ॉन्ट स्तर पर ऐसा करना अधिक स्पष्ट है। उदाहरण के लिए

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