示例#1
0
  /*
   * (non-Javadoc)
   *
   * @see br.arca.morcego.Component#paint(java.awt.Graphics)
   */
  public void paint(Graphics g) {
    // TODO: paint a fancy textbox with description of node

    Graphics2D graphic = (Graphics2D) g;

    TextArea textArea = new TextArea(text);

    textArea.setLocation(0, 0);
    textArea.setVisible(true);

    Font font = new Font(null, Font.PLAIN, 10);

    FontRenderContext frc = new FontRenderContext(null, false, false);

    TextLayout l = new TextLayout(text, font, frc);

    Rectangle2D textBounds = l.getBounds();

    int margin = Config.getInteger(Config.descriptionMargin);
    int border = 1;
    int distX = 5;
    int distY = 5;

    int width = (int) textBounds.getWidth() + 2 * margin + 2 * border;
    int height = (int) textBounds.getHeight() + 2 * margin + 2 * border;

    int cornerX = originX - width - distX;
    int cornerY = originY - height - distY;

    if (cornerX < 0) {
      cornerX = 0;
    }
    if (cornerY < 0) {
      cornerY = 0;
    }

    graphic.setColor(Config.getColor(Config.descriptionBorder));
    graphic.fillRect(
        cornerX - margin - border,
        cornerY - margin - border,
        (int) textBounds.getWidth() + 2 * margin + 2 * border,
        (int) textBounds.getHeight() + 2 * margin + 2 * border);

    graphic.setColor(Config.getColor(Config.descriptionBackground));

    graphic.fillRect(
        cornerX - margin,
        cornerY - margin,
        (int) textBounds.getWidth() + 2 * margin,
        (int) textBounds.getHeight() + 2 * margin);

    graphic.setColor(Config.getColor(Config.descriptionColor));
    textArea.paint(g);

    l.draw(graphic, cornerX, cornerY + (int) textBounds.getHeight());
  }
  void setupGUI() {
    taDictionary = new TextArea();
    taDictionary.setLocation(0, 0);
    taDictionary.setSize(106, 567);
    taDictionary.setBackground(new Color(-1));
    taDictionary.setText("");
    taDictionary.setRows(5);
    taDictionary.setColumns(1);
    getContentPane().add(taDictionary);

    tfSourc = new TextField();
    tfSourc.setLocation(224, 90);
    tfSourc.setSize(266, 25);
    tfSourc.setBackground(new Color(-1));
    tfSourc.setText("");
    tfSourc.setColumns(10);
    getContentPane().add(tfSourc);

    lblSourc = new Label();
    lblSourc.setLocation(106, 90);
    lblSourc.setSize(119, 25);
    lblSourc.setText("Source Word:");
    getContentPane().add(lblSourc);
    /*
    lblDestinatio = new Label();
    lblDestinatio.setLocation(106,97);
    lblDestinatio.setSize(119,25);
    lblDestinatio.setText("Destination Wor");
    getContentPane().add(lblDestinatio);
       */

    lblDestinatio = new Label();
    lblDestinatio.setLocation(106, 120);
    lblDestinatio.setSize(119, 25);
    lblDestinatio.setText("Destination Word:");
    getContentPane().add(lblDestinatio);

    lblWordSize = new Label();
    lblWordSize.setLocation(106, 27); // 106,120
    lblWordSize.setSize(119, 25);
    lblWordSize.setText("Word Size:");
    getContentPane().add(lblWordSize);

    tfWordSize = new TextField();
    tfWordSize.setLocation(224, 27); // 224,120
    tfWordSize.setSize(263, 25);
    tfWordSize.setBackground(new Color(-1));
    tfWordSize.setText("5");
    tfWordSize.setColumns(10);
    getContentPane().add(tfWordSize);

    tfSourc_6 = new TextField();
    tfSourc_6.setLocation(226, 120);
    tfSourc_6.setSize(263, 25);
    tfSourc_6.setBackground(new Color(-1));
    tfSourc_6.setText("");
    tfSourc_6.setColumns(10);
    getContentPane().add(tfSourc_6);

    lblFileNam = new Label();
    lblFileNam.setLocation(104, 0);
    lblFileNam.setSize(119, 25);
    lblFileNam.setText("FilePath:");
    getContentPane().add(lblFileNam);

    tfFilePat = new TextField();
    tfFilePat.setLocation(224, 0);
    tfFilePat.setSize(266, 25);
    tfFilePat.setBackground(new Color(-1));

    // OS Detection
    if (System.getProperty("os.name").startsWith("Windows")) {
      // includes: Windows 2000,  Windows 95, Windows 98, Windows NT, Windows Vista, Windows XP
      tfFilePat.setText("c:\\ics340\\words.txt");
      System.out.println("Detected Windows: " + System.getProperty("os.name"));
    } else {
      tfFilePat.setText("/Users/jasonedstrom/ics340/d1.txt");
      System.out.println("Detected Mac OS X: " + System.getProperty("os.name"));
    }

    tfFilePat.setColumns(10);
    getContentPane().add(tfFilePat);

    btLoadTextFiel = new JButton();
    btLoadTextFiel.setLocation(108, 50); // 108,27
    btLoadTextFiel.setSize(198, 32);
    btLoadTextFiel.setText("Load Words from Text Field");
    getContentPane().add(btLoadTextFiel);

    btLoadFil = new JButton();
    btLoadFil.setLocation(306, 50); // 306,27
    btLoadFil.setSize(183, 32);
    btLoadFil.setText("Load Words from File");
    getContentPane().add(btLoadFil);

    btFindPat = new JButton();
    btFindPat.setLocation(106, 160);
    btFindPat.setSize(384, 38);
    btFindPat.setText("Find Path");
    getContentPane().add(btFindPat);

    lblDictCoun = new JLabel();
    lblDictCoun.setLocation(108, 513);
    lblDictCoun.setSize(300, 27);
    lblDictCoun.setForeground(new Color(-65536));
    lblDictCoun.setText("Words in Dictionary = 0 words");
    getContentPane().add(lblDictCoun);

    lblIndexing1 = new JLabel();
    lblIndexing1.setLocation(107, 454);
    lblIndexing1.setSize(130, 27);
    lblIndexing1.setForeground(new Color(-16777216));
    lblIndexing1.setText("");
    getContentPane().add(lblIndexing1);

    lblFindPat = new JLabel();
    lblFindPat.setLocation(108, 540);
    lblFindPat.setSize(250, 27);
    lblFindPat.setForeground(new Color(-14646771));
    lblFindPat.setText("Time to find Path: 0 milliseconds");
    getContentPane().add(lblFindPat);

    lblCos = new JLabel();
    lblCos.setLocation(360, 540);
    lblCos.setSize(175, 27);
    lblCos.setForeground(new Color(-16777216));
    lblCos.setText("Cost of Path: 0.0");
    getContentPane().add(lblCos);

    lblProgres = new JLabel();
    lblProgres.setLocation(108, 484);
    lblProgres.setSize(371, 26);
    lblProgres.setForeground(new Color(-14646771));
    lblProgres.setText("Time to Build Graph: 0 milliseconds");
    getContentPane().add(lblProgres);

    testpanel = new JPanel();
    testpanel.setLocation(106, 200);
    testpanel.setSize(350, 200);
    testpanel.setForeground(new Color(-14646771));
    // testpanel.setText("Test Location");
    getContentPane().add(testpanel);

    btClear = new JButton();
    btClear.setLocation(355, 513);
    btClear.setSize(125, 25);
    btClear.setText("Clear Results");
    getContentPane().add(btClear);

    // add actionlisteners to buttons
    btFindPat.addActionListener(this);
    btLoadTextFiel.addActionListener(this);
    btLoadFil.addActionListener(this);
    btClear.addActionListener(this);

    setTitle("WordLadderGUI");
    setSize(500, 600);
    setVisible(true);
    setResizable(false);
  }
示例#3
0
  public MainFrame(
      final Socket socket, String user, ObjectInputStream reader, ObjectOutputStream writer) {
    super("Main");

    frame = this;
    this.socket = socket;
    owner = user;
    Reader = reader;
    Writer = writer;

    screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frameSize = new Dimension(300, 510);
    setSize(frameSize);
    setLocation(screenSize.width / 2 - frameSize.width, (screenSize.height - frameSize.height) / 2);
    setBackground(Color.decode("#ccfffa"));
    setResizable(false);
    setLayout(null);

    // add Label "Online User List:"
    Label label1 = new Label("Online User List:");
    label1.setSize(200, 20);
    label1.setLocation(20, 40);
    add(label1);
    // add user list
    userList = new List(10, false);
    userList.setSize(260, 95);
    userList.setLocation(20, 60);
    add(userList);
    // add label "chat content:"
    Label label2 = new Label("Chat Content:");
    label2.setSize(200, 20);
    label2.setLocation(20, 160);
    add(label2);
    // add chat content
    textArea = new TextArea("", 18, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
    textArea.setBackground(Color.white);
    textArea.setEditable(false);
    textArea.setSize(260, 220);
    textArea.setLocation(20, 180);
    add(textArea);
    // add label "Send Message:"
    Label label3 = new Label("Send Message:");
    label3.setSize(200, 20);
    label3.setLocation(20, 405);
    add(label3);
    // add input textfield
    final TextField inputArea = new TextField();
    inputArea.setSize(205, 25);
    inputArea.setLocation(20, 425);
    add(inputArea);

    // add send button
    Button send = new Button("Send");
    send.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent arg0) {
            String text = inputArea.getText();
            try {
              Writer.writeObject(new MSG("MSG", text));
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            textArea.setText(textArea.getText().concat("[" + owner + "]:" + text + "\n"));
            inputArea.setText("");
          }
        });
    send.setSize(50, 25);
    send.setLocation(230, 425);
    add(send);

    // add button "Start Game"
    final Button btnGame = new Button("Start Game");
    btnGame.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (!isGame) {
              setSize(460, 510);
              controlPanel = new ControlPanel(socket, Reader, Writer, frame, owner);
              controlPanel.setLocation(300, 60);
              add(controlPanel);
              isGame = true;
              try {
                Writer.writeObject(new MSG("REQLIST", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              btnGame.setLabel("End Game");
            } else {
              try {
                Writer.writeObject(new MSG("QUIT", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              setSize(300, 510);
              remove(controlPanel);
              isGame = false;
              btnGame.setLabel("Start Game");
            }
          }
        });
    btnGame.setSize(90, 25);
    btnGame.setLocation(65, 465);
    add(btnGame);

    // add button quit
    Button btnQuit = new Button("Exit");
    btnQuit.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (isGame) {
              if (!controlPanel.canQuit) return;
              try {
                Writer.writeObject(new MSG("QUIT", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              controlPanel.isHost = false;
              controlPanel.panelHost = null;
              controlPanel.panelHost = null;
              controlPanel.initPanelStart();
              controlPanel.panelStart.setSize(135, 420);
              controlPanel.panelStart.setLocation(0, 0);
              controlPanel.add(controlPanel.panelStart);
            }
            runFlag = false;
            if (socket != null) {
              try {
                Writer.writeObject(new MSG("QUT", ""));
                socket.close();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
              }
            }
            System.exit(0);
          }
        });
    btnQuit.setSize(55, 25);
    btnQuit.setLocation(175, 465);
    add(btnQuit);

    new ReceiveThread(Reader).start();
  }