Painting with Swing

With Swing, we do not draw text or images directly onto a frame or applet. A component subclass is created, usually a subclass of JPanel, and the painting is performed in the subclasses' paintComponent method. An instance of this component subclass is then created and added to the content pane. The PaintPanel example shows a panel subclass performing painting.

PaintPanel

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Graphics;

class PaintPanel extends JPanel
{
public PaintPanel()
{
setPreferredSize(new Dimension(110, 24));
}


public void paintComponent (Graphics g)
{
super.paintComponent(g);
g.drawString(''Text to be drawn" , 20, getHeight() );
}
}


The constructor, PaintPanel, uses the javax.swing.JPanel.setPreferredSize method to set the size of the panel, in this case, 110 pixels wide and 24 pixels high. The first statement in the paintComponent method must be

super.paintComponent(g);

This causes the component to paint its background. The java.awt.Graphics.drawString method is used to paint the text. Two useful methods in javax.swing.JComponent that can be used with drawString are getHeight and getWidth. These return the current component height and width, respectively.An instance of PaintPanel is created and then added to the content pane, cp, as follows:

PaintPanel p = new PaintPanel();
cp.add(p);

CustomerDetails

import javax.swing.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Container;

public class CustomerDetails extends JFrame
implements ActionListener, ItemListener
{
JTextArea addressArea;
JScrollPane addressPane;
JLabel addressLabel;
Jlabel freqLabel;
JComboBox freqButton;
JCheckBox tradeButton;
JRadioButton age1;
JRadioButton age2;
JRadioButton age3;
JRadioButton age4;
ButtonGroup ageButton;
JPanel agePanel;
JPanel freqPanel;
Container cp;

String[] comboString={''first time","occasionally", "frequently"}

public CustomerDetails()
{

/* set up layout panels */
cp = this.getContentPane();
cp.setLayout(new GridLayout(5,1));
agePanel = new JPanel();
agePanel.setLayout(new GridLayout(4,1) );
freqPanel = new JPanel();
freqPanel.setLayout(new GridLayout(1,2) );

/* set up name address label */
addressLabel = new JLabel("Enter Name and Address:");
cp.add(addressLabel) ;

/* set up name address text area */

addressArea = new JTextArea(3,12);
addressPane = new JScrollPane(addressArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
cp.add(addressPane);

/* set up Trade Customer Checkbox button */

tradeButton = new JCheckBox("Trade Customer", false);
tradeButton.addItemListener(this);
cp.add(tradeButton);

/* set up shopping frequency combo box */

freqLabel = new JLabel(
"How often do you shop with us: ");
freqPanel.add(freqLabel);
freqButton = new JComboBox(comboString);
freqButton.addActionListener(this);
freqPanel.add(freqButton);
cp.add(freqPanel);
/* set up age range radio button */
ageButton = new ButtonGroup();
age1= new JRadioButton(''age under 20”);
age2= new JRadioButton(“20-39”);

age3= new JRadioButton(“40-59”);
age4= new JRadioButton(“over 60”);

ageButton.add(age1);
ageButton.add(age2);
ageButton.add(age3);
ageButton.add(age4);
age1.addActionListener(this);
age2.addActionListener(this);
age3.addActionListener(this);
age4.addActionListener(this);


agePanel.add(age1);
agePanel.add(age2);

agePanel.add(age3);

agePanel.add(age4);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
// actionPerformed is ActionListener interface method
// which responds to action event of selecting
// combo box or radio button
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JComboBox)
{
System.out.println("Customer shops:” +freqButton.getSelectedItem());
}
else if (e.getSource() instanceof JRadioButton)
{
if (age1.isSelected() )
{
System.Out.Println(“customer is under 20”);
}
else if (age2.isSelected() )
{
System.Out.Println(“customer is under 20-39”);
}
else if (age3.isSelected() )
{
System.out.println("Customer is 40 - 59");
} else if (age4.isSelected() ) {
System.out.println("Customer is over 60");
}
}

}

// itemStateChanged is ItemListener interface method
// which responds to item event of clicking checkbox
public void itemStateChanged (ItemEvent e)
{
if (e.getSource() instanceof JCheckBox)
{
JCheckBox buttonLabel = (JCheckBox)
e.getItemSelectable();
if (buttonLabel == tradeButton){
if (e.getStateChange() == e.SELECTED) {
System.out.println(''Customer is trade");
} else
{
System.out.println("Customer is not trade");
}
}
}
}

// main method creates CustomerDetails frame
public static void main(String args[]) {
CustomerDetails cd = new CustomerDetails ();
cd.setTitle("Customer Details Screen");
cd.setSize(400,600);
cd.setVisible(true);
}
}

No comments:

Post a Comment