Untitled

By Hot Tamarin, 2 Weeks ago, written in php.
  1. package draw;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class SimpleDraw extends Frame
  6.         implements      WindowListener,
  7.                                 ActionListener,
  8.                                 ItemListener,
  9.                                 KeyListener {
  10.         private static final long serialVersioUID = 1L;
  11.        
  12.         SimpleDrawCanvas drawCanvas;
  13.         Panel drawControls;
  14.        
  15.         //buttons:
  16.         Button north, east, south, west;
  17.         Choice upDown; //comboBox with color
  18.         MenuBar menuBar;
  19.         Menu simpleDrawMenu;
  20.         MenuItem preferencesItem;
  21.        
  22.         private class WindowEventProcessor extends WindowAdapter{
  23.        
  24.                 public void windowClosing(WindowEvent e){
  25.                         System.exit(0);
  26.                 }
  27.                 public void windowOpened(WindowEvent e){
  28.                         SimpleDraw.this.requestFocus();
  29.                 }
  30.         }
  31.        
  32.        
  33.        
  34.         public SimpleDraw(){
  35.                 super("SimpleDraw");
  36.                
  37.                 menuBar = new MenuBar();
  38.                 simpleDrawMenu = new Menu("Simple Draw");
  39.                 preferencesItem = new MenuItem("Preferences ...");
  40.                
  41.                 setMenuBar(menuBar);
  42.                 menuBar.add(simpleDrawMenu);
  43.                
  44.                 //ToDo: ActionListener
  45.                
  46.                 simpleDrawMenu.add(preferencesItem);           
  47.                
  48.                 // define layout manager
  49.                 setLayout(new GridLayout(1,2));
  50.                 drawCanvas = new SimpleDrawCanvas();
  51.                 drawControls = buildControlPanel();
  52.                
  53.                 add(drawControls);
  54.                 add(drawCanvas);
  55.                
  56.                 //V1: window-listener hinzufügen
  57.                 // addWindowListener(this);
  58.                
  59.                 //V2: anonym inner class
  60. //              addWindowListener(new WindowAdapter(){
  61. //                      public void windowClosing(WindowEvent e){
  62. //                              System.exit(0);
  63. //                      }
  64. //                      public void windowOpened(WindowEvent e){
  65. //                              requestFocus();
  66. //                      }
  67. //              });
  68.                
  69.                 //V3: seperate class
  70.                 addWindowListener(new WindowEventProcessor());
  71.                 addKeyListener(this);
  72.                
  73.                 setSize(600, 300);
  74.         }
  75.        
  76.         private Panel buildControlPanel(){
  77.                 Panel panel = new Panel();
  78.                 panel.setLayout(new GridLayout(3,3));
  79.                 panel.setBackground(Color.white);
  80.                
  81.                 panel.add(new Label());
  82.                 panel.add(north = new Button("North"));
  83.                 north.addActionListener(this);         
  84.                 panel.add(new Label());
  85.                
  86.                 panel.add(west = new Button("West"));
  87.                 west.addActionListener(this);
  88.                
  89.                 upDown = new Choice();
  90.                 upDown.add("black");
  91.                 upDown.add("green");
  92.                 upDown.add("blue");
  93.                 upDown.addItemListener(this);
  94.                 panel.add(upDown);
  95.                
  96.                 panel.add(east = new Button("East"));
  97.                 east.addActionListener(this);
  98.                
  99.                 panel.add(new Label());
  100.                 panel.add(south = new Button("South"));
  101.                 south.addActionListener(this);         
  102.                 panel.add(new Label());
  103.                
  104.                 return panel;  
  105.         }
  106.        
  107.         //Action Listener
  108.         public void actionPerformed(ActionEvent e){
  109.                 if (e.getSource() == north){
  110.                         drawCanvas.addPoint(SimpleDrawCanvas.NORTH);
  111.                 }
  112.                 else if (e.getSource() == east){
  113.                         drawCanvas.addPoint(SimpleDrawCanvas.EAST);
  114.                 }
  115.                 else if (e.getSource() == south){
  116.                         drawCanvas.addPoint(SimpleDrawCanvas.SOUTH);
  117.                 }
  118.                 else if (e.getSource() == west){
  119.                         drawCanvas.addPoint(SimpleDrawCanvas.WEST);
  120.                 }
  121.                 requestFocus();
  122.         }
  123.                
  124.         //ItemListener
  125.         public void itemStateChanged(ItemEvent e){
  126.                 if (e.getSource() instanceof Choice){
  127.                         String choice = (String)e.getItem();
  128.                         if (choice.equals("black"))
  129.                                 drawCanvas.setActColor(Color.black);
  130.                         else if (choice.equals("blue"))
  131.                                 drawCanvas.setActColor(Color.blue);
  132.                         else if (choice.equals("green"))
  133.                                 drawCanvas.setActColor(Color.green);
  134.                 }
  135.                 requestFocus();
  136.         }
  137.        
  138.         //keyListener
  139.         public void keyPressed(KeyEvent e){
  140.                 switch(e.getKeyCode()){
  141.                         case KeyEvent.VK_LEFT:
  142.                                 drawCanvas.addPoint(SimpleDrawCanvas.WEST);
  143.                         case KeyEvent.VK_RIGHT:
  144.                                 drawCanvas.addPoint(SimpleDrawCanvas.EAST);
  145.                         case KeyEvent.VK_UP:
  146.                                 drawCanvas.addPoint(SimpleDrawCanvas.NORTH);
  147.                         case KeyEvent.VK_DOWN:
  148.                                 drawCanvas.addPoint(SimpleDrawCanvas.SOUTH);
  149.                 }
  150.         }
  151.                
  152.         public void keyReleased(KeyEvent e){
  153.         }
  154.  
  155.         public void keyTyped(KeyEvent e){
  156.         }
  157.        
  158.        
  159.         //methoden des window listeners müssen implementiert werden.          
  160.         //Window Listener      
  161.         public void windowClosed(WindowEvent e){               
  162.         }
  163.        
  164.         public void windowClosing(WindowEvent e){
  165.                 System.exit(0);
  166.         }
  167.        
  168.         public void windowDeiconified(WindowEvent e){          
  169.         }
  170.        
  171.         public void windowIconified(WindowEvent e){            
  172.         }
  173.        
  174.         public void windowOpened(WindowEvent e){
  175.                 requestFocus();
  176.         }
  177.        
  178.         public void windowActivated(WindowEvent e){            
  179.         }
  180.        
  181.         public void windowDeactivated(WindowEvent e){          
  182.         }
  183.        
  184.         //main program
  185.         public static void main (String[] args){
  186.                 SimpleDraw simpleDraw = new SimpleDraw();
  187.                 simpleDraw.setVisible(true);
  188.         }
  189. }
  190.  
  191.  
  192.