• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Programming exercise

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone!
I've just started programming in Java and I'm stuck on this particular exercise.
I need to create a red square of width 50 pixels in the window centre, which then has to grow by 2 pixels every 50 milliseconds until the width is
250 pixels. I've got this to work. Now the square needs to remain at the centre at all times, which means that it has to grow by 100 pixels in each direction of the window(symmetrically). How can this be accomplished in a for loop?
I've written the following code, which increases the width by 2 pixels every 50 millisec. But SquareFigure.setWidth increases the square only downwards and in the right direction:
SquareFigure.create();
SquareFigure.setWidth(50);
for (int i = 1; i < 201; i = i + 2) {
Delay.milliseconds(50);
SquareFigure.setWidth(50 + i);

}
}
}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SquareFigure is a class you wrote? That means I don't know much about it. But to get it in the center of the screen you must have calculated a starting point X & Y probably for the top left corner? Every time you update the square, move the location as well as the size. To grow by 2:
x = x - 1;
y = y - 1;
height = height + 2;
width = height;
Or you could resize the square and then calculate where to put it. Should give the exactly same results unless you get rounding trouble:
heigth = height + 2;
width = height;
x = (screen width - square width) / 2;
y = (screen heigth - square height) / 2;
Think that will work?
 
Amil Mehmedagic
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class SquareFigure is given and consists of the sole object window and methods,which can be sent to this class: moveRight,moveLeft,moveUp,moveDown,moveTo,setWidth,setColour and drawHollow
The absolute center of the window is at (196,134), which means that as the width of the square is 50 pixels, the top-left corner is at (171,109).
The square is supposed to grow to top-left corner of (71,9);top-right corner (321,9); bottom-left corner at (71,259) and bottom-right corner at (321,259); The square is supposed to grow symmetrically to all corners from the middle at once, so that the final width is 250pix.(from 50 pixels originally).
My new code merely changed the side of the growing square, but still doesn't make it grow to all sides(only to the top-left and bottom-left hand side).
public class GrowingSquare {

public static void main ( String [ ] args){

SquareFigure.create();
SquareFigure.setWidth(50);
int oldX = SquareFigure.getXCorner();
int oldY = SquareFigure.getYCorner();
int height = SquareFigure.getYCorner();
for (int i = 1; i < 101; i = i + 2){
Delay.milliseconds(50);
SquareFigure.setWidth(50+i);
SquareFigure.moveTo((oldX = oldX - 2),(oldY = oldY - 2));
height = height + 2;

}

}
}
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amil,
if you could use the UBB code tags, it would make reading your code easier.
i don't understand why you originaly set the height to SquareFigure.getYCorner(), or for that matter, what height is used for.
to get the sqaure to grow, if you increase it's size by 2, you need to move it's top corner by 1. you are moving it 2.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic