Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

mix java swing components with java graphics components  RSS feed

 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Iam programming a program which shows the current time as an analog clock, which i construct with the graphics class.
My GUI consists of buttons to set time, start clock or timer from the swing components.
Now I have been reading about java graphics class, iam not sure if it is OK to have both the GUI and graphics on the same layer (JPanel)
and where to implement the graphics methods.


 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

To paint something on JPanel you need to override paintComponent(Graphics g) method.
However javafx8 is forthcoming to provide rich desktop applications. It has built-in clock.
You may download samples and soure code here.
clock.PNG
[Thumbnail for clock.PNG]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would stay away from mixing AWT and Swing components, just to have a unified look and feel, but it is possible. http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html gives a good overview of what you should watch out for.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Ulf. Stick to Swing throughout.

I am moving this discussion to our GUIs forum.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would stay away from mixing AWT and Swing components

I see that he doesn't mix swing and awt components. He uses swing components everywere.
 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers

I dont understand how I can write with the Graphics class on the same JPanel as I use swing components on or should I use different layers and which layers do I need in that case.
Where should my paintComponent method be?, should it be in the same class (MakeGUI) or should I have a class of its own like I have now (PlotGraphics).
Iam a little bit confused about these things
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my experience it is possible to paint and place components in the same JPanel.

But in swing you are in danger to have many troubles with layout and size. That's why you may choose MigLayout. This should help you a lot.

Again if you are brand new to Swing then take a good book.
But to my mind javafx in future will be more asked than swing.
JavaFX provides events like touch, swipe,zoom, 3D graphics. And much less painful layouts.
 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seem like theres no call to paintComponent method, i just don't understand why???




 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Christian,

as Volodymyr said, it IS possible to use one panel for both your components and for doing some drawings like lines and strings.
But I advise strongly against this. For one thing: it is usually up to the layout manager to decide the location and the
size of the components, making it hard for you to prevent drawing right over your components.

Best thing to do is to use one or more panels for your components, and one or more other panels in which you
do your drawings. And only for these drawing panels do you need to overwrite the paintComponent-method. In that
case there is no need for a separate class for the drawings,

Greetings,
Piet
 
Jamie Coleshill
Greenhorn
Posts: 8
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what I tend to do most times.

 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks, i will try to use several panels. one for graphics and one for the swing components.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seem like theres no call to paintComponent method, i just don't understand why???


Because there is no paintComponent() method on a JFrame.

Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the frame. Read the Swing tutorial on Custom Painting for more information and working examples to play with.

I suggest you start with the working example and modify the code for you requirement so you have a better structured GUI that executes on the Event Dispatch Thread.

Also, for animation you should NOT be using a Thread. Instead you should be using a Swing Timer. The tutorial also has a section on using Timers.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If JPanel is used solely as container of components should it have isDoubleBuffered property set to false?
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid such long lines, which make the code very difficult to read. I have sorted out the worst of them, but most of your // comments are unnecessary.
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:If JPanel is used solely as container of components should it have isDoubleBuffered property set to false?


No, leave it as it is.

The only time you could turn double buffering off is when you do the double buffering yourself, for instance
when you have a BufferedImage the size of you panel, and you draw that BI over that panel.

At least, according to me. Maybe someone can add to this (or refute it).

Greetz,
Piet

 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If JPanel is used solely as container of components should it have isDoubleBuffered property set to false?


No, because then the components on the panel would not be double buffered.

Anyway you can't turn double buffering off for a panel as double buffering is set at the frame level. See: Double Buffering Support.

 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody, now I think I know better how to do
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!