• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

need help with graphics problem

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my program can run, but some of the vehicle it spawns are cut off in the output. Can someone help me find my problem?

purpose of program: randomly spawns 10 vehicle in random positions.(cars or trucks)











Thanks alot for any help.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this would better sit in the Swing forum, hence moving it.
 
Sheriff
Posts: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to draw the trucks and cars outside their own bounds. You think they will fit because of the call to setBounds, but the layout manager (GridLayout) ignores that and sets new bounds based on the frame size and number of vehicles.
 
Willie Tsang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I tried changing the layout manager and the bounds, but then if i don't use grided layout, only one vehicle will show up. Can you tell me which should i use, or change?
 
Rancher
Posts: 3325
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

purpose of program: randomly spawns 10 vehicle in random positions.(cars or trucks)



Your custom painting is wrong. For example you set the size of your Car to be (80, 55). But then you randomly generate x/y to be between 100/300. So if x/y are (100, 300) then the painting of your cars is done outside the bounds of your Car. Instead your Car painting should always be done from (0, 0). Then to position your cars on the parent panel you use the setLocation(x, y) of the Car.

The code should be something like:





only one vehicle will show up.



Another problem is that panels are opaque, which means that you can't see through the panel so all the panels will paint on top of one another.

Instead try extending JComponent. By default a JComponent doesn't paint a background so you should be able to see all Vehicles separately.
 
Willie Tsang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much, i finally got it to work....
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic