• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet Troubles

 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question deals with an example taht I am trying to attempt from a book. I would liek to have some help or guidance in solving this problem.

Write an applet that reads in the size of the side of a square and displays a hollow square of that sizw out of asterisks, by using the drawString method inside your applets paint method. Use an input dialog to read the size from the user. Your program should work for squares of all lengths of side between 1 and 20.

Here is how I attemped this problem thus far.

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

public class Ch4_23 extends JApplet
{
int number1;
int number2;
int number3;
int number4;
int number5;

double modulus;
double sum;
public void init()
{
String firstNumber;
String secondNumber;
String thirdNumber;
String fourthNumber;
firstNumber = JOptionPane.showInputDialog(" Enter the first floating pont number");
secondNumber = JOptionPane.showInputDialog(" Enter the second floating point number");
thirdNumber = JOptionPane.showInputDialog(" Enter the first floating pont number");
fourthNumber = JOptionPane.showInputDialog(" Enter the second floating point number");
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
number3 = Integer.parseInt( thirdNumber );
number4 = Integer.parseInt( fourthNumber );
sum = number1 % number2;




}
public void paint( Graphics g)
{
super.paint( g );
g.drawRect( number1, number2, number3, number4);
number3 = number2 + 10;
number4 = number2 + 20;
number5 = number2 + 30;
g.drawString("***", number1, number2 );
g.drawString("* *", number1, number3 );
g.drawString("***", number1, number4 );






}
}



This isn't really getting the square highlighted with asterisks. It is however off when I attempt this. Can someone please guide me to figuring this one out?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you describe with an example what the program is doing and what you want it to do that is different? That would save us a lot of time.

For example:
The current output: ***123***

And I want: ** 23 **
 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output I am seeking is for the asterisks to form a square with asterisks. LIke

****
* *
****

But, the output I am recieving is

****
---
| | ( This is supposed to be a squarelol I just didnt know how to
--- type a square onto this message lol)

I am trying to come up with an outcome of entering the dimensions of the square and it will output that square highlighted by asterisks, once again as so

****
* *
****
 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOH!
****
* *
****

It is supposed to be a square I dsont know why it is not forming a square even in this message. =( But, as I said it is supposed to form a square imprinting with asterisks.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the Font fixed width? A space is probably not as wide as an *
 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, how would i be able to set that equal? The main objective I have is the output being asterisks forming a square and the size of the square would be determined by the inputs I put in before.
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your problem was getting the * to line up in columns with separating spaces, then you need to set the Font to Monospaced to maintain the * in columns.
Or is your problem creating the needed number of * and spaces?
Can you describe the algorithm you are using?
For example if you want a 5 by 5 square:
Create a string with 5 * and one with a leading and ending * with 5-2 intervening spaces
print the full * string
loop to print the * spaces * line 5-2 times
print the full * string
replace 5 with a variable to generalize the code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic