Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4247016

Code sample included for java tutorial

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P3 P3
    • 6u10
    • 1.2.1_03
    • docs
    • None
    • b02
    • generic
    • generic
    • Verified

      Filing this as an RFE. _Roger Pease


      >Date: Fri, 11 Jun 1999 14:44:24 +1000
      >From: Stan Gifford <###@###.###>
      >X-Accept-Language: en
      >MIME-Version: 1.0
      >To: ###@###.###
      >Subject: Code sample included for java tutorial
      >
      >Hi I could not find an example of the following *anywhere*. Should you
      >wish to include it in the online tutorial please feel free.
      >
      >The program is an application.
      >It has a swing menu that draws a box or an oval centred on a window.
      >(Pretty simple Huh?)
      >
      >Enjoy!
      >
      >Stan.
      >
      >--
      >==============================================================================
      > ,-_|\ SSE Internet : ###@###.###
      > / \ Sun Microsystems Australia Pty Ltd.
      > \_,-\_* 828 Pacific Highway Phone : +61 2 9844 5335
      > v Gordon, N.S.W. 2072 Fax : +61 2 9844 5161
      > ACN 003-145-337
      > Disclaimer : Why is everyone flying the wrong way except me?
      >==============================================================================
      >
      >import java.awt.*;
      >import java.awt.event.*;
      >import javax.swing.JMenu;
      >import javax.swing.JMenuItem;
      >import javax.swing.JCheckBoxMenuItem;
      >import javax.swing.JRadioButtonMenuItem;
      >import javax.swing.ButtonGroup;
      >import javax.swing.JMenuBar;
      >import javax.swing.KeyStroke;
      >import javax.swing.ImageIcon;
      >
      >import javax.swing.JTextArea;
      >import javax.swing.JScrollPane;
      >import javax.swing.JFrame;
      >import java.awt.geom.*;
      >import javax.swing.*;
      >import java.awt.image.*;
      >
      >
      >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);
      >
      > }
      >}

            sharonz Sharon Zakhour (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: