How To Draw On A Jframe
Game programming: JFrame, JPanel, paint method
To paint something nosotros first demand a surface where to paint on. This surface or canvas where nosotros are going to paint our starting time example is a JPanel object. In the same fashion a sheet needs a frame to hold information technology, our JPanel will exist framed in a window made past the JFrame class.
JFrame: The window
The following lawmaking creates a window "Mini Tennis" of 300 pixels past 300 pixels. The window won´t be visible until we call setVisible(truthful). If we don´t include the concluding line "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)", when we shut the window the programme won´t finish and will continue running.
package com.edu4java.minitennis1; import javax.swing.JFrame; public course Game { public static void main(String[] args) { JFrame frame = new JFrame("Mini Tennis"); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } If we run it nosotros will obtain:
With these few instructions we will obtain a window which tin can be maximize, minimize, modify it´s size with the mouse, etc. When nosotros create a JFrame object nosotros start an engine which manages the user interface. This engine communicates with the operative organisation both to paint in the screen equally to receive information from the keyboard and from the mouse. Nosotros volition call this engine "AWT Engine" or "Swing Engine" because it is made by these two libraries. In the offset java versions only AWT existed and so Swing was added. This engine uses several threads.
What is a thread in java?
A program is executed by just i processor, line past line. Threads allow a program to first several executions at the same time. This is as if, there were several processors running at the same time their ain sequence of instructions.
Even though threads and concurrence are very powerful tools, there tin be problems when two threads enter the same variables. It is interesting to retrieve that two threads can be running the aforementioned code at the aforementioned time.
We can think that a thread is like a melt preparing a dish reading a recipe. Two concurrent threads would be like ii cooks working in the same kitchen, preparing one dish with the same recipe o with differents recipes. The problems come when both try to utilise the same frying pan at the same fourth dimension.
AWT Engine and Thread AWT-EventQueue
The AWT Engine starts several threads which can exist seen if we commencement the aplication with debug and we go to the debug perspective. Each thread is every bit if it was an contained programme running at the aforementioned fourth dimension as the other threads. Further on we will run across more most threads, meanwhile I am but interested that you lot remember the third thread we see in the debug view called "Thread [AWT-EventQueue-0]" this thread is the one in charge of painting the screen and receiving the mouse and keyboard events.
JPanel: The canvas
To be able to paint nosotros want to know WHERE and where is an JPanel object which will be included in the window. We extend the JPanel class to be able to overwrite the pigment method which is the method chosen by the AWT Engine to paint what appears in the screen.
package com.edu4java.minitennis1; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class Game2 extends JPanel { @Override public void paint(Graphics yard) { Graphics2D g2d = (Graphics2D) yard; g2d.setColor(Color.Reddish); g2d.fillOval(0, 0, xxx, 30); g2d.drawOval(0, 50, 30, 30); g2d.fillRect(50, 0, 30, 30); g2d.drawRect(fifty, 50, 30, 30); g2d.draw(new Ellipse2D.Double(0, 100, xxx, 30)); } public static void main(String[] args) { JFrame frame = new JFrame("Mini Tennis"); frame.add(new Game2()); frame.setSize(300, 300); frame.setVisible(truthful); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } The paint method receives by parameter a Graphics2D object which extends from Graphics. Graphics is an one-time class used past AWT which has been replaced with Graphics2D which has more than and better functionality. The parameter is all the same a Graphics blazon due to compatibility but we volition utilize Graphics2D, so we need to create a variable g2d "Graphics2D g2d = (Graphics2D) g;". Once we have g2d nosotros can use all the Graphics2D methods to draw.
The beginning thing nosotros practise is cull the colour we use to draw: "g2d.setColor(Color.Ruby-red);". Afterward, we draw circles and squares.
Positioning in the canvas. Coordinate "10" and "y"
To depict something inside the canvass we should indicate in which position we are going to start painting. For this, each of the points in the canvas has an associated position (10,y) being (0,0) the point of the elevation-left corner.
The first ruby-red circumvolve is painted with "g2d.fillOval(0, 0, 30, 30)": the start 2 parameters are the position (x,y) and after comes the width and the acme. As a consequence nosotros take a circumvolve with thirty pixels of diameter in the position(0,0).
The empty circle is fatigued with "g2d.drawOval(0, 50, 30, xxx)": which draws a circumvolve in the position 10=0 (left margin) and y=50 (50 pixels below the top margin) with a summit of 30 pixels and a width of 30 pixels.
Rectangles are fatigued with "g2d.fillRect(50, 0, 30, 30)" and "g2d.drawRect(fifty, 50, 30, 30)" in a similar manner to the circles.
Lastly "g2d.draw(new Ellipse2D.Double(0, 100, 30, 30))" draws the last circle using an Ellipse2D.Double object.
At that place are a lot of methods in Graphics2D. Some of them will be seen in the following tutorials.
When does the AWT engine telephone call the pigment method?
The AWT engine calls the paint method every fourth dimension the operative organisation reports that the canvas has to be painted. When the window is created for the kickoff time paint is called. The paint method is also called if nosotros minimize and after we maximize the window and if we change the size of the window with the mouse.
We can picket this behaviour if nosotros put a breakpoint in the first line of the paint method and nosotros run the program in the debug style.
It is interesting to see that the pigment method is ran by the Thread AWT-EventQueue, which is the one in charge of painting the screen.
Source: http://www.edu4java.com/en/game/game1.html
Posted by: parkblegame94.blogspot.com

0 Response to "How To Draw On A Jframe"
Post a Comment