Results 1 to 6 of 6
  1. #1
    oldalistair is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default paintComponent() Method straitjacket

    I have been happily building up a familiarity with JAVA and making progress with a program copied over from one which I already have running in Delphi Pascal. But I have now run into what looks like a mighty brick wall. Apparently JAVA requires all graphics coding to be carried out inside a single paintComponent() method! OK for the simple programs I have studied. But my "Cloze" program involves selecting and running from a MENU bar a range of different 'graphics' activities (in reality I only need to use g.drawText) involving the input of words or phrases with their precise screen coordinates to fill gaps in various passages loaded in from a FILE menu. (Word 'jigsaws' so to speak). It looks as I may have to cram several pages of code into one paintComponent() method, but in any case I find I am unable to call this method from anywhere in the program (how can it be called from a Menu?) I thought I had everything working fine by using getGraphics() instead, but my beautiful Cloze passages appear only fleetingly on the screen. Do I need to program a separate class for each activity and then try to call these classes from a menu system?
    I found JAVA so easy and logical but now I am disillusioned. As I said programming this in Delphi Pascal presented no problems of this magnitude.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your paintComponent method can interact with fields in the class, can iterate through lists or arrays of objects and so forth. If you study examples of graphics programming, you'll find many ways to do what you wish with flexibility and OOP-compliance. And FYI, I'm an ex Delphi programmer and will never go back.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Just as an example... many animation programs in Java (at least all of mine, and quite a few others) use Sprites, or small components that are painted. A Sprite can be an interface or an abstract class, or even just a class. They provide a paint or draw or some other method used to output graphics to the screen. Then, whatever component they are being drawn to just calls the method, and all your drawing is done.

    For your case, you'll want menu actions to update a set of variables that indicate what should be drawn where. If I read your post right, you're adding text to the screen, so you may want a Text class that has variables indicating position and a string of text, and possibly a paint(Graphics) method. Then you keep a list of the Text objects you need to draw, and loop through the list painting them every time paintComponent is called. Just don't forget a repaint() call after adding a piece of text!

    EDIT: In case you don't know, repaint() queues a call to paint the component, which eventually calls paintComponent(). repaint(int,int,int,int) does the same for a specific region, so you may want to look at that method as well.
    Last edited by Singing Boyo; 09-04-2010 at 06:09 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    can I just add outside of the actual problem domain, it is very poor to expect one language to be like another language.

  5. #5
    oldalistair is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks to Fubarable and Singing Boyo for their most useful comments. As for old_Marshy_1981's no doubt well-deserved reproach I hereby apologise to Java enthusiasts for inadvertently implying the superiority of another programming language. However, the precise reason for my seeking help in this forum is that, having a mindset created by some previous experience of other languages (Basic, Pascal and C) I have been taken aback by an apparent mountain I had not expected to encounter. I have four related word games, called from a menu, requiring the function drawString for each initial screen display, and four quite complex action-event-triggered user input routines each of which requires drawString. Using getGraphics() for the 8 routines the program compiles and runs perfectly but the initial display for each game vanishes from the screen. I have now tried bundling all the code for the display routines (100 lines of code) into a paintComponent() method, but still cannot figure out how to activate this method.(repaint() didn't help)
    Fubarable is right. I really need to get down to studying more graphics programs to get the feel of this Java way of doing things. I'll no doubt get there in the end. After all everything else I have coded compiles and works.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Talking

    Quote Originally Posted by Singing Boyo View Post
    Just as an example... many animation programs in Java (at least all of mine, and quite a few others) use Sprites, or small components that are painted. A Sprite can be an interface or an abstract class, or even just a class. They provide a paint or draw or some other method used to output graphics to the screen. Then, whatever component they are being drawn to just calls the method, and all your drawing is done.

    For your case, you'll want menu actions to update a set of variables that indicate what should be drawn where. If I read your post right, you're adding text to the screen, so you may want a Text class that has variables indicating position and a string of text, and possibly a paint(Graphics) method. Then you keep a list of the Text objects you need to draw, and loop through the list painting them every time paintComponent is called. Just don't forget a repaint() call after adding a piece of text!

    EDIT: In case you don't know, repaint() queues a call to paint the component, which eventually calls paintComponent(). repaint(int,int,int,int) does the same for a specific region, so you may want to look at that method as well.
    don't you just love the singing one lol as for old_Marshy_1981 reference I will let it slide :D

Similar Threads

  1. paintComponent vs paintComponents
    By alacn in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 03:33 AM
  2. Trouble with paintComponent()
    By ShirlyPunk in forum AWT / Swing
    Replies: 17
    Last Post: 03-13-2010, 04:27 PM
  3. Replies: 10
    Last Post: 02-28-2010, 01:52 PM
  4. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •