A panel is a window component that provides a rectangular subarea in a window. The Java class javax.swing.JPanel allows you to add widgets to a panel with different layouts, in the same manner as you do with window frames. In BreezySwing, the class GBPanel, a subclass of javax.swing.JPanel, provides a more limited functionality. However, panels are useful for decorating windows, displaying graphs and charts, developing interactive sketchpads, and other graphics-based applications.allow programmers perform graphics operations, such as drawing geometric shapes, text, and so forth. A panel can also detect mouse events, such as button presses and releases, mouse cursor movement, and mouse dragging (pressing mouse button and moving the mouse cursor simultaneously).
A BreezySwing panel is added to a window like any other component. In this section, we present two simple examples that illustrate the display of colored panels and the response to mouse presses in them.
The first example program displays four colored panels at startup, and allows the user to change the colors of the panels by clicking a button. The next two screen shots show the initial window and the result of one button click.
import javax.swing.*;
import BreezySwing.*;
import java.awt.Color;
import java.util.*;
public class
PanelDemo1 extends GBFrame{
GBPanel northWest = addPanel(new GBPanel(), 1,1,1,1);
GBPanel southWest = addPanel(new GBPanel(), 1,2,1,1);
GBPanel northEast = addPanel(new GBPanel(), 2,1,1,1);
GBPanel southEast = addPanel(new GBPanel(), 2,2,1,1);
JButton changeColorBtn = addButton("Change color", 3,1,2,1);
private java.util.List<GBPanel>
panels = new ArrayList<GBPanel>();
private
Random numbers = new Random();
public
PanelDemo1(){
northWest.setBackground(Color.red);
southWest.setBackground(Color.green);
northEast.setBackground(Color.blue);
southEast.setBackground(Color.yellow);
panels.add(northWest);
panels.add(southWest);
panels.add(northEast);
panels.add(southEast);
}
public
void buttonClicked(JButton buttonObj){
// Reset all
backgrounds to random colors
for (GBPanel panel : panels)
panel.setBackground(new
Color(numbers.nextInt(255),
numbers.nextInt(255),
numbers.nextInt(255)));
}
public
static void main (String[] args){
JFrame frm = new PanelDemo1();
frm.setSize (200, 200);
frm.setTitle("Random
Colors");
frm.setVisible (true);
}
}
The second example program allows
the user to change the color of a panel by clicking on it directly with the mouse. To
support this response, the programmer must define a new subclass of GBPanel, in much the same manner as we have defined main
window classes to be subclasses of GBFrame.
The program now consists of two files, PanelDemo2.java
(containing the main window class) and ColorPanel.java
(containing the new subclass, named ColorPanel).
The code for the main
window class creates four instances of ColorPanel, passes them their initial colors as arguments,
and adds them to the window. The window's button goes away, as does its buttonClicked method
and its constructor. Here is the code (PanelDemo2.java):
import javax.swing.*;
import BreezySwing.*;
import java.awt.Color;
public class
PanelDemo2 extends GBFrame{
GBPanel northWest = addPanel(new ColorPanel(Color.red), 1,1,1,1);
GBPanel southWest = addPanel(new ColorPanel(Color.green), 1,2,1,1);
GBPanel northEast = addPanel(new ColorPanel(Color.blue), 2,1,1,1);
GBPanel southEast = addPanel(new ColorPanel(Color.yellow), 2,2,1,1);
public
static void main (String[] args){
JFrame frm = new PanelDemo2();
frm.setSize (200, 200);
frm.setTitle("Random
Colors");
frm.setVisible (true);
}
}
Note how much simpler this code is
than the earlier code for PanelDemo1.java.
That's because the new panel class now has the code for setting up the panel and responding to events.
The new panel class, defined in ColorPanel.java, sets the panel's initial background color in the constructor, and includes a method named mousePressed to handle a mouse pressed event. When the user presses the mouse in the rectangular area of a panel, this method, which does nothing in the GBPanel class, is triggered (much like buttonClicked for all buttons and menuItemSelected for all menus). The mousePressed method is overridden in the new panel class, to reset the panel's background to a new random color. Here is the code:
import javax.swing.*;
import BreezySwing.*;
import java.awt.Color;
import java.util.Random;
public class ColorPanel extends GBPanel{
private
Random numbers = new Random();
public
ColorPanel(Color color){
setBackground(color);
}
public
void mousePressed(int x, int y){
setBackground(new Color(numbers.nextInt(255),
numbers.nextInt(255),
numbers.nextInt(255)));
}
}
Note that the mousePressed method takes two integer coordinates as arguments. These will be the coordinates of the mouse press in the panel's rectangular area when the method is triggered. You will see how to use this information to draw graphical shapes in our next examples.
Back to tutorial | Next topic: Simple graphics |