9 Replies - 459 Views - Last Post: 25 March 2012 - 12:11 PM Rate Topic: -----

#1 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 13-February 11

Thinking about beginning to use a Canvas

Posted 25 March 2012 - 07:04 AM

I've been googling around and searching on here - and while I haven't found a ton, I've found some interesting stuff.

Thus far I've always used Swing. I've created a JFrame and draw directly on it (I don't bother creating a JPanel - probably bad). For the most part, I'm more interested in creating my own custom components. That is, I don't use buttons, labels, textfields, etc - I draw everything to the screen and if I want to make something clickable, I just figure out where the mouse was clicked, and figure out what was in that location.

My goal, really, is that I'm working on a 2d project. I'm drawing everything myself. It's a game in the idea of a KairoSoft game (like GameDev Story). I eventually want to try isometric drawing, and, as I said, I'm drawing all the controls myself - I'm not going to use buttons or labels or any of that stuff.

Should I be moving over to this Canvas? I'd actually never heard of it before - but it searching for some ideas, I'd noticed people using it in their tutorials. It seems to pretty much work the same, except you override some other things, and apparently it's a little more efficient or smooth when drawing.

So would I be out of line here to be switching over if I'm not planning on actually using any JComponents? And would I switch over to threads in this case, instead of the Swing Timer?

I'm a little confused because I notice people creating the canvas on a JFrame - and isn't that going into Swing, then? My understanding is that a Canvas is AWT - so why am I tossing it on a Swing component? Do I still use Swing programming ideas (Timers?), or what?

Just looking for a little guidance.

Thanks for reading.

Is This A Good Question/Topic? 1
  • +

Replies To: Thinking about beginning to use a Canvas

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:10 AM

The general rule of thumb is to avoid mixing AWT and Swing components. From what I understand, AWT components are always drawn on top of swing components, so you may have z-ordering issues with certain things.

Why is it that you want to use Canvas anyway? Swing is already double buffered, and there is little point in using a BufferStrategy in a windowed application.

For a sim like Game Dev Story, I recommend you use a Swing Timer. There won't be so much going on screen that you need to worry about combining repaint() requests.

This post has been edited by farrell2k: 25 March 2012 - 10:12 AM

Was This Post Helpful? 1
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:25 AM

I know at first, that a library you're not familiar with like Swing can be daunting. I was tempted to roll my own too, but honestly, you are going to have to reinvent a LOT of stuff for you to have decent controls. What's wrong with extending JComponent for your controls? You can make them any size, and do all of the same custom drawing in paintComponent for it, but you get the added functionality of so much more. Like adding it to a container, having the LayoutManager position it, being able to use a ComponentListener, etc.

As for Canvas, well - just have a look at why the lightweight framework was designed.

http://docs.oracle.c...ghtweights.html

If Canvas is the only thing your using and you are drawing all of your controls yourself, then I doubt there is any downside to using it. But I don't recommend drawing all the controls yourself to begin with...
Was This Post Helpful? 1
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:35 AM

View PostSheph, on 25 March 2012 - 05:25 PM, said:

I know at first, that a library you're not familiar with like Swing can be daunting. I was tempted to roll my own too, but honestly, you are going to have to reinvent a LOT of stuff for you to have decent controls. What's wrong with extending JComponent for your controls? You can make them any size, and do all of the same custom drawing in paintComponent for it, but you get the added functionality of so much more. Like adding it to a container, having the LayoutManager position it, being able to use a ComponentListener, etc.

As for Canvas, well - just have a look at why the lightweight framework was designed.

http://docs.oracle.c...ghtweights.html

If Canvas is the only thing your using and you are drawing all of your controls yourself, then I doubt there is any downside to using it. But I don't recommend drawing all the controls yourself to begin with...


You linked to a document from 1997...

A JPanel with a swing timer is perfect for him.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:40 AM

Unless you are implementing yourself your double buffering like in here

http://www.dreaminco...way-to-repaint/

no need to use a Canvas
Was This Post Helpful? 2
  • +
  • -

#6 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 13-February 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:53 AM

Thanks, guys. I'll just stick with Swing.

I questioned it because a lot of examples for things I was looking up used this Canvas - and I found some posts saying to use it if you're not using Swing Components. Although, perhaps it is a better idea to make the components look and act the way I want, instead of just ignoring them.

The whole mixing swing/awt thing is a confusion for me. I see it written quite a bit - but then come examples of how to do things in swing, that involve using classes from the awt library. For example - overriding Paint() to paint onto a JPanel - it uses Graphics, which you need to import awt to use - so right there we've already had to mix awt with swing. I just haven't seen it explained well enough yet for it to make sense to me.

*edit - I suppose the overriding paint() is bad - too. I've been reading to override paintComponent() instead, so I'm just getting used to doing that now. But, still, the paintComponent uses the Graphics class.

This post has been edited by NantucketSleighride: 25 March 2012 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:57 AM

Lets rephrase the sensence: "Do not mix Swing and AWT drawable component" which is not the case of Layout, ActionEvent, MouseEvent, Graphics, ....
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 10:57 AM

View Postfarrell2k, on 25 March 2012 - 12:35 PM, said:

You linked to a document from 1997...

A JPanel with a swing timer is perfect for him.

I fail to see how the year of the document has to do with anything. The page has good information on why the lightwork framework was implemented. I was showing it to him so he could understand why Canvas might not be the best approach instead of telling him: "Oh hey use JPanel and a Swing Timer" like we say for every other post on this forum without explaining exactly WHY to the poor soul, and then they trust us so blindly without even bothering to question us. A little challenge toward normality is good, and I thought this was the case. He was challenging the preconceived notion to use Swing and it brought forth some research on my own part to figure out exactly WHY we don't mix AWT and Swing. Heck, I didn't know myself - I just trusted you guys before.
Was This Post Helpful? 0
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 11:20 AM

View PostNantucketSleighride, on 25 March 2012 - 12:53 PM, said:

*edit - I suppose the overriding paint() is bad - too. I've been reading to override paintComponent() instead, so I'm just getting used to doing that now. But, still, the paintComponent uses the Graphics class.

Here is another place where we just assume overriding paint is bad. Let's figure out why or why not. There are three methods called from the paint() method of a JComponent, which consist of paintComponent(), paintBorder(), and paintChildren(). Most of the drawing in a Component consists of paintComponent() - the actual thing you're painting. You don't need to override the other two. (if you added a Border to the component, you shouldn't override paintBorder() for instance) You can override paint() but unless you call super.paint(g) none of those three methods would get invoked. Typically you put things in paintComponent and let Swing handle default painting because that is how it was designed to be used.

I would say, technically it's not "bad" you just have to understand the consequences of doing one over the other.

http://docs.oracle.c...ing/closer.html
Was This Post Helpful? 1
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: Thinking about beginning to use a Canvas

Posted 25 March 2012 - 12:11 PM

View PostSheph, on 25 March 2012 - 06:20 PM, said:

View PostNantucketSleighride, on 25 March 2012 - 12:53 PM, said:

*edit - I suppose the overriding paint() is bad - too. I've been reading to override paintComponent() instead, so I'm just getting used to doing that now. But, still, the paintComponent uses the Graphics class.

Here is another place where we just assume overriding paint is bad. Let's figure out why or why not. There are three methods called from the paint() method of a JComponent, which consist of paintComponent(), paintBorder(), and paintChildren(). Most of the drawing in a Component consists of paintComponent() - the actual thing you're painting. You don't need to override the other two. (if you added a Border to the component, you shouldn't override paintBorder() for instance) You can override paint() but unless you call super.paint(g) none of those three methods would get invoked. Typically you put things in paintComponent and let Swing handle default painting because that is how it was designed to be used.

I would say, technically it's not "bad" you just have to understand the consequences of doing one over the other.

http://docs.oracle.c...ing/closer.html


Good point. You also need to be aware of when to call super.paint(), if you decide to override paint(). You may end up drawing over components. See below.

	public class TestFrame extends JFrame {
	private JPanel panel;
	
	public TestFrame() {
		setSize(300,300);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//JPanel
		panel = new JPanel();
		panel.setBackground(Color.red);
		
		//add components.
		add(panel, BorderLayout.CENTER);
	}

	public void paint(Graphics g) {
		super.paint(g); //draws over components.
		g.setColor(Color.blue);
		g.fillRect(0, 0, 100,100);
		//super.paint(g); //doesn't draw over components.
	}
	
	public static void main(String[]args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					new TestFrame().setVisible(true);
				}
				catch(Exception e) {
					//Log Headless or IllegalArgumentExceptions.
				}
			}
		});
	}//end main()
}//end class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1