 |
/**
* This demo, provided by Stan Gifford and offered to the Java Tutorial,
* has two menu items: one draws a box outline in the center of the window
* and the other draws a circle outline in the center.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
class new_JLabel extends JLabel {
static int draw_mode=0;
static int width=500;
static int height=500;
public void paint(Graphics g) {
//
// Erase the screen (Set background white)
//
Dimension d=getSize();
width=d.width;
height=d.height;
g.setColor(Color.white);
g.fillRect(0,0,width,height);
if (draw_mode == 1) {dobox(g);}
if (draw_mode == 2) {dooval(g);}
}
public void update(Graphics g) {
}
static void dobox(Graphics g) {
g.setColor(Color.red);
g.drawRect(width/2-25,height/2-25,50,50);
}
static void dooval(Graphics g) {
g.setColor(Color.black);
g.drawOval(width/2-15,height/2-20,30,40);
}
}
public class DiskMap extends JFrame implements ActionListener,
ItemListener {
static new_JLabel output;
static JScrollPane scrollPane;
static Container contentpane;
static String newline = "\n";
final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;
static DiskMap window;
public DiskMap() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JCheckBoxMenuItem cbMenuItem;
JRadioButtonMenuItem rbMenuItem;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Add regular components to the window, using the default BorderLayout.
Container contentPane = getContentPane();
output = new new_JLabel();
output.setOpaque(true);
output.setBackground(Color.white);
scrollPane = new JScrollPane(output);
//Create the menu bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//Build the first menu.
menu = new JMenu("File");
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Draw a Box");
menuItem.addActionListener(this);
menuItem.setActionCommand("1");
menu.add(menuItem);
menuItem = new JMenuItem("Draw an Oval");
menuItem.addActionListener(this);
menuItem.setActionCommand("2");
menu.add(menuItem);
contentPane.add(scrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
if (e.getActionCommand() == "1") {output.draw_mode=1;};
if (e.getActionCommand() == "2") {output.draw_mode=2;};
window.repaint();
}
public void itemStateChanged(ItemEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
}
// Returns just the class name -- no package info.
protected String getClassName(Object o) {
String classString = o.getClass().getName();
int dotIndex = classString.lastIndexOf(".");
return classString.substring(dotIndex+1);
}
public static void main(String[] args) {
window = new DiskMap();
window.setTitle("DiskMap");
window.setSize(500, 500);
window.setVisible(true);
}
}
-- Main.sfshaza - 19 May 2009
|