Professional Documents
Culture Documents
E element()
boolean offer(E o)
E peek()
E poll()
E remove()
java.util.Random;
java.util.concurrent.Delayed;
java.util.concurrent.DelayQueue;
java.util.concurrent.TimeUnit;
}
long last = 0;
for (int i=0; i < 10; i++) {
SecondsDelayed delay = (SecondsDelayed)(queue.take());
String name = delay.getName();
long tt = delay.getTriggerTime();
if (i != 0) {
System.out.println("Delta: " +
(tt - last) / (double)BILLION);
}
System.out.println(name + " / Trigger time: " + tt);
last = tt;
}
}
}
The DelayTest program places ten elements in the DelayQueue
before processing them.
Here is the output for one run of DelayQueue:
Delaying: 8 for loop 0
Delaying: 7 for loop 1
Delaying: 2 for loop 2
Delaying: 4 for loop 3
Delaying: 0 for loop 4
Delaying: 9 for loop 5
Delaying: 3 for loop 6
Delaying: 4 for loop 7
Delaying: 6 for loop 8
Delaying: 2 for loop 9
loop 4 / Trigger time:
Delta: 1.9995545
loop 2 / Trigger time:
Delta: 0.0012475
loop 9 / Trigger time:
Delta: 0.9995177
loop 6 / Trigger time:
Delta: 0.9995187
loop 3 / Trigger time:
Delta: 6.408E-4
loop 7 / Trigger time:
Delta: 2.0001667
loop 8 / Trigger time:
Delta: 0.9986953
loop 1 / Trigger time:
Delta: 0.9995595
loop 0 / Trigger time:
Delta: 1.001262
loop 5 / Trigger time:
1883173869520000
1883175869074500
1883175870322000
1883176869839700
1883177869358400
1883177869999200
1883179870165900
1883180868861200
1883181868420700
1883182869682700
The output indicates that the item for loop 4 is set to start at
time 0, that is, without delay:
Delaying: 0 for loop 4
So it runs first:
loop 4 / Trigger time: 1883173869520000
java.awt.BorderLayout;
java.awt.EventQueue;
java.io.InputStream;
java.text.ParseException;
javax.swing.JButton;
javax.swing.JLabel;
javax.swing.JFrame;
static javax.swing.JFrame.*;
javax.swing.UIManager;
javax.swing.plaf.synth.SynthLookAndFeel;
}
synth.load(is, aClass);
} catch (ParseException e) {
System.err.println("Bad configuration file");
System.exit(-2);
}
try {
UIManager.setLookAndFeel(synth);
} catch
(javax.swing.UnsupportedLookAndFeelException e) {
System.err.println("Cannot change to Synth");
System.exit(-3);
}
JFrame frame = new JFrame("First");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello, Synth");
label.setHorizontalAlignment(JLabel.CENTER);
frame.add(label);
frame.setSize(300, 100);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
The HelloSynth program creates a new SynthLookAndFeel object
and loads an XML file, synth.xml, with
synth.load(InputStream, Class). This means that you need the XML
file to complete this first example:
<synth>
<style id="label">
<font name="Monospaced" size="18" style="BOLD ITALIC"/>
<opaque value="true"/>
<state>
<color value="PINK" type="FOREGROUND"/>
<color value="YELLOW" type="BACKGROUND"/>
</state>
</style>
<bind style="label" type="region" key="Label"/>
</synth>
Run the HelloSynth program with the synth.xml file. The program
reads in the XML file, creates a JLabel, and adds it to the
screen. Notice the absence of any color or font configuring of
the label in the program. Instead, that's done through the XML
file. The synth.xml file configures the label as follows:
18-point bold-italic font, opaque, foreground color of pink
(text color), and background color of yellow. The HelloSynth
program then draws the label according to those specifications.
java HelloSynth synth.xml
If you don't like that color-font combination, you can change
the information in the XML file to produce something more
appealing. For example, you might change the font to bold, the
text color to red, and the background to black:
<synth>
<style id="label">
<opaque value="true"/>
<font name="Monospaced" size="18" style="BOLD"/>
<state>
<color value="RED" type="FOREGROUND"/>
<color value="BLACK" type="BACKGROUND"/>
</state>
</style>
<bind style="label" type="region" key="Label"/>
</synth>
By simply changing the XML file, and without
recompiling your program, you change the look produced by the
program.
java HelloSynth synth.xml
And that is the power of the Synth look and feel. It allows you
to change the look produced by the program without reprogramming.
The format of the XML file used with Synth is described in the
Synth File Format document
(http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/synth/doc-files/synthF
ileFormat.html).
Inside the outermost synth tag, you can have tags such as
<style>, <bind>, <font>, <color>, <imagePainter>, and <imageIcon>.
There are also special properties that you can set, these are
listed in the Component Specific Properties document
(http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/synth/doc-files/compon
entProperties.html).
Essentially, you define styles and bind them to components, as
shown in the previous examples.
The <style> element contains the most information. You can
configure numerous elements for each named style:
o
o
o
o
o
o
o
o
o
o
<property>
<defaultsProperty>
<state>
<font>
<painter>
<imagePainter>
<backgroundImage>
<opaque>
<imageIcon>
<inputMap>
The <font> tag allows you to specify the font's name, size, and
style. Supported styles are PLAIN, BOLD, and ITALIC. To get
both BOLD and ITALIC, provide both in a quoted string, with
a space between the two:
<font style="BOLD ITALIC"/>
The <state> tag can be more complex than shown in the examples.
For instance, if you describe a button, and you want a different
look when the mouse is over the button, you could configure a
MOUSE_OVER state, as follows:
<state value="MOUSE_OVER">
ENABLED
PRESSED
DISABLED
FOCUSED
SELECTED
DEFAULT