0

I come from an Objective-C background and I have been developing on the iOS and Mac OS platform for a number of years now. I love the concept of custom drawing and therefore I would like to replicate what had been done by me on the aforementioned platform.

In iOS I would call -drawRect() to draw a custom view and then, I could go ahead and add other visual components on top of that drawing. I know that there are equivalents in java swing and awt called paint() and paintComponent(). However, I am not quite sure how the drawing goes about. I do not know when to call the super method (beginning or end ?) and how to add other components to the custom drawn component (custom drawing sometimes appears on top, sometimes beneath).

Can someone shed some light on this ?

I have already read up a little bit on when to use what, but I am still not 100% sure, so can anybody explain when exactly to use paint() and when paintComponent() ?

the_critic
  • 12,720
  • 19
  • 67
  • 115
  • You could take a look at [this answer](http://stackoverflow.com/questions/18005505/why-does-one-have-to-use-the-paintcomponent-method-to-draw-in-java/18005806#18005806) and [this answer](http://stackoverflow.com/questions/12175174/paintcomponent-vs-paint-and-jpanel-vs-canvas-in-a-paintbrush-type-gui/12175819#12175819) – MadProgrammer Dec 02 '13 at 19:50

1 Answers1

2

Start with the java tutorials.

http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
  • +1 for the tutorial link which covers the basics and contains working examples. Quick summary, don't override paint(), custom painting is done in the `paintComponent()` method. – camickr Dec 02 '13 at 19:10
  • This answer did not really help me -- I had already skimmed this tutorial earlier this day. However, I guess it will lead others to useful resources, so I accepted your answer. My problem was, that I had not been aware of how to use layout managers. I used null (none) and it expected a preferred size from me, whereas I had only set the panel's bounds. I have read and am aware of the fact that you must not use no layout manager (null), but it serves the purpose in my case. – the_critic Dec 02 '13 at 22:57
  • Your question had nothing to do with layout managers. Custom painting and layout managers are largely unrelated topics. You can override paintComponent and draw things however you want while the layout manager determines how your component fits inside your container. – Dodd10x Dec 03 '13 at 15:17
  • Well you really should not blame me, because I did not know what had been wrong, so I obviously could not get my point across... I thought I had misunderstood the whole concept, but it turns out, it is exactly how I think it should be, only that the behaviour had been faulty due to the layout manager and the preferredSize... – the_critic Dec 04 '13 at 21:35
  • I'm not blaming you, merely addressing that my answer could not possibly have helped you with layout managers if you had not asked about them or implied you were having any specific problem that could be related to them. In the future if something isn't working correctly post an example of the problem so people can troubleshoot it properly. – Dodd10x Dec 05 '13 at 14:19