The Artima Developer Community
Sponsored Link

Java Answers Forum
Problems with manipulating drawString

2 replies on 1 page. Most recent reply: Aug 19, 2002 7:46 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Hang Ping

Posts: 2
Nickname: hangping
Registered: Aug, 2002

Problems with manipulating drawString Posted: Aug 17, 2002 3:14 PM
Reply to this message Reply
Advertisement
Hi I have a small code where I have to pass a number which will be used to draw a square e.g. if I pass 3 then it would be:
***
***
***

However, I cannot seem to have a clue where I am going wrong, especially when I am trying to manipulate the drawString method. Any help is appreciated. Thanks.
FULL CODE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BuildSquare1 extends JApplet {

public void init()

{




}


public void paint( Graphics g )

{

super.paint( g );

String strNumber;
int number;
strNumber = JOptionPane.showInputDialog( "Enter number to represent square side" );
number = Integer.parseInt( strNumber );

String result;

result = DisplaySquare( number );

g.drawString(result, 25, 40);


}


public String DisplaySquare( int a )
{

String output = "";

for (int row = 1; row <= a; row ++) {

for (int column = 1; column <= a; column++ ) {

output += output + "* ";

}
output += "\n";

}

return output;


}

}


Javed

Posts: 15
Nickname: javedm
Registered: Aug, 2002

Re: Problems with manipulating drawString Posted: Aug 19, 2002 1:55 AM
Reply to this message Reply
Hi,

It would be more helpful if we can have more details like, what is the output, what is the error message, if any.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Problems with manipulating drawString Posted: Aug 19, 2002 7:46 AM
Reply to this message Reply

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BuildSquare1 extends JApplet {

private String numberString = "3";

public void init(){
boolean done = false;
numberString = JOptionPane.showInputDialog( "Enter number to represent square side" );
}

public void paint(Graphics g){
int number = toInt(numberString);
for (int row = 1; row <= number; row ++) {
String nextLine = "";
for (int column = 1; column <= number; column++ ) {
nextLine += nextLine + "*";
}
g.drawString(nextLine, 10, 10 + row*10);
}

}

private int toInt(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
n = -1;
}
return n;
}


}

Flat View: This topic has 2 replies on 1 page
Topic: passing serialized object from servlet to applet Previous Topic   Next Topic Topic: Problem with targetting another frame

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use