• 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

Problem painting component

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I made a component called GradientLegend but I am having trouble to properly display it on a JFrame. The component is painted and immediately after disappears, leaving the frame empty (at least that's what happens when I compile and run the code below with java 1.4.1 for linux). By the tutorials I've been through the code seems to be right, does anyone find what's wrong? The code follows.
Thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
I'm not sure why you're passing in the x,y location of the component as constructor parameters; in any case, the problem is that these are wrong as soon as the container is laid out. The wrong coordinates lead to the image being drawn in an offscreen location, so you can't see it.
Instead of passing in x, y, use getLocation().x and getLocation().y in paintComponent(). You should find that your program works perfectly.
 
Leonardo Fernandes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, I just found out what the problem was. I use x and y as parameters for the caller to decide where does it want to show the gradient. In the paintComponent() method I was increasing y all the time, and when the component was repainted (which seems to happen right after the first paint), y was greater then the component height. I set y to it original value at the end of paintComponent() and now it works fine. By the way, how do I become something better than a greenhorn?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You automatically get to be a "ranch hand" after you've posted some number of times -- 30 times, I think? If you want something more colorful than that, you can either work very hard and be elected a bartender... or you can take the easy way out!
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic