Drawstring() and mirror printing.

Discussion in 'Java' started by mariuszpietrzak73@gmail.com, Feb 1, 2014.

  1. Guest

    Hi.
    Please help me to reslove issue. I have to print mirror image of string. I tryed to use AffineTransform and scale but it didnt work well. Mabe have you any idea to do this
     
    , Feb 1, 2014
    #1
    1. Advertising

  2. Stefan Ram Guest

    writes:
    >I have to print mirror image of string.


    The following program creates an image for 10 seconds
    and then shows its mirror immage.

    class Main extends javax.swing.JComponent implements java.lang.Runnable
    { final int[] X ={ 800, 800 };
    private static final long serialVersionUID = 0L;
    java.awt.image.BufferedImage window
    = new java.awt.image.BufferedImage
    ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    java.awt.image.BufferedImage window1
    = new java.awt.image.BufferedImage
    ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    public static void main( final java.lang.String args[] )
    { java.awt.EventQueue.invokeLater( new Main() ); }
    public static int get( final int color, final int shift )
    { return (( color &( 0x000000FF << shift ))>> shift ); }
    public static int B( final int color ){ return get( color, 0 ); }
    public static int G( final int color ){ return get( color, 8 ); }
    public static int R( final int color ){ return get( color, 16 ); }
    public static int reduce( final int intensity ){ return intensity > 127 ? 128 + 64 : 64; }
    public static void limit( final double[] a, final int i, final int max )
    { if( a[ i ]< 0 ){ a[ i ]= 0; return; }else
    if( a[ i ]> max ){ a[ i ]= max; return; }}
    public static int color( final int r, final int g, final int b )
    { return ( r << 16 )|( g << 8 )| b; }
    public static int color( final double[] C )
    { return color( ( int )C[ 0 ],( int )C[ 1 ],( int )C[ 2 ]); }
    @java.lang.Override public final void run()
    { final javax.swing.JFrame frame = new javax.swing.JFrame( this.getClass().getName() );
    Main.this.setPreferredSize( new java.awt.Dimension( X[ 1 ], X[ 0 ]));
    frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
    frame.add( Main.this ); frame.pack(); frame.setVisible( true );
    final java.lang.Runnable loop = new java.lang.Runnable()
    { double[] x ={ X[ 0 ]/2, X[ 1 ]/2 }; double[] C ={ 127, 127, 127 };
    long t = 0; long b = 0; long c = 0;
    public void run(){ if( b == 0L )b = java.lang.System.nanoTime();
    if( b == -1L ){ if( java.lang.System.nanoTime() - c > 1_000_000_000L )
    { final java.awt.image.BufferedImage tmp = window; window = window1; window1 = tmp;
    Main.this.repaint(); c = java.lang.System.nanoTime(); }
    java.awt.EventQueue.invokeLater( this ); }
    else if( java.lang.System.nanoTime() - b > 10_000_000_000L )
    { window.copyData( window1.getRaster() );
    for( int y = 0; y < X[ 0 ]; ++y )for( int x = 0; x < X[ 1 ]; ++x )
    { final int color = Main.this.window1.getRGB( x, y );
    Main.this.window.setRGB( X[ 1 ]- x - 1, y, color ); }
    Main.this.repaint(); b = -1; c = java.lang.System.nanoTime(); java.awt.EventQueue.invokeLater( this ); }
    else { for( int i = 0; i < 100_000; ++i )
    { for( int j = 0; j < 2; ++j ){ x[ j ]+=( java.lang.Math.random() - 0.5 ); limit( x, j, X[ j ] - 1 ); }
    for( int j = 0; j < 3; ++j ){ C[ j ]+=( java.lang.Math.random() - 0.5 ); limit( C, j, 255 ); }
    Main.this.window.setRGB(( int )x[ 1 ],( int )x[ 0 ], color( C )); }
    final long l = java.lang.System.nanoTime(); if( l - t > 100_000L ){ Main.this.repaint(); t = l; }
    java.awt.EventQueue.invokeLater( this ); }}}; loop.run(); }
    @java.lang.Override public void paintComponent( final java.awt.Graphics graphics )
    { graphics.drawImage( Main.this.window, 0, 0, null ); }}
     
    Stefan Ram, Feb 1, 2014
    #2
    1. Advertising

  3. On 2/1/14, 5:34 PM, wrote:
    >
    > Please help me to [resolve] issue. I have to print mirror image of string. I [tried] to use AffineTransform and scale. but it didn't work well. Maybe have you any idea to do this?
    >


    As an alternative to Stefan Ram delightful example, you can alter the
    graphics context's transform to invert through the desired axis. The
    example below uses scale(1, -1) to create a vertical reflection:

    private static class MirrorPanel extends JPanel {

    private static final int SZ = 42;
    private static final String s = "Hello";

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setFont(getFont().deriveFont(42f));
    g2d.drawString(s, SZ, SZ);
    g2d.drawLine(SZ, SZ,
    SZ + g2d.getFontMetrics().stringWidth(s), SZ);
    g2d.scale(1, -1);
    g2d.drawString(s, SZ, -SZ);
    }

    @Override
    public Dimension getPreferredSize() {
    return new Dimension(SZ * 5, SZ * 2);
    }
    }

    See also this related example due to Daniele Futtorovic:

    <http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/2d6db5d6d6739b22>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Feb 2, 2014
    #3
  4. Stefan Ram Guest

    writes:
    >I tryed to use AffineTransform


    The next program is similar to the one I posted before, but
    uses AffineTransform. The mirror animation now transforms
    smoothly between the original image and the mirror image.

    class Main extends javax.swing.JComponent implements java.lang.Runnable
    { final int[] X ={ 800, 800 };
    private static final long serialVersionUID = 0L;
    java.awt.image.BufferedImage window
    = new java.awt.image.BufferedImage
    ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    java.awt.image.BufferedImage window1
    = new java.awt.image.BufferedImage
    ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    public static void main( final java.lang.String args[] )
    { java.awt.EventQueue.invokeLater( new Main() ); }
    public static int get( final int color, final int shift )
    { return (( color &( 0x000000FF << shift ))>> shift ); }
    public static int B( final int color ){ return get( color, 0 ); }
    public static int G( final int color ){ return get( color, 8 ); }
    public static int R( final int color ){ return get( color, 16 ); }
    public static void limit( final double[] a, final int i, final int max )
    { if( a[ i ]< 0 ){ a[ i ]= 0; return; }else
    if( a[ i ]> max ){ a[ i ]= max; return; }}
    public static int color( final int r, final int g, final int b )
    { return ( r << 16 )|( g << 8 )| b; }
    public static int color( final double[] C )
    { return color( ( int )C[ 0 ],( int )C[ 1 ],( int )C[ 2 ]); }
    long l0 = -1L;
    @java.lang.Override public final void run()
    { final javax.swing.JFrame frame = new javax.swing.JFrame( this.getClass().getName() );
    Main.this.setPreferredSize( new java.awt.Dimension( X[ 1 ], X[ 0 ]));
    frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
    frame.add( Main.this ); frame.pack(); frame.setVisible( true );
    final java.lang.Runnable loop = new java.lang.Runnable()
    { double[] x ={ X[ 0 ]/2, X[ 1 ]/2 }; double[] C ={ 127, 127, 127 };
    long t = 0; long b = 0; long c = 0;
    public void run(){ if( b == 0L )b = java.lang.System.nanoTime();
    if( b == -1L )
    { final long l = java.lang.System.nanoTime();
    while( java.lang.System.nanoTime() - l < 100_000_000L )
    { final long l1 = java.lang.System.nanoTime(); if( l0 < 0 )l0 = l;
    final double k = 0.5 - java.lang.Math.cos
    ( ( l1 - l0 )/ 1E9 %( 2 * java.lang.Math.PI )) / 2;
    final java.awt.Graphics2D g =( java.awt.Graphics2D )Main.this.window.getGraphics();
    g.clearRect( 0, 0, X[ 1 ], X[ 0 ]);
    g.setTransform( new java.awt.geom.AffineTransform( 1 - 2 * k, 0, 0, 1, k * X[ 1 ], 0 ));
    g.drawImage( Main.this.window1, 0, 0, null );
    Main.this.paintImmediately( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); }
    java.awt.EventQueue.invokeLater( this ); }
    else if( java.lang.System.nanoTime() - b > 10_000_000_000L )
    { window.copyData( window1.getRaster() );
    Main.this.window.createGraphics();
    b = -1; c = java.lang.System.nanoTime(); java.awt.EventQueue.invokeLater( this ); }
    else { for( int i = 0; i < 100_000; ++i )
    { for( int j = 0; j < 2; ++j ){ x[ j ]+=( java.lang.Math.random() - 0.5 ); limit( x, j, X[ j ] - 1 ); }
    for( int j = 0; j < 3; ++j ){ C[ j ]+=( java.lang.Math.random() - 0.5 ); limit( C, j, 255 ); }
    Main.this.window.setRGB(( int )x[ 1 ],( int )x[ 0 ], color( C )); }
    final long l = java.lang.System.nanoTime();
    if( l - t > 100_000L )
    { Main.this.paintImmediately
    ( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); t = l; }
    java.awt.EventQueue.invokeLater( this ); }}}; loop.run(); }
    @java.lang.Override public void paintComponent( final java.awt.Graphics graphics )
    { graphics.drawImage( Main.this.window, 0, 0, null ); }}
     
    Stefan Ram, Feb 2, 2014
    #4
  5. Silvio Guest

    On 02/02/2014 02:25 AM, Stefan Ram wrote:
    > writes:
    >> I tryed to use AffineTransform

    >
    > The next program is similar to the one I posted before, but
    > uses AffineTransform. The mirror animation now transforms
    > smoothly between the original image and the mirror image.
    >
    > class Main extends javax.swing.JComponent implements java.lang.Runnable
    > { final int[] X ={ 800, 800 };
    > private static final long serialVersionUID = 0L;
    > java.awt.image.BufferedImage window
    > = new java.awt.image.BufferedImage
    > ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    > java.awt.image.BufferedImage window1
    > = new java.awt.image.BufferedImage
    > ( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );
    > public static void main( final java.lang.String args[] )
    > { java.awt.EventQueue.invokeLater( new Main() ); }
    > public static int get( final int color, final int shift )
    > { return (( color &( 0x000000FF << shift ))>> shift ); }
    > public static int B( final int color ){ return get( color, 0 ); }
    > public static int G( final int color ){ return get( color, 8 ); }
    > public static int R( final int color ){ return get( color, 16 ); }
    > public static void limit( final double[] a, final int i, final int max )
    > { if( a[ i ]< 0 ){ a[ i ]= 0; return; }else
    > if( a[ i ]> max ){ a[ i ]= max; return; }}
    > public static int color( final int r, final int g, final int b )
    > { return ( r << 16 )|( g << 8 )| b; }
    > public static int color( final double[] C )
    > { return color( ( int )C[ 0 ],( int )C[ 1 ],( int )C[ 2 ]); }
    > long l0 = -1L;
    > @java.lang.Override public final void run()
    > { final javax.swing.JFrame frame = new javax.swing.JFrame( this.getClass().getName() );
    > Main.this.setPreferredSize( new java.awt.Dimension( X[ 1 ], X[ 0 ]));
    > frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
    > frame.add( Main.this ); frame.pack(); frame.setVisible( true );
    > final java.lang.Runnable loop = new java.lang.Runnable()
    > { double[] x ={ X[ 0 ]/2, X[ 1 ]/2 }; double[] C ={ 127, 127, 127 };
    > long t = 0; long b = 0; long c = 0;
    > public void run(){ if( b == 0L )b = java.lang.System.nanoTime();
    > if( b == -1L )
    > { final long l = java.lang.System.nanoTime();
    > while( java.lang.System.nanoTime() - l < 100_000_000L )
    > { final long l1 = java.lang.System.nanoTime(); if( l0 < 0 )l0 = l;
    > final double k = 0.5 - java.lang.Math.cos
    > ( ( l1 - l0 )/ 1E9 %( 2 * java.lang.Math.PI )) / 2;
    > final java.awt.Graphics2D g =( java.awt.Graphics2D )Main.this.window.getGraphics();
    > g.clearRect( 0, 0, X[ 1 ], X[ 0 ]);
    > g.setTransform( new java.awt.geom.AffineTransform( 1 - 2 * k, 0, 0, 1, k * X[ 1 ], 0 ));
    > g.drawImage( Main.this.window1, 0, 0, null );
    > Main.this.paintImmediately( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); }
    > java.awt.EventQueue.invokeLater( this ); }
    > else if( java.lang.System.nanoTime() - b > 10_000_000_000L )
    > { window.copyData( window1.getRaster() );
    > Main.this.window.createGraphics();
    > b = -1; c = java.lang.System.nanoTime(); java.awt.EventQueue.invokeLater( this ); }
    > else { for( int i = 0; i < 100_000; ++i )
    > { for( int j = 0; j < 2; ++j ){ x[ j ]+=( java.lang.Math.random() - 0.5 ); limit( x, j, X[ j ] - 1 ); }
    > for( int j = 0; j < 3; ++j ){ C[ j ]+=( java.lang.Math.random() - 0.5 ); limit( C, j, 255 ); }
    > Main.this.window.setRGB(( int )x[ 1 ],( int )x[ 0 ], color( C )); }
    > final long l = java.lang.System.nanoTime();
    > if( l - t > 100_000L )
    > { Main.this.paintImmediately
    > ( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); t = l; }
    > java.awt.EventQueue.invokeLater( this ); }}}; loop.run(); }
    > @java.lang.Override public void paintComponent( final java.awt.Graphics graphics )
    > { graphics.drawImage( Main.this.window, 0, 0, null ); }}
    >


    Stefan,

    Would you be so kind to pull code in your posts through some code
    formatting utility? You may choose to use this horrible layout and may
    even have gotten so used to it that you find it legible but for the
    casual followers of threads on this NG it is really painful.

    I for one habitually skip all code in your posts because it takes me at
    least twice the effort to read as the average snippet.

    Silvio
     
    Silvio, Feb 2, 2014
    #5
  6. Guest

    W dniu sobota, 1 lutego 2014 23:34:37 UTC+1 użytkownik napisał:
    > Hi.
    >
    > Please help me to reslove issue. I have to print mirror image of string. I tryed to use AffineTransform and scale but it didnt work well. Mabe have you any idea to do this


    Thank you very much for help. The simple solution but so much time I spent to understand it. Yor solution is really working.
     
    , Feb 3, 2014
    #6
  7. Roedy Green Guest

    On Mon, 3 Feb 2014 07:49:25 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    >I tryed to use AffineTransform and scale but it didnt work well. Mabe have =
    >you any idea to do this


    Now you see how it is done, can you give any hints why you went off
    the rails?
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Because a thing seems difficult for you, do not think it
    impossible for anyone to accomplish.
    ~ Marcus Aurelius 121-04-26 180-03-17
     
    Roedy Green, Feb 3, 2014
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson

    Special characters with DrawString?!

    Jonah Olsson, Oct 1, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,437
    Dmitriy Lapshin [C# / .NET MVP]
    Oct 3, 2003
  2. Xiaolei Li

    No Focused View on DrawString

    Xiaolei Li, Apr 16, 2004, in forum: Java
    Replies:
    3
    Views:
    408
    Xiaolei Li
    Apr 18, 2004
  3. Chris Smith
    Replies:
    1
    Views:
    3,420
    Wolfgang
    May 31, 2004
  4. Dave Ekhaus
    Replies:
    3
    Views:
    10,541
    Darryl Pierce
    May 10, 2005
  5. Mark B
    Replies:
    1
    Views:
    1,641
    Andrew Morton
    Jul 10, 2009
Loading...

Share This Page