Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Displaying a box  RSS feed

 
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am currently in the middle of a certification program and doing quite well but now I am a bit stuck.

I have two classes in the program, one defines a box based on drawRectangle, the other is a simple Applet to display the box.

This is the Box - Class (if there is a mistake here, do not tell me I want to figure this out myself):



And this is the stub of the Applet to display a grid of boxes:



A hint in the right direction would be highly appreciated.
 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • What have you tried?
  • Do you know how to reference a class from an external file?
  •  
    Marcus Rauchfuss
    Ranch Hand
    Posts: 51
    1
    Eclipse IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Reference a Class?
    No I don't know that.
     
    Marcus Rauchfuss
    Ranch Hand
    Posts: 51
    1
    Eclipse IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have to display a few box-objects from the box Class in the Grid Applet, how do I go about that. Whenever I try the paint-method in the Applet, it does not work.
     
    Master Rancher
    Posts: 3001
    105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Marcus,

    I can't speak for Knute of course, but what I think he meant was: there is no
    'package' statement at the top of your classes. So, does the Applet class
    know where to find the Box class?

    And if that is not what he meant, well, he will let us know.

    Meanwhile, you are very close to the solution. What comes next is
    from the bottom of my memory, but I think I have it right.

    The painting sequence for an Applet (that extends Panel), is as follows:

    1)
    The method 'update(g)' is called. It clears the panel to the background
    This will be done by the system.

    2)
    then paint(Graphics g) is called, and that is where your method comes in.
    Since your panel is already cleared to the background, you do not have to
    do that in this method. So, all you have to care about are your boxes.

    Now look again at your Box class. You see the method 'display(Graphics g)',
    so each box knows how to draw itself, given a Graphics g.

    Now, what do you think you need to do in the paint method, given
    that you have a list of all your boxes?

    Greetz,
    Piet

     
    Marcus Rauchfuss
    Ranch Hand
    Posts: 51
    1
    Eclipse IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Piet,

    frankly, I have no idea, what to do next. I tried implementing a paint() method in Grid, to no avail.


    And about the package: Both classes are in the same package/project in Eclipse.
     
    Piet Souris
    Master Rancher
    Posts: 3001
    105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Marcus,

    okay. We're in the 'paint' method, we are given a Graphigs g by the system,
    so if you want to draw 'box01', what about:


    Greetz,
    Piet
     
    Marcus Rauchfuss
    Ranch Hand
    Posts: 51
    1
    Eclipse IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, I just found that out, through a suggestion by Eclipse, too. ;-)

     
    Piet Souris
    Master Rancher
    Posts: 3001
    105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Marcus,

    yes, these IDE's can be a great help. Success in finishing the job!

    Greetz,
    Piet
     
    Bartender
    Posts: 3321
    86
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Box class should extend java.awt.Component. The Box classes display() method should be called paint() and you do not need the paint() method in the Grid class.

    Now you can just add your Box instances to the Grid instance using one of the add() methods and the boxes will draw themselves as and when required.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!