2

If I want to display ellipses and rectangles on a screen, should I use a canvas or a JPanel?

What is the difference? When do I use each?

Anonymous181
  • 1,863
  • 6
  • 24
  • 27
  • use the Swing component. – user1329572 Apr 30 '12 at 18:31
  • See also [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for the many reasons to use Swing rather than AWT. – Andrew Thompson Apr 30 '12 at 19:22
  • Use `JPanel` for generic tasks. You should only use `Canvas` if you require more control with regards to painting (for instance game titles would use Canvas as they require different buffering strategies, setting FPS and so forth). In a sense, don't agree with many of the answers here; each have their own niche, and Swing runs on top oi the AWT. – Maarten Bodewes Aug 14 '22 at 12:25

4 Answers4

5

Canvas is an AWT object; JPanel is a lightweight Java Swing object. If you have a Java Swing GUI, I'd strongly recommend using JPanel.

Here's a good link on JPanel:

In the simplest case, you use a JPanel exactly the same way as you would a Panel. Allocate it, drop components in it, then add the JPanel to some Container. However, JPanel also acts as a replacement for Canvas (there is no JCanvas)...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Broken link, I would have liked to read it since this post is 8 years old –  Jul 03 '20 at 16:21
  • 1
    Here is another, different link to the same chapter: [Core Web Programming, Marty Hall, Larry Brown](https://books.google.com/books?id=q45_UDI77PoC&pg=PA585&lpg=PA585&dq=However,+JPanel+also+acts+as+a+replacement+for+Canvas+(there+is+no+JCanvas)...&source=bl&ots=RVHQqXgzW8&sig=ACfU3U1xjQaJJ9DnnnKMJLkf8r0nfEXcag&hl=en&sa=X&ved=2ahUKEwig4aH5_7PqAhVEUKwKHaBhBAAQ6AEwAHoECAoQAQ#v=onepage&q=However%2C%20JPanel%20also%20acts%20as%20a%20replacement%20for%20Canvas%20(there%20is%20no%20JCanvas)...&f=false) – paulsm4 Jul 04 '20 at 16:22
3

In general, if you're using Swing, you should only use Swing components. Mixing Swing and AWT components in the same GUI leads to strange results. So I would use a JPanel, or a raw JComponent.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Or you can use a JLabel if you want display static images like icons.

BufferedImage image=
  new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();

// draw
g2.draw(new Ellipse2D.Double(x, y, rectwidth,rectheight));
g2.fill (new Ellipse2D.Double(0, 0, 100, 50));

JLabel label = new JLabel(new ImageIcon( image ));
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

That kind of depends on what you like, but, I would use JFrame, it directly creates a window, and you can draw onto it. Canvas has its advantages, it let's you write one less import, it also separates window stuff from canvas graphics stuff, but it has its the disadvantages of not having access to window. it depends on what you like and need.