Results 1 to 1 of 1
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default I wonder when resizing it wont print the centered text again?

    I wonder when resizing it wont print the centered text again?


    Java Code:
    import java.awt.*;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.awt.font.*;
    import java.awt.geom.*;
    
    public class HorizontallyCenteredText {
      public static void main(String[] args) {
    
        final Frame frame = new ApplicationFrame("HorizontallyCenteredText v1.0") {
    
          @Override
          public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            Rectangle clipBounds = g2.getClipBounds();
            int width = (int) clipBounds.getWidth();
            int middle = width /2;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    
            g2.setFont(new Font("Serif", Font.PLAIN, 48));
    
            paintHorizontallyCenteredText(g2, "come", middle, 75);
            paintHorizontallyCenteredText(g2, "and", middle, 125);
            paintHorizontallyCenteredText(g2, "play", middle, 175);
          }
    
          protected void paintHorizontallyCenteredText(Graphics2D g2, String s,
              float centerX, float baselineY) {
            FontRenderContext frc = g2.getFontRenderContext();
            Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
            float width = (float)bounds.getWidth();
            g2.drawString(s, centerX - width / 2, baselineY);
          }
        };
        frame.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    frame.repaint();
                    System.out.println("componentResized");
                }
        });
    
    
        frame.setVisible(true);
      }
    }
    Java Code:
    public class ApplicationFrame extends Frame {
            public ApplicationFrame() {
                this("ApplicationFrame v1.0");
            }
            public ApplicationFrame(String title) {
                    super(title);
                    createUI();
            }
            protected void createUI() {
                setSize(500, 400);
                center();
    
                addWindowListener(new java.awt.event.WindowAdapter()  {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e ) {
                        dispose();
                        System.exit(0);
                    }
                });
            }
    
            public void center() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                Dimension frameSize = getSize();
                int x = (screenSize.width - frameSize.width) / 2;
                int y = (screenSize.height - frameSize.height) / 2;
                setLocation(x, y);
    
    
            }
    }
    Last edited by willemjav; 03-15-2014 at 09:57 AM.

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2012, 11:06 AM
  2. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  3. wont print zero
    By jjth39347 in forum New To Java
    Replies: 16
    Last Post: 03-27-2011, 01:35 AM
  4. Program compiles but wont run to text file...
    By marylanddem in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 04:05 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •