already explained above how is the general structure of a program written in Java using JOGL for the time show you how to draw a centered JFrame screen whatever the resolution of it so that later , we can draw into the same few graphics using JOGL . this way begin to study the general structure of a program written in Java why, later, exploring the methods used by GLEventListener .
Here's the code:
/ * Import the JFrame and JPanel classes
contained within the package * SWING * /
import javax.swing. JFrame ;
import javax.swing. JPanel ;
/ * import Container class, BorderLayout,
* Dimension Toolkit AWT package. * /
import java.awt. Container ;
import java.awt. BorderLayout ;
import java.awt. Toolkit;
import java.awt. Dimension ;
/ / Create our main class that extends JFrame class public class
jFrameCentrado extends JFrame {
/ * need a JPanel to enter
* the items displayed in the JFrame. * /
JPanel panel;
/ * declare a container to enter
* our main panel, the container
* is the JFrame in general. * /
Container container;
/ * The toolkit is useful for
* basic information about the computer, how resolution
* or size of the screen, where
* is running the program * /
Toolkit kit;
/ * This variable will use to store
* Dimension (W x H) in pixels, of
* screen where the program is running. * / Dimension
dimensionPantalla;
/ / Variable to store the height, in pixels, screen
int height;
/ / Variable to store the width, in pixels,
screen int width;
/ / Constructor public
jFrameCentrado ()
{/ * Call the superclass of JFrame
* which put a title to it. * /
super ( "Frame centered" )
/ * instantiate an object of Toolkit for
* general data on our computer. * /
Toolkit kit = . GetDefaultToolkit ();
/ * obtain the screen size in pixels * /
kit.getScreenSize dimensionPantalla = ();
/ * store the height and width, in pixels, of the screen.
* Because methods "getHeight" and "getWidth"
return a variable of type double *, we need to force
* this program to convert int variable, ie we will "cast." * /
height = (int ) dimensionPantalla.getHeight ();
width = (int ) dimensionPantalla.getWidth ();
/ * We tell Java that container will be the same JFrame * /
container = getContentPane ();
/ * Instantiate an object of class JPanel in this
* case is empty. * /
panel = new JPanel ();
/ * add the JPanel in the JFrame using the
* prebuilt container, placing the JPanel
* in the center of the JFrame using BorderLayout * /
container.add (panel, BorderLayout. CENTER);
/ * Since we have obtained the height and width, in pixels
* screen, define the size of the JFrame.
* In this case is the size of half the width
* screen for half the height of the screen. * /
this . SetSize (width / 2, height / 2);
/ * Now we place the JFrame exactly in the center of the
* screen is running the program * /
this . setLocation (width / 4, height / 4);
/ * We do resizable and visible * /
this . setResizable ( true);
this . SetVisible (true )
/ * Finally, we tell Java that we want our
* JFrame closes when clicking on demos close button (cross)
* will appear at the top right of it. * /
this . SetDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE)
} / / End of constructor
/ / main method to start execution
our program public static void main (String [] args) {
/ * Simply create a class object
* jFrameCentrado, then the program will run
* constructor code. * /
jFrameCentrado new ();}
} / / End of class jFrameCentrado
When you compile and run the code above, we show a centered JFrame like figure following:
This will be very useful in our subsequent program, as we enter within it with the help of JPanel created graphics created with JOGL . If you want to download the source code you can click on the following insertion in social class:
Download jFrameCentrado.java source
0 comments:
Post a Comment