Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Paint the screen without paint methods?  RSS feed

 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of this question maybe about 20 seconds ago and have a feeling I'll know the answer right after I hit submit but hey, let's give it a shot.

Was just thinking, is it possible to paint to the screen without using methods such as paint(), paintBorder(), paintChildren, paintBorder, paintComponent, paintAll() and any other paint like methods I may have missed. These are, by the way, taken both from JComponent and Component. So, is it possible?
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Was just thinking, is it possible to paint to the screen without using methods such as paint()...

I could be wrong, but I cannot think of a way. That's really what the paint related methods are designed to do when working with Swing components.
To clarify, though, of all the methods you listed - in most situations you should only ever need to override the paintComponent(Graphics g):

Then repaint the screen by calling the repaint() method. The other methods are all handled internally by repaint() and normally don't need to be touched.
This post I wrote a while ago explains in more detail, and has a link to a post on Oracle's site with additional information you might find interesting.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, I have read quite extensively on the matter. However, I find it constraining that we are limited to use the Java API when having the option to paint. Now, I might be touching at a fundamental theory of Java that using the API makes your life, as a programmer, much easier and more efficient. I would like to stray from that ideology to wonder if there is a possible way to "Hard Code" your own painting methods or even an entire painting API. I understand that the only paint method you will only ever have to override is the paintComponent() method, doing this allows you to build your own custom drawings on your component. This is great and allows us to not have to reinvent the wheel but it would be nice to know I have the option to create my own methods that do work with the JVM and the underlying operating system.

As I become more familiar with the Java language I find questions, such as these, that pressure me to find ways to start at the fundamentals and work my way up. I feel that with the basic syntax and concepts of Java I should be able to, in theory, recreate the entire Java API. This is in no way what I want to do and would ever want to do but the thought still looms over my head...

In addition to this there may be another question deserving an answer. Whenever a Java program is created and paints something to the screen, this could be a game or an application for graphing functions, are they ALWAYS using these paint methods? Or is it possible these developers devised their own methods that paint more efficiently as they are directed towards their applications own needs?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Was just thinking, is it possible to paint to the screen without using methods such as paint(), paintBorder(), paintChildren, paintBorder, paintComponent, paintAll() and any other paint like methods I may have missed.

If your 'other paint like methods I may have missed' includes Icon#paintIcon(...) then, no.

You can use the Icon interface with a component that has a setIcon(...) method, probably JLabel. You will still need to call repaint() after any change of state that changes the way the Icon is to be painted.

 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:I would like to stray from that ideology to wonder if there is a possible way to "Hard Code" your own painting methods or even an entire painting API.

Why reinvent in the wheel? Generally speaking, creating frameworks that replicate functionality that is already provided in order to simply say "I did that" isn't a good use of your time and energy. You would be very hard pressed to single handedly build a more efficient and lighter weight GUI system than that developed by Oracle.

Now, games are not built with Swing (serious games, anyhow). You might want to check out some of the open source Java-based initiatives if you are interested in game development. Game Engines such as JMonkey. JMonkey uses the Java language as an interface to control OpenGL.

That said, truly high-end games are not usually generated using Java. If we are talking about Call of Duty quality games, those are built in custom C++ based frameworks and engines that interface with OpenGL. You need the flexibility that C based languages give you with controlling direct memory block access in order to squeeze out the most performance you can.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a programmer it's great to view things in the abstract. Just knowing that something does something for you is mostly all you will ever need. An example of this is that I know that there are methods that paint for me in Java, but I do not necessarily know how they work under the hood. I am interested in how they work, though. By reinventing the wheel I really am just allowing myself to see how Oracle made those methods happen, which is very interesting to me. I also believe that understanding how your tools work is almost as important as knowing how to use them well. This belief, however, is something I am trying to walk away from as I become more knowledgeable in Computer Science. Too much complexity leads to an overwhelming amount of possibilities. A great example of knowing just enough is that I may not know exactly how my car works...what I do know is that when I press that gas pedal I am going to start moving. Which is a nice, abstract view on how my life in transportation works. Similarly, knowing how my computer works from the ground up is mostly not necessary for learning Java, or any programming language for that matter. Nor is it necessary for me to understand every aspect about how Java works behind the scenes to be a good programmer. The only reason I wonder of fundamentals is because I do like to know how these things work. And in the end, learning them is extremely satisfying.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!