Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java applet wrap around.

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java applet wrap around.

    Hey,

    I'm learning java and I was wondering how to make the text in my applet go down a line
    when I enter the text in the console.

    Thanks!!!

    	int x = 25 - 1;
    	int y= 50 - 1;
     
     
     
     
    	public void paint(Graphics g) {
    		Scanner sc = new Scanner(System.in);
     
     
    		while(true) {
     
     
    			String message = sc.nextLine();
     
    			 g.drawString(message, x, y);
     
     
    		}
     
     
     
     
    	}
    Last edited by allstar2005; April 23rd, 2019 at 01:27 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Java applet wrap around.

    You need to look at FontMetrics or LineMetrics and determine the height of the letter(s) based on the Font. Then you need to adjust the y value accordingly. And applets are pretty much obsolete and very few browsers still support them.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java applet wrap around.

    Thanks, but I'm not sure how to implement that into code as I said I'm pretty new to java.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Java applet wrap around.

    Font f = g.getFont(); (if no font is defined, the set it). See the Font class for details.
    Linemetrics lm = f.getLineMetrics(<arguments go here>);
    int h = lm.getHeight();
    y = y+h;
    Now redraw your string.

    You need to read the Java API to understand this. That is essential to programming in Java.

    Regards
    Jim

  5. #5
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java applet wrap around.

    I'll read the java API thanks for letting me know .

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Java applet wrap around.

    Because this topic can be confusing and it can also be done different ways (FontMetrics vs LineMetrics), I created this example for you to use as a reference. Some of this could be simplified with helper methods. Also, I don't do much of this so there may also be more efficient ways.

    Regards,
    Jim


    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
    public class HelloWorld extends JPanel {
     
       JFrame			 frame		= new JFrame("Hello World");
       Font				 font		= new Font("Arial", Font.PLAIN, 48);
       String			 firstLine	= "Hello";
       String			 secondLine	= "World";
       FontRenderContext frc		= new FontRenderContext(null, true, true);
       LineMetrics		 lm			= font.getLineMetrics("Hello World", frc);
     
       public static void main(String[] args) {
              // schedule for the EDT
              // the method start() just gets us out of static context
    	  SwingUtilities.invokeLater(() -> new HelloWorld().start());
     
       }
     
       public void start() {
              // basic bookkeeping to create a panel in a frame and display
    	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  setPreferredSize(new Dimension(400, (int) (400 * .618)));
    	  setBackground(Color.white);
    	  frame.add(this);
    	  frame.pack();
    	  frame.setLocationRelativeTo(null); // center on screen
    	  frame.setVisible(true);
       }
     
       public void paintComponent(Graphics g) {
    	  super.paintComponent(g);
    	  Graphics2D g2d = (Graphics2D) g;
     
    	  // smooth out the graphics
    	  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    	        RenderingHints.VALUE_ANTIALIAS_ON);
     
    	  g2d.setFont(font);
     
              // Center in the panel.  I guess for the height but 
              // the horizontal centering was computed.
     
    	  int h = (int) lm.getHeight();
    	  TextLayout tm = new TextLayout("Hello", font, frc);
    	  Rectangle2D rect = tm.getBounds();
    	  float w = (float) rect.getWidth();
    	  g2d.drawString("Hello", (400 - w) / 2, 100);
     
    	  tm = new TextLayout("World", font, frc);
    	  rect = tm.getBounds();
    	  w = (float) rect.getWidth();
    	  g2d.drawString("World", (400 - w) / 2, 100 + h);
     
       }
     
    }

Similar Threads

  1. how to wrap the java App as signle executable for Linux
    By Embedded in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 28th, 2013, 12:32 AM
  2. Re: Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2012, 01:54 PM
  3. Trying to wrap my head around Java
    By Sylis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2012, 08:10 AM
  4. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  5. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM