Java by API/java.awt/Toolkit
Содержание
- 1 Toolkit: addAWTEventListener(AWTEventListener listener, long eventMask)
- 2 Toolkit: beep()
- 3 Toolkit: createImage(byte[] img)
- 4 Toolkit: getColorModel()
- 5 Toolkit: getDefaultToolkit()
- 6 Toolkit: getImage(URL url)
- 7 Toolkit: getMenuShortcutKeyMask()
- 8 Toolkit: getProperty(String key, String def)
- 9 Toolkit: getScreenResolution()
- 10 Toolkit: getScreenSize()
- 11 Toolkit: getSystemClipboard()
- 12 Toolkit: getSystemEventQueue()
- 13 Toolkit: prepareImage(Image image, int width, int height, ImageObserver observer)
Toolkit: addAWTEventListener(AWTEventListener listener, long eventMask)
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ruponentEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main implements AWTEventListener {
public void eventDispatched(AWTEvent evt) {
if (evt.getID() == WindowEvent.WINDOW_OPENED) {
ComponentEvent cev = (ComponentEvent) evt;
if (cev.getComponent() instanceof JFrame) {
System.out.println("event: " + evt);
JFrame frame = (JFrame) cev.getComponent();
loadSettings(frame);
}
}
}
public static void loadSettings(JFrame frame) {
System.out.println("loading");
}
public static void saveSettings() {
System.out.println("saving");
}
public static void main(String[] args) throws Exception {
Toolkit tk = Toolkit.getDefaultToolkit();
final Main main = new Main();
tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK);
final JFrame frame = new JFrame("");
frame.setName("your frame");
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new AbstractAction("Quit") {
public void actionPerformed(ActionEvent evt) {
try {
main.saveSettings();
System.exit(0);
} catch (Exception ex) {
System.out.println(ex);
}
}
});
mb.add(menu);
frame.setJMenuBar(mb);
frame.pack();
frame.setVisible(true);
}
}
Toolkit: beep()
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
}
}
Toolkit: createImage(byte[] img)
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public final class MainClass {
private MainClass() {
}
public static Image getImage(Class relativeClass, String filename) {
Image returnValue = null;
InputStream is = relativeClass.getResourceAsStream(filename);
if (is != null) {
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int ch;
while ((ch = bis.read()) != -1) {
baos.write(ch);
}
returnValue = Toolkit.getDefaultToolkit().createImage(
baos.toByteArray());
} catch (IOException exception) {
System.err.println("Error loading: " + filename);
}
}
return returnValue;
}
}
Toolkit: getColorModel()
/*
* Output:
Color model = DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("Color model = " + tk.getColorModel());
}
}
Toolkit: getDefaultToolkit()
/*
* Output:
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
}
}
Toolkit: getImage(URL url)
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void load() throws MalformedURLException {
URL url = new URL("image address");
Image im = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mt = new MediaTracker(this);
mt.addImage(im, 0);
try {
mt.waitForID(0);
} catch (InterruptedException e) {
System.err.println("Unexpected interrupt in waitForID!");
return;
}
if (mt.isErrorID(0)) {
System.err.println("Couldn"t load image file " + url);
return;
}
}
public static void main(String[] args) {
}
}
Toolkit: getMenuShortcutKeyMask()
/*
* Output:
Menu shortcut key mask = 2
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("Menu shortcut key mask = "
+ tk.getMenuShortcutKeyMask());
}
}
Toolkit: getProperty(String key, String def)
/*
* Output:
AWT.control = Ctrl
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("AWT.control = " + tk.getProperty("AWT.control", ""));
}
}
Toolkit: getScreenResolution()
/*
* Output:
Screen resolution = 96
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("Screen resolution = " + tk.getScreenResolution());
}
}
Toolkit: getScreenSize()
/*
* Output:
Screen width = 1280
Screen height = 1024
*/
import java.awt.Dimension;
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
System.out.println("Screen width = " + d.width);
System.out.println("Screen height = " + d.height);
}
}
Toolkit: getSystemClipboard()
/*
* Output:
System clipboard = sun.awt.windows.WClipboard@1f6f0bf */
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("System clipboard = " + tk.getSystemClipboard());
}
}
Toolkit: getSystemEventQueue()
/*
* Output:
System event queue = java.awt.EventQueue@e5855a
*/
import java.awt.Toolkit;
public class MainClass {
public static void main(String[] args) {
Toolkit tk = Toolkit.getDefaultToolkit();
System.out.println("System event queue = " + tk.getSystemEventQueue());
}
}
Toolkit: prepareImage(Image image, int width, int height, ImageObserver observer)
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
public class MainClass {
public static void main(String[] args) {
ImageObserver myObserver = new ImageObserver() {
public boolean imageUpdate(Image image, int flags, int x, int y, int width, int height) {
if ((flags & HEIGHT) != 0)
System.out.println("Image height = " + height);
if ((flags & WIDTH) != 0)
System.out.println("Image width = " + width);
if ((flags & FRAMEBITS) != 0)
System.out.println("Another frame finished.");
if ((flags & SOMEBITS) != 0)
System.out.println("Image section :" + new Rectangle(x, y, width, height));
if ((flags & ALLBITS) != 0)
System.out.println("Image finished!");
if ((flags & ABORT) != 0)
System.out.println("Image load aborted...");
return true;
}
};
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.getImage(args[0]);
toolkit.prepareImage(img, -1, -1, myObserver);
}
}