0

I'm making a game in Java. I found this tutorial: http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

It shows how to do active rendering. They use a Canvas to paint. I've been using JPanel's up until this point; however, I'd like to switch over to this method. Now, I have several JPanels and some JButtons added to the JPanels. I would like to know if I should switch over to java.awt.Button and java.awt.Panel instead so that I don't mix Swing and AWT Components.

On the other hand, in the tutorial the Canvas is inside a JFrame, so isn't that already mixing Swing and AWT? Is that okay?

Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
helsont
  • 4,347
  • 4
  • 23
  • 28

2 Answers2

3

You should be using only Swing components. E.g. You should go from Button to JButton and all the replacing controls.

AWT Component                         (Closest) Swing Replacement  
Button                                          JButton  
Canvas                                          JPanel  
Checkbox                                        JCheckBox  
Checkbox in CheckboxGroup                       JRadioButton in ButtonGroup  
Choice                                          JComboBox  
Component                                       JComponent  
Container                                       JPanel  
Label                                           JLabel  
List                                            JList    
Menu                                            JMenu  
MenuBar                                         JMenuBar  
MenuItem                                        JMenuItem  
Panel                                           JPanel  
PopupMenu                                       JPopupMenu   
Scrollbar                                       JScrollBar  
ScrollPane                                      JScrollPane  
TextArea                                        JTextArea  
TextField                                       JTextField  

But some aspects are common to both AWT and Swing e.g event handling and layout mgmt is common between two as well

Cratylus
  • 52,998
  • 69
  • 209
  • 339
3
  • you can to use JComponent (haven't implemented LayoutManager) or JPanel (implemented FlowLayout) without any issue (in compare with Canvas) for painting with code compiled in Java6/7

  • have to change method paint(AWT Component) to the paintComponent(Swing JComponent), more in the trail Graphics(2D)

  • there are reasons for using Canvas in 21th century, for example for high perfomance or hard graphics code that required access to the resources that came from Native OS, e.g. graphics for CAD / CAM, 3D and graphics based on OpenGL/CL, multimedia players

  • use Swing Timer instead of plain Thread

  • use KeyBindings instead of KeyListener

mKorbel
  • 109,525
  • 20
  • 134
  • 319