I have a canvas that is drawing everything in a paintComponent() method.
Is it possible to draw outside of paintComponent (without calling a method within paintComponent?)
If so how do you go about doing this?
It depends what you mean and why you need it. For example, it is possible to create a BufferedImage, get the Graphics2D object, Graphics.paint() everything that should be on the image, then drop the image into a JLabel.
But since I do not know what you are trying to achieve (as opposed to what you are trying to do) I cannot know if that answer solves the unstated problem.
I found out how to solve this issue.
What I did was make JPanel an inner class to my JFrame class.
In JPanels paintComponent I had it calling a method from the outer class which did some updating of the graphics, by passing paintComponents Graphics2D object.
This has allowed me to paint "outside" of paintComponent, just as I needed.
Related
I am attempting to make a "Pong" game, and I am struggling to draw the Rectangle that will be the "Racket". I have attempted to use Java2D graphics, but it just doesn't want to work for me. The 2 links below will show the "Racket" class and "Window" class. The Racket class is where I draw the Racket for the game, and the Window class is where I make the JFrame. Racket Class Window Class
It's supersimple. How could your Window class know that it should draw your
Racket?
I tell you what todo but I won't provide code, because you only posted an image.
Extend JFrame instead of just instantiating it (or JPanel for reusage purposes)
Override the paint method (don't forget the super.paint(g) call)
Create an instance of Racket as a member of Window class
Call racketInstance.paint(g)
These are the simplest steps to follow if you want it to be drawn. But for your whole target this is not the best approach.
You must extend JPanel and override the method paint(Graphics g). In this method, you must draw the Racket.
In your class Window, add your JPanel subclass instead of a standard javax.swing.JPanel.
I am kind of newbie in Java and in my program, I have to draw some lines, arcs etc. in a function different from paintComponent() in some cases in a class that extends JPanel.
I generally work with ActionScript for visual stuff and I can draw any geometric figure in any functions when I work with AS.
So, is there way to do the same thing in Java, and how?
Thank you!
Why not draw in paintComponent(...)? You state your new, and perhaps you might misunderstand how to do Swing graphics best, so knowing your requirements and the rationale behind them can help us help you.
You can draw on a BufferedImage at any time and in any code
Then that image can be displayed either in a JComponent's paintComponent(...) method
or in a JLabel's Icon via its setIcon(...) method.
Be sure to read the Graphics2D API which will have all the methods needed to do your drawings.
Be sure to read the Swing graphics tutorials because doing this type of work requires a paradigm shift.
Do not get your Graphics object by calling getGraphics() on a component such as a JPanel. Either get it via calling getGraphics() on a BufferedImage or from the paintComponent(Graphics g) parameter.
when I have JPanel, it has it´s default Graphics which is passed to paint(Graphics g) and similiar function. Can I somehow switch that default Graphics for my own? From outside of the JPanel class? I am looking for something like JPanel.setGraphics(Graphics g). Thank you.
No, but you may override its paintComponent method, cast the Graphics object passed as argument to Graphics2D, and draw whatever you want on it.
As far as I know, the only way to control which Graphics object is passed around is to enable the debug graphics option. This is done by calling JComponent.setDebugGraphicsOptions(int), which will replace the original Graphics object by an instance of javax.swing.DebugGraphics.
The instanciation of DebugGraphics is hardcoded in the method getGraphics of class JComponent, so I see no way to use your own implementation here (other than using the instrumentation of the JVM to rewrite the code, as mock libraries do).
I'm creating a simple 2D game in java. I've only done this in C++ with the Windows API so far. In java, I'm getting a Graphics object by extending JFrame and calling this.getContentPane().getGraphics(). With this object, I'm drawing everything to the screen, every frame. This raises a few questions:
Backbuffer: I'm not getting any flickering effects, while I'm not using a backbuffer (I'm drawing directly on the Graphics object). Why is this? Does java has a built-in backbuffer or something?
Animations: I'm used to put all animation parts in a single sprite sheet, like in this example:
http://www.envygames.com/share/sample_animation.jpg
Now, someone has told me that you can just draw animated .gif's and java will draw these independent of the game loop. I've tried this out and it doesn't seem to work. Is this true or am I also supposed to use these sprite sheets in java?
Thanks
Yes, Java has a double buffer rendering strategy that can be switched on and off...
Java: how to do double-buffering in Swing?
About the animated gifs, I think it is right, but you may have to put them in the appropriate container (maybe as the icon of a JLabel?).
getting a Graphics object by extending JFrame and
calling this.getContentPane().getGraphics().
don't painting directly to the JFrame for Custom Painting you have to look for JLabel that allows painting everything, another choise will be extending JCompoments, or JPanel for that
for painting in the Swing you have to look for paintComponent(Graphics g), not paint(Graphics g), because this method are lots of time used in examples and ditributed on some of Java ExamplesDepots, that's wrong method with possible lacks
I would like to have a vertical line drawn over a JPanel, and make it glide over it, without this process invoking the paintComponent() of the JPanel. I have thought of using the GlassPane but I don't think it is the correct way since there are other components in the frame containing the JPanel, and so it isn't specific to it (and I'm not actually sure it wouldn't call the paintComponent() anyway).
Any ideas?
Maybe you should be using Layered Panes if you just want to isolate the line painting code from the rest of your painting code.
If your painting code is expensive, then maybe you should be creating a BufferedImage and then just redraw the image in your paintComponent() code. This is faster than repainting from scratch every time.
Even with a GlassPane the underlying component would have to be repainted at some point. There is not a nice way to avoid a paintComponent call to JPanel.
However, the JPanel should not be doing things in paintComponent other than painting. If you are trying to avoid calling it, then it sounds like you have something in the paintComponent method that needs to be changed or cached somehow.
Is there a reason why you do not want to call the paintComponent() method on the JPanel? Repainting the object to render the line gliding over it will most likely be the easiest solution.