This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Search

Thứ Năm, 13 tháng 2, 2014

Use the Event queue to retrieve event

import java.awt.AWTEvent;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EventQueuePanel extends JPanel implements ActionListener {
  EventQueuePanel() {
    JButton button = new JButton("Draw line");
    add(button);
    button.addActionListener(this);
  }

  public void actionPerformed(ActionEvent evt) {
    Graphics g = getGraphics();

    displayPrompt(g, "Click to chooose the first point");
    Point p = getClick();
    g.drawOval(p.x - 2, p.y - 244);
    displayPrompt(g, "Click to choose the second point");
    Point q = getClick();
    g.drawOval(q.x - 2, q.y - 244);
    g.drawLine(p.x, p.y, q.x, q.y);
    displayPrompt(g, "Done! Press button the start again.");
    g.dispose();
  }

  public void displayPrompt(Graphics g, String s) {
    y += 20;
    g.drawString(s, 0, y);
  }

  public Point getClick() {
    EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
    while (true) {
      try {
        AWTEvent evt = eq.getNextEvent();
        if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
          MouseEvent mevt = (MouseEventevt;
          Point p = mevt.getPoint();
          Point top = getRootPane().getLocation();
          p.x -= top.x;
          p.y -= top.y;
          return p;
        }
      catch (InterruptedException e) {
      }
    }
  }

  private int y = 60;
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("EventQueueTest");
    frame.setSize(300200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new EventQueuePanel());

    frame.show();
  }

}


Convert lines into the canonical MIME format, that is, terminate lines with CRLF

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Convert lines into the canonical MIME format, that is, terminate lines with
 * CRLF.
 * <p>
 
 * This stream can be used with the Part.writeTo and Message.writeTo methods to
 * generate the canonical MIME format of the data for the purpose of (e.g.)
 * sending it via SMTP or computing a digital signature.
 */
public class CRLFOutputStream extends FilterOutputStream {
  protected int lastb = -1;

  protected static byte[] newline;
  static {
    newline = new byte[2];
    newline[0(byte'\r';
    newline[1(byte'\n';
  }

  public CRLFOutputStream(OutputStream os) {
    super(os);
  }

  public void write(int bthrows IOException {
    if (b == '\r') {
      out.write(newline);
    else if (b == '\n') {
      if (lastb != '\r')
        out.write(newline);
    else {
      out.write(b);
    }
    lastb = b;
  }

  public void write(byte b[]) throws IOException {
    write(b, 0, b.length);
  }

  public void write(byte b[]int off, int lenthrows IOException {
    int start = off;

    len += off;
    for (int i = start; i < len; i++) {
      if (b[i== '\r') {
        out.write(b, start, i - start);
        out.write(newline);
        start = i + 1;
      else if (b[i== '\n') {
        if (lastb != '\r') {
          out.write(b, start, i - start);
          out.write(newline);
        }
        start = i + 1;
      }
      lastb = b[i];
    }
    if ((len - start0)
      out.write(b, start, len - start);
  }
}

JavaMail Authenticator

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class MainClass {

  public static void main(String[] argsthrows Exception {

    Properties props = new Properties();

    String host = "yourserver.edu";
    String provider = "pop3";

    Session session = Session.getDefaultInstance(props, new MailAuthenticator());
    Store store = session.getStore(provider);
    store.connect(host, null, null);

    Folder inbox = store.getFolder("INBOX");
    if (inbox == null) {
      System.out.println("No INBOX");
      System.exit(1);
    }
    inbox.open(Folder.READ_ONLY);

    Message[] messages = inbox.getMessages();
    for (int i = 0; i < messages.length; i++) {
      System.out.println("Message " (i + 1));
      messages[i].writeTo(System.out);
    }
    inbox.close(false);
    store.close();
  }
}

class MailAuthenticator extends Authenticator {

  public MailAuthenticator() {
  }

  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username""password");
  }
}

 
Le Van Chat