home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1995 November
/
PCWK1195.iso
/
inne
/
win95
/
sieciowe
/
hotja32.lzh
/
hotjava
/
classsrc
/
awt
/
demo
/
awttest.java
next >
Wrap
Text File
|
1995-08-11
|
6KB
|
260 lines
/*
* @(#)AWTTest.java 1.20 95/01/31 Sami Shaio
*
* Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package awt.demo;
import awt.*;
import java.io.*;
import java.lang.*;
import java.util.*;
class OpenItem extends MenuItem implements FileChooser {
public OpenItem(Menu m, Window w) {
super("Open...", m);
fileDialog = new FileDialog("Open File...", w);
}
public void fileChosen() {
System.out.println("Chose file: " + fileDialog.selectedFile());
}
public void selected() {
fileDialog.show(this);
}
private FileDialog fileDialog;
}
class TextItem {
public TextItem(Window p, String label, Font f, int x, int y) {
parent = p;
pLabel = label;
font = f;
px = x;
py = y;
height = f.height;
width = f.stringWidth(pLabel);
}
public void paint() {
parent.setFont(font);
parent.drawString(pLabel, px, py);
}
public void moveTo(int x, int y) {
parent.clearRect(px, py-height, width, height);
px = x;
py = y;
paint();
}
private Window parent;
private String pLabel;
private Font font;
private int px;
private int py;
private int height;
private int width;
}
class FileMenu extends Menu {
public FileMenu(MenuBar mbar, Window w) {
MenuItem item;
super("File", mbar);
new MenuItem("New", this);
new OpenItem(this, w);
item = new MenuItem("Save", this);
item.disable();
new MenuItem("Save As...", this);
new ToggleItem("Motion events", false, this);
new MenuItem("Quit", this);
contentWindow = w;
enabled = true;
}
public void selected(int index) {
int height;
int width;
if (index == 0) {
aw = new AWTTest();
}
if (index == 4) {
if (enabled) {
contentWindow.disablePointerMotionEvents();
enabled = false;
} else {
contentWindow.enablePointerMotionEvents();
enabled = true;
}
}
height = AWTTest.boldFont.height + 5;
width = AWTTest.boldFont.stringWidth("selected item ") +
AWTTest.normalFont.stringWidth("index=" + 10) + 5;
contentWindow.clearRect(50, 50, width, height);
contentWindow.drawRect(50, 50, width, height);
contentWindow.setFont(AWTTest.boldFont);
contentWindow.drawString("selected item ", 52, 50+height);
contentWindow.setFont(AWTTest.normalFont);
contentWindow.drawString("index=" + index,
52 +
AWTTest.boldFont.stringWidth("selected item "),
50 + height);
}
private AWTTest aw;
private Window contentWindow;
private boolean enabled;
};
class TestWindow extends Window {
TestWindow(Frame f) {
Color white = new Color(AWTTest.ws, 255, 255, 255);
Color red = new Color(AWTTest.ws, 255, 0, 0);
Color green = new Color(AWTTest.ws, 0, 255, 0);
Color blue = new Color(AWTTest.ws, 0, 0, 255);
Color gray = new Color(AWTTest.ws, 200, 200, 200);
super(f, false, white, 300, 300);
w1 = new Window(this, false, green, 300, 100);
w2 = new Window(this, false, red, 300, 100);
w21 = new Window(this, false, blue, 300, 100);
//w22 = new Window(w2, false, gray);
v21 = new Scrollbar(w2, Scrollbar.VERTICAL, true);
v22 = new Scrollbar(w21, Scrollbar.VERTICAL, false);
v22.moveTo(50, 50);
vsb = new Scrollbar(w1, Scrollbar.VERTICAL, true);
hsb = new Scrollbar(w1, Scrollbar.HORIZONTAL, true);
vsb.setScrollTarget(this);
hsb.setScrollTarget(this);
button = new Button("button 1", w1, 0, 0);
button1 = new Button("button 2", w1, 0, 25);
button2 = new Toggle("button 3", w1, 0, 50, false);
t = new TextItem(w1,
"I'm a text item",
AWTTest.boldFont,
200, 200);
textField = new TextField("initial value",
w1,
100,
100,
20);
layout();
}
public void paint() {
t.paint();
}
public void resize() {
layout();
vsb.setMaximum(w1.height - 20);
hsb.setMaximum(w1.width - 10);
v21.setMaximum(w2.height - 20);
//v22.setMaximum(w22.height - 20);
v22.moveTo(w21.width / 2, w21.height / 2);
}
void scrollButton() {
int h = hsb.value();
int v = vsb.value();
button.moveTo(h, v);
button1.moveTo(h, v+25);
button2.moveTo(h, v+50);
t.moveTo(h+100, v+100);
textField.moveTo(h+100,v+100);
}
boolean scrollVertically(int dy) {
return false;
}
boolean scrollHorizontally(int dx) {
return false;
}
public void lineUp() {
scrollButton();
}
public void lineDown() {
scrollButton();
}
public void pageUp() {
scrollButton();
}
public void pageDown() {
scrollButton();
}
public void dragAbsolute(int value) {
scrollButton();
}
Window w21;
Window w22;
Scrollbar v21;
Scrollbar v22;
Window w1;
Window w2;
TextItem t;
Scrollbar vsb;
Scrollbar hsb;
Button button;
Button button1;
Toggle button2;
TextField textField;
}
class AWTTest {
public AWTTest() {
f = new Frame(AWTTest.ws,
true, false, 300, 300,
new Color(AWTTest.ws,200, 200, 200));
f.setTitle("AWT demo " + nFrames++);
mbar = new MenuBar(f);
contentWindow = new TestWindow(f);
fileMenu = new FileMenu(mbar, contentWindow);
editMenu = new Menu("Edit", mbar);
new MenuItem("Cut", editMenu);
new MenuItem("Copy", editMenu);
new MenuItem("Paste", editMenu);
f.show();
}
public static void main(String args[]) {
AWTTest a1;
AWTTest.nFrames = 1;
ws = new WServer();
boldFont = new Font(AWTTest.ws, "helvetica-bold-14");
normalFont = new Font(AWTTest.ws, "lucidasans-typewriter-12");
a1 = new AWTTest();
ws.run();
}
public static Font boldFont;
public static Font normalFont;
public static int nFrames;
public static WServer ws;
Frame f;
MenuBar mbar;
FileMenu fileMenu;
Menu editMenu;
Window contentWindow;
};