0

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Square extends JApplet {
int size;
public void init()
{
String input = JOptionPane.showInputDialog(
"Enter square size:");

size = Integer.parseInt( input );
}
public void squareOfCharacters( Graphics g )
{
String input = JOptionPane.showInputDialog(
"Enter Character:");
int y = 50, x = 5;
for ( int a = 1; a <= size * size; a++ ) {
g.drawString( "\n%s!\n", x += 5, y );

}
}
public void paint( Graphics g )
{
squareOfCharacters( g );
}
}

I am having the worst time with this. I am supposed to form the square out of whatever character is contained in character parameter fillcharacter. Thus, if side is 5 and fillcharacter is"#", the method should display
#####
#####
#####
#####
#####

Thanks..............

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Phaelax
0

First, you should take out the JOptionPane from teh squareOfCharacters method, otherwise you'll be prompted every time the applet repaints.

And instead of using a single FOR loop, use two nested FOR loops. The outer loop would be your row counter (up-down) and the inner loop would be your columns(left-right).

Rather than draw each individual characters of every line, build a string of the needed length (in this case 5) and then draw the string row once.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.