• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

problem in drawing circle with Action Events

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i was trying to learn Event Handling and i must create a button that when clicked ask for x , y , radius in JOptionPane and draw a circle here my code


so when i run the program it shows the button and when pressed it will ask for input but do`nt draw circle
 
Rancher
Posts: 3785
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All drawing should be done in the paintComponent (or called from) method.  The method would be told what to draw by values controlled by other methods.  If an event happens that should cause some drawing to be done,  the code handling the event would tell paintComponent what to do by setting some values somewhere and call the repaint method which would cause the paintComponent method to be called.  Then paintComponent would look at its drawing instruction values and do the requested drawing.

On closer examination of the code I see that has been done.   Next check would be for the size and location of the JComponent class object.  Is it being shown in the JFrame?
 
Bartender
Posts: 3959
155
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you add something to a JFrame, that something will be placed at the center of the contentPane. That contentPane can only have one component in its center. Now, in the constructor of the frame you add first your circleComponent, and later, the panel with the button, overwriting the circleComponent.

A simple soluton is: add the circleComponent with: add(component, BorderLayout.CENTER) and the buttonPanel with add(panel, BorderLayout.PAGE_START)
 
sahan foraty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:When you add something to a JFrame, that something will be placed at the center of the contentPane. That contentPane can only have one component in its center. Now, in the constructor of the frame you add first your circleComponent, and later, the panel with the button, overwriting the circleComponent.

A simple soluton is: add the circleComponent with: add(component, BorderLayout.CENTER) and the buttonPanel with add(panel, BorderLayout.PAGE_START)


thanks . so i must assemble every component to different layout?? what will happen when i have more component than my number of layout ? or i can add same kind of component to one layout without override it?
 
Piet Souris
Bartender
Posts: 3959
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword here is: LayoutManager.

You can place as many components as you like anywhere you want, by using suitable LayoutManagers. For instance, if you want to have more than one Component in the center of a contentPane, then first create a JPanel, with a default FlowLayout, add as many components to this panel as you like, and add the panel to the frame.

As said: knowledge of LayoutManagers is important. Here is a link to an Oracle tutorial: LayoutManagers
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic