Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Free and open-source graphics device driver wikipedia , lookup
Hold-And-Modify wikipedia , lookup
Stereoscopy wikipedia , lookup
BSAVE (bitmap format) wikipedia , lookup
Apple II graphics wikipedia , lookup
Waveform graphics wikipedia , lookup
Graphics processing unit wikipedia , lookup
Framebuffer wikipedia , lookup
javax.swing.JPanel javax.swing.JPanel, a subclass of JComponent, builds general-purpose GUI containers that you can use to organize other components or display computer graphics. To use a JPanel object to organize other components: 1. 2. 3. 4. 5. Build the JPanel object. Embellish it as desired. Give it an appropriate layout manager. Add components to it. Add the panel to the containment hierarchy. JComponent JPanel Example Here is a window that is an interface to a desktop calculator. The buttons are organized into three separate JPanel objects, containing: clear buttons number buttons entry buttons The clear and entry buttons are in JPanel objects that use 2 × 1 grid layout managers. Here’s the code that builds the clear buttons: JButton btnCE = new JButton( "CE" ); JButton btnC = new JButton( "CLEAR" ); The following code builds the left panel and adds these buttons to it. JPanel leftPanel = new JPanel( ); leftPanel.setLayout( new GridLayout( 2, 1, 2, 2 ) ); leftPanel.add( btnC ); leftPanel.add( btnCE ); javax.swing.JPanel Page 1 The following code builds the center panel: JPanel centerPanel = new JPanel( ); centerPanel.setLayout( new GridLayout( 4, 3, 2, 2 ) ); The number buttons are kept in an array. Here’s the code to build them: JButton [] btnList = new JButton[10]; for ( int k = 0; k < btnList.length; k++ ) btnList[k] = new JButton( Integer.toString( k ) ); The decimal point button must be built separately. The bottom left corner of the number pad is an unused button containing a blank caption added to the panel as filler. JButton btnDot = new JButton( "." ); JButton btnBlank = new JButton( " " ); Components are added to a grid layout in row-major order – i.e. starting with coordinate (0, 0) and proceeding by rows. Since this doesn’t match the order of the buttons in the array, it is easiest to add each button to the panel separately. centerPanel.add( centerPanel.add( centerPanel.add( centerPanel.add( centerPanel.add( centerPanel.add( etc. btnList[7] btnList[8] btnList[9] btnList[4] btnList[5] btnList[6] ); ); ); ); ); ); The entry buttons are similarly built and added to a third panel. Finally, the three panels are added to the west, center and east areas of the frame’s border layout. win.add( leftPanel, BorderLayout.WEST ); win.add( centerPanel, BorderLayout.CENTER ); win.add( rightPanel, BorderLayout.EAST ); javax.swing.JPanel Page 2 A JPanel can also be used to render computer graphics. The following material assumes that you are familiar with the material in the collection Graphics, Color, Font. In particular you must understand the topic Graphics Context. A JPanel object is visually rendered by its own internal paint method named paintComponent, whose declaration appears as: void paintComponent( Graphics g ) Whenever the JVM determines that the JPanel object needs to be rendered – such as when it is resized or maximized – it automatically calls paintComponent and passes the graphics context to parameter g. Thus, to customize the rendering of a JPanel object: Create your own subclass of JPanel. Morph its inherited paintComponent method. Within paintComponent call the appropriate Graphics methods in parameter g. Example This application creates the window shown to the right. The subclass of JPanel is shown on the next page. 1 2 3 4 5 6 7 8 9 10 11 12 13 import javax.swing.*; public class MyApp { public static void main( String [] args ) { JFrame win = new JFrame( "Panel Graphics" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.add( new MyPanel( ) ); win.setSize( 350, 150 ); win.setVisible( true ); } } javax.swing.JPanel Page 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { public void paintComponent( Graphics g ) { super.paintComponent( g ); // call superclass g.setColor( Color.RED ); g.setFont( new Font( "Trebuchet MS", Font.PLAIN, 24 ) ); g.drawString( "Welcome to Graphics", 20, 50 ); g.setColor( Color.BLUE ); g.drawRect( 20, 52, 250, 6 ); } } Exercises 1. Using the code from the first example, write a complete Java program (application or applet) that uses JPanel objects to build and display the window shown to the right. The calculator’s display (showing 0) is a JTextField that has been added to the north area of the window. javax.swing.JPanel Page 4 2. Modify your solution to exercise #1 so that the window includes a display of the calculator’s internal accumulator (see the picture to the right). This is yet another JPanel object added to the south area of the window. It contains a JLabel object (containing the text Accumulator) and a JTextField object (showing 0). 3. Write a complete Java program that builds and displays the GUI shown below. Two JPanel objects are used: one for the full name and one for the mailing address. Put titled borders around them. 4. Write a complete Java application that uses a JPanel object painting the picture shown to the right. The picture must resize in proportion to the window, as shown below. javax.swing.JPanel Page 5 5. Write a complete Java application that uses a JPanel object painting a complicated picture of your own design. Here are two samples to inspire your creativity. javax.swing.JPanel Page 6