Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help using loops to create a specific design.  RSS feed

 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am completing the exercises in Java How To Program 8th Edition by Harvey and Paul Deitel.
There are two optional exercises GUI and Graphics Case Study Exercises on page 141 Chapter 4.
I have to use loops and control statements to draw lines thereby creating two designs.
I am trying to create the design on Fig. 4.20. The lines do not line up correctly.
The lines should fan out from all corners. Individually, they look exactly like it should.
Example of one corner.


For the other corners I have different starting and ending positions.
To draw upper-left corner I have a starting position of 0,0 and ending position of 0,height.
I then move ending position up one vertical step and right one horizontal step. I repeat this
fifteen times.

To draw upper-right corner I have a starting position of width,0 and ending position of
width,height. I then move ending position up one vertical step and left one horizontal step.

To draw bottom-left corner I have a starting position of 0,height and ending position of
0,0. I then move ending position down one vertical step and right one horizontal step.

To draw bottom-right corner I have a starting position of width, height and ending position of
width,0. I then move ending position down one vertical step and left one horizontal step.

For the second design Fig. 4.21 all my lines line up correctly no matter the direction I
resize it. Both designs frame size is 250,250. Both designs are divided into fifteen steps.
 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I did not make question clear. Why do the lines not line up properly?
I have included screenshots to hopefully make problem clearer.
I tried to include the self-executable jar file, but .jar files are not allowed as attachment in the message.
NotLiningUp.png
[Thumbnail for NotLiningUp.png]
Lines not lining up correctly.
LiningUp.png
[Thumbnail for LiningUp.png]
Second exercise showing lines lining up properly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many programming languages, including java, use something called "integer division". My guess (and it is early and I've had no coffee, so I could be 100% wrong) is that this is causing your problems.

15 / 8 will return 1
15 / 9 will return 1
15 / 10 will return 1

you need to cast one of your terms to a float, and store it in a float (assuming the g.drawLine will take a float-type).
 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It did not quite solve it at first. I had to cast the float to int in the g.drawLine.

I appreciate the help.
Solved.png
[Thumbnail for Solved.png]
It now displays correctly.
 
Mateusz Filipowicz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to follow your instruction to make the upper right corner and fit both upper left and upper right g.drawLines in one "while" loop by specifying each perimeter, but I don't get it why It doesn't allow me.

 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I do not know how to combine the two steps into one while loop.
I tried your code and the only thing I can think of is that it might be the fact that
you are writing etc in the

When you are writing the second g.drawLine() the values of endY will be different.

For my upper left corner, endX is incremented and endY is decremented.
For my upper right corner, endX and endY is decremented.

I don't know if I am allowed to post my full code, but I have posted the starting and ending positions
in the first post.
For each separate while loop, I increment and decrement as follows:
 
Mateusz Filipowicz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Johan, I tried to build new while loops, and declare each parameters as you mentioned in your first post. Though, my lines weren't visible because i haven't initialized new counts in my while loops. Now all corners looks nice and clean !
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!