I have some doubts programming graphics in java, can you help me?
now i always use (in PC with windows) a class that extends from jframe, and i override the paint method, even i use doblebuffer, etc.
but i saw that other codes add a jpanel to the jframe, and use the jpanel method paint... other codes use canvas and use paint method... or extends from an applet, and add to the applet a canvas object, and use the canvas paint method...
can you tell me why these differences? my graphics code works fine with jframe, but i will need to use it in android, maybe in android i must use applet?
thanks in advance.
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.
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 have been playing about with some simple painting of Graphics2D and have some extremely good help from the community here.
I managed to get the flickering resolved from my "bouncy balls" by moving the code away from the main JFrame class and into a JPanel which I then added to the JFrame class, can anyone tell me why this would make such a difference?
When you draw in a JComponent's paintComponent method (such as a JPanel's), you use Swing which uses double-buffering when drawing by default. Drawing directly in a JFrame's paint method will only allow AWT type drawing since the JFrame directly inherits from Frame, a heavy weight container, and since AWT graphics does not use double buffering by default and this will lead to choppy animation.
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.