2

I had to make an applet that shows its own source code. This is what I have:

 //Reference the required Java libraries
 import java.applet.Applet; 
 import java.awt.*; 

 //The applet code
 public class FirstApplet extends Applet {

     public void paint(Graphics g) {

       //Draw a rectangle width=250, height=100
       g.drawRect(0,0,250,600); 

       //Set the color to blue
       g.setColor(Color.blue); 

       //Write the message to the web page
       g.drawString("\n //Reference the required Java libraries\n import java.applet.Applet;\n import java.awt.*; \n //The applet code\n public class FirstApplet extends Applet {\n     public void paint(Graphics g) {\n       //Draw a rectangle width=250, height=100\n      g.drawRect(0,0,250,100); \n       //Set the color to blue\n       g.setColor(Color.blue); \n       //Write the message to the web page\n       g.drawString",10,50); 
    }
 } 

However, the \n is not making a new line. My text continues horizontally until finished. How would I crate new lines inside the g.drawString field?

user2913004
  • 123
  • 1
  • 3
  • 8
  • 1
    Increase the y value by the text height of the previous line? – Josh M Oct 29 '13 at 14:56
  • I tried making the rectangle larger, still didn't work. – user2913004 Oct 29 '13 at 14:57
  • That would be help. [http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d-drawstring][1] [1]: http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d-drawstring – MugosDynamic Oct 29 '13 at 14:59

2 Answers2

0

Perhaps you could try something like this (Untested):

public static void draw(final Graphics g, final int startX, final int startY, final String... lines){
    final FontMetrics metrics = g.getFontMetrics();
    final int spacing = metrics.getHeight() + metrics.getMaxDescent();
    draw(g, startX, startY, spacing, lines);
}

public static void draw(final Graphics g, final int startX, final int startY, final int vSpacing, final String... lines){
    int y = startY;
    for(final String line : lines){
        g.drawString(line, startX, y);
        y += vSpacing;
    }
}

The first method will calculate the height (based on the height and descent of the current Font of your Graphics object). The second method allows you to input a custom vertical spacing value for more customizability.

Josh M
  • 11,611
  • 7
  • 39
  • 49
0

I had to make an applet that shows its own source code.

Two alternatives:

  1. Use AppletContext.showDocument(URL) to browse to the source file.
  2. Use a JTextArea with JTextComponent.read(Reader,Object) to read the source.

BTW

  1. Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
  2. Why AWT rather than Swing? See this answer on Swing extras over AWT for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see Mixing Heavyweight and Lightweight Components.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433