/* * * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ package java2d;
/** * Introduction to the J2Ddemo. * * @author Brian Lichtenwalter * @author Alexander Kouznetsov
*/
@SuppressWarnings("serial") publicclass Intro extends JPanel {
privatestaticfinal Color myBlack = new Color(20, 20, 20); privatestaticfinal Color myWhite = new Color(240, 240, 255); privatestaticfinal Color myRed = new Color(149, 43, 42); privatestaticfinal Color myBlue = new Color(94, 105, 176); privatestaticfinal Color myYellow = new Color(255, 255, 140); private ScenesTable scenesTable; privateboolean doTable; privatefinal Surface surface;
public Intro() {
EmptyBorder eb = new EmptyBorder(80, 110, 80, 110);
BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
setBorder(new CompoundBorder(eb, bb));
setLayout(new BorderLayout());
setBackground(GRAY);
setToolTipText("click for scene table");
add(surface = new Surface());
addMouseListener(new MouseAdapter() {
@Override publicvoid mouseClicked(MouseEvent e) {
removeAll(); if ((doTable = !doTable)) {
setToolTipText("click for animation");
surface.stop(); if (scenesTable == null) {
scenesTable = new ScenesTable(Intro.this);
}
add(scenesTable);
} else {
setToolTipText("click for scene table");
surface.start();
add(surface);
}
revalidate();
repaint();
}
});
}
publicvoid start() { if (!doTable) {
surface.start();
}
}
publicvoid stop() { if (!doTable) {
surface.stop();
}
}
publicstaticvoid main(String[] argv) { final Intro intro = new Intro();
WindowListener l = new WindowAdapter() {
@Override publicvoid windowIconified(WindowEvent e) {
intro.stop();
}
};
JFrame f = new JFrame("Java2D(TM) Demo - Intro");
f.addWindowListener(l);
f.getContentPane().add("Center", intro);
f.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int w = 720; int h = 510;
f.setLocation(screenSize.width / 2 - w / 2, screenSize.height / 2 - h
/ 2);
f.setSize(w, h);
f.setVisible(true);
intro.start();
}
/** * ScenesTable is the list of scenes known to the Director. * Scene participation, scene name and scene pause amount columns. * Global animation delay for scene's steps.
*/ staticclass ScenesTable extends JPanel implements ActionListener,
ChangeListener { privatefinal Intro intro; private JTable table; private TableModel dataModel;
@SuppressWarnings("LeakingThisInConstructor") public ScenesTable(final Intro intro) { this.intro = intro;
@Override public Object getValueAt(int row, int col) {
Surface.Scene scene = intro.surface.director.get(row); if (col == 0) { return scene.participate;
} elseif (col == 1) { return scene.name;
} else { return scene.pauseAmt;
}
}
@Override public String getColumnName(int col) { return names[col];
}
@Override publicClass<?> getColumnClass(int c) { return getValueAt(0, c).getClass();
}
@Override publicboolean isCellEditable(int row, int col) { return col != 1 ? true : false;
}
@Override publicvoid setValueAt(Object aValue, int row, int col) {
Surface.Scene scene = intro.surface.director.get(row); if (col == 0) {
scene.participate = aValue;
} elseif (col == 1) {
scene.name = aValue;
} else {
scene.pauseAmt = aValue;
}
}
};
table = new JTable(dataModel);
TableColumn col = table.getColumn("");
col.setWidth(16);
col.setMinWidth(16);
col.setMaxWidth(20);
col = table.getColumn("Pause");
col.setWidth(60);
col.setMinWidth(60);
col.setMaxWidth(60);
table.sizeColumnsToFit(0);
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
JPanel panel = new JPanel(new BorderLayout());
JButton b = new JButton("Unselect All");
b.setHorizontalAlignment(JButton.LEFT);
Font font = new Font(Font.SERIF, Font.PLAIN, 10);
b.setFont(font);
b.addActionListener(this);
panel.add("West", b);
while (thread == me) {
Scene scene = director.get(index); if (((Boolean) scene.participate).booleanValue()) {
repaint(); try { Thread.sleep(sleepAmt);
} catch (InterruptedException e) { break;
} if (scene.index > scene.length) {
scene.pause(); if (++index >= director.size()) {
reset();
}
}
} else { if (++index >= director.size()) {
reset();
}
}
} thread = null;
}
/** * Part is a piece of the scene. Classes must implement Part * in order to participate in a scene.
*/ interface Part {
publicvoid reset(int newwidth, int newheight);
publicvoid step(int w, int h);
publicvoid render(int w, int h, Graphics2D g2);
publicint getBegin();
publicint getEnd();
}
/** * Director is the holder of the scenes, their names & pause amounts * between scenes.
*/ staticclass Director extends ArrayList<Scene> {
GradientPaint gp = new GradientPaint(0, 40, myBlue, 38, 2, myBlack);
Font f1 = new Font(Font.SERIF, Font.PLAIN, 200);
Font f2 = new Font(Font.SERIF, Font.PLAIN, 120);
Font f3 = new Font(Font.SERIF, Font.PLAIN, 72);
public Director(Surface surf) {
Object[][][] partsInfo = {
{ { "J - scale text on gradient", "0" },
{ new GpE(GpE.BURI, myBlack, myBlue, 0, 20), new TxE("J", f1, TxE.SCI, myYellow, 2, 20) } },
{ { "2 - scale & rotate text on gradient", "0" },
{ new GpE(GpE.BURI, myBlue, myBlack, 0, 22), new TxE("2", f1, TxE.RI | TxE.SCI, myYellow, 2, 22) } },
{ { "D - scale text on gradient", "0" },
{ new GpE(GpE.BURI, myBlack, myBlue, 0, 20), new TxE("D", f1, TxE.SCI, myYellow, 2, 20) } },
{ { "J2D demo - scale & rotate text on gradient", "1000" },
{ new GpE(GpE.SIH, myBlue, myBlack, 0, 40), new TxE("J2D demo", f2, TxE.RI | TxE.SCI, myYellow, 0, 40) } },
{ { "Previous scene dither dissolve out", "0" },
{ new DdE(0, 20, 1, surf) } },
{ { "Graphics Features", "999" },
{ new Temp(Temp.RECT, null, 0, 15), new Temp(Temp.IMG, surf.duke, 2, 15), new Temp(Temp.RNA | Temp.INA, surf.duke, 16, 130), new Features(Features.GRAPHICS, 16, 130, surf) } },
{ { "J2D demo - texture text on gradient", "1000" },
{ new GpE(GpE.WI, myBlue, myBlack, 0, 20), new GpE(GpE.WD, myBlue, myBlack, 21, 40), new TpE(TpE.OI | TpE.NF, myBlack, myYellow, 4, 0, 10), new TpE(TpE.OD | TpE.NF, myBlack, myYellow, 4, 11, 20), new TpE(TpE.OI | TpE.NF | TpE.HAF, myBlack, myYellow, 5,
21, 40), new TxE("J2D demo", f2, 0, null, 0, 40) } },
{ { "Previous scene random close out", "0" },
{ new CoE(CoE.RAND, 0, 20, surf) } },
{ { "Text Features", "999" },
{ new Temp(Temp.RECT, null, 0, 15), new Temp(Temp.IMG, surf.duke, 2, 15), new Temp(Temp.RNA | Temp.INA, surf.duke, 16, 130), new Features(Features.TEXT, 16, 130, surf) } },
{ { "J2D demo - composite text on texture", "1000" },
{ new TpE(TpE.RI, myBlack, gp, 40, 0, 20), new TpE(TpE.RD, myBlack, gp, 40, 21, 40), new TpE(TpE.RI, myBlack, gp, 40, 41, 60), new TxE("J2D demo", f2, TxE.AC, myYellow, 0, 60) } },
{ { "Previous scene dither dissolve out", "0" },
{ new DdE(0, 20, 4, surf) } },
{ { "Imaging Features", "999" },
{ new Temp(Temp.RECT, null, 0, 15), new Temp(Temp.IMG, surf.duke, 2, 15), new Temp(Temp.RNA | Temp.INA, surf.duke, 16, 130), new Features(Features.IMAGES, 16, 130, surf) } },
{ { "J2D demo - text on gradient", "1000" },
{ new GpE(GpE.SDH, myBlue, myBlack, 0, 20), new GpE(GpE.SIH, myBlue, myBlack, 21, 40), new GpE(GpE.SDH, myBlue, myBlack, 41, 50), new GpE(GpE.INC | GpE.NF, myRed, myYellow, 0, 50), new TxE("J2D demo", f2, TxE.NOP, null, 0, 50) } },
{ { "Previous scene ellipse close out", "0" },
{ new CoE(CoE.OVAL, 0, 20, surf) } },
{ { "Color Features", "999" },
{ new Temp(Temp.RECT, null, 0, 15), new Temp(Temp.IMG, surf.duke, 2, 15), new Temp(Temp.RNA | Temp.INA, surf.duke, 16, 99), new Features(Features.COLOR, 16, 99, surf) } },
{ { "J2D demo - composite and rotate text on paints", "2000" },
{ new GpE(GpE.BURI, myBlack, myBlue, 0, 20), new GpE(GpE.BURD, myBlack, myBlue, 21, 30), new TpE(TpE.OI | TpE.HAF, myBlack, myBlue, 10, 31, 40), new TxE("J2D demo", f2, TxE.AC | TxE.RI, myYellow, 0, 40) } },
{ { "Previous scene subimage transform out", "0" },
{ new SiE(60, 60, 0, 40, surf) } },
{ { "CREDITS - transform in", "1000" },
{ new LnE(LnE.ACI | LnE.ZOOMI | LnE.RI, 0, 60), new TxE("CREDITS", f3, TxE.AC | TxE.SCI, RED, 20, 30), new TxE("CREDITS", f3, TxE.SCXD, RED, 31, 38), new TxE("CREDITS", f3, TxE.SCXI, RED, 39, 48), new TxE("CREDITS", f3, TxE.SCXD, RED, 49, 54), new TxE("CREDITS", f3, TxE.SCXI, RED, 55, 60) } },
{ { "CREDITS - transform out", "0" },
{ new LnE(LnE.ACD | LnE.ZOOMD | LnE.RD, 0, 45), new TxE("CREDITS", f3, 0, RED, 0, 9), new TxE("CREDITS", f3, TxE.SCD | TxE.RD, RED, 10, 30) } },
{ { "Contributors", "1000" },
{ new Temp(Temp.RECT, null, 0, 30), new Temp(Temp.IMG, surf.dukeanim, 4, 30), new Temp(Temp.RNA | Temp.INA, surf.dukeanim, 31, 200), new Contributors(34, 200, surf) } }, };
for (Object[][] partInfo : partsInfo) {
List<Part> parts = new ArrayList<Part>(); for (Object part : partInfo[1]) {
parts.add((Part) part);
}
add(new Scene(parts, partInfo[0][0], partInfo[0][1]));
}
}
}
/** * Scene is the manager of the parts.
*/ staticclass Scene extends Object {
public Object name; public Object participate = Boolean.TRUE; public Object pauseAmt; public List<Part> parts; publicint index; publicint length;
public Scene(List<Part> parts, Object name, Object pauseAmt) { this.name = name; this.parts = parts; this.pauseAmt = pauseAmt; for (Part part : parts) { int partLength = part.getEnd(); if (partLength > length) {
length = partLength;
}
}
}
publicvoid reset(int w, int h) {
index = 0; for (int i = 0; i < parts.size(); i++) {
(parts.get(i)).reset(w, h);
}
}
publicvoid step(int w, int h) { for (int i = 0; i < parts.size(); i++) {
Part part = parts.get(i); if (index >= part.getBegin() && index <= part.getEnd()) {
part.step(w, h);
}
}
}
publicvoid render(int w, int h, Graphics2D g2) { for (int i = 0; i < parts.size(); i++) {
Part part = parts.get(i); if (index >= part.getBegin() && index <= part.getEnd()) {
part.render(w, h, g2);
}
}
}
publicvoid pause() { try { Thread.sleep(Long.parseLong((String) pauseAmt));
} catch (Exception ignored) {
}
System.gc();
}
} // End Scene class
/** * Text Effect. Transformation of characters. Clip or fill.
*/ staticfinalclass TxE implements Part {
@Override publicvoid reset(int w, int h) { if (doRandom) { int num = (int) (Math.random() * 5.0); switch (num) { case 0:
type = OVAL; break; case 1:
type = RECT; break; case 2:
type = RECT | WID; break; case 3:
type = RECT | HEI; break; case 4:
type = ARC; break; default:
type = OVAL;
}
}
shape = null;
bimg = null;
extent = 360.0;
zoom = 2.0;
}
@Override publicvoid step(int w, int h) { if (bimg == null) { int biw = surf.bimg.getWidth(); int bih = surf.bimg.getHeight();
bimg = new BufferedImage(biw, bih,
BufferedImage.TYPE_INT_RGB);
Graphics2D big = bimg.createGraphics();
big.drawImage(surf.bimg, 0, 0, null);
} double z = Math.min(w, h) * zoom; if ((type & OVAL) != 0) {
shape = new Ellipse2D.Double(w / 2 - z / 2, h / 2 - z / 2, z,
z);
} elseif ((type & ARC) != 0) {
shape = new Arc2D.Double(-100, -100, w + 200, h + 200, 90,
extent, Arc2D.PIE);
extent -= eIncr;
} elseif ((type & RECT) != 0) { if ((type & WID) != 0) {
shape = new Rectangle2D.Double(w / 2 - z / 2, 0, z, h);
} elseif ((type & HEI) != 0) {
shape = new Rectangle2D.Double(0, h / 2 - z / 2, w, z);
} else {
shape = new Rectangle2D.Double(w / 2 - z / 2, h / 2 - z
/ 2, z, z);
}
}
zoom += zIncr;
}
@Override publicint getEnd() { return ending;
}
} // End CoE class
/** * Dither Dissolve Effect. For each successive step in the animation, * a pseudo-random starting horizontal position is chosen using list, * and then the corresponding points created from xlist and ylist are * blacked out for the current "chunk". The x and y chunk starting * positions are each incremented by the associated chunk size, and * this process is repeated for the number of "steps" in the * animation, causing an equal number of pseudo-randomly picked * "blocks" to be blacked out during each step of the animation.
*/ staticclass DdE implements Part { privatefinal Surface surf; privateint beginning, ending; private BufferedImage bimg; private Graphics2D big; private List<Integer> list, xlist, ylist; privateint xeNum, yeNum; // element number privateint xcSize, ycSize; // chunk size privateint inc; privateint blocksize;
public DdE(int beg, int end, int blocksize, Surface surf) { this.beginning = beg; this.ending = end; this.blocksize = blocksize; this.surf = surf;
}
privatevoid createShuffledLists() { int width = bimg.getWidth(); int height = bimg.getHeight();
xlist = new ArrayList<Integer>(width);
ylist = new ArrayList<Integer>(height);
list = new ArrayList<Integer>(ending - beginning + 1); for (int i = 0; i < width; i++) {
xlist.add(i, i);
} for (int i = 0; i < height; i++) {
ylist.add(i, i);
} for (int i = 0; i < (ending - beginning + 1); i++) {
list.add(i, i);
}
java.util.Collections.shuffle(xlist);
java.util.Collections.shuffle(ylist);
java.util.Collections.shuffle(list);
}
@Override publicvoid step(int w, int h) { if (bimg == null) { int biw = surf.bimg.getWidth(); int bih = surf.bimg.getHeight();
bimg = new BufferedImage(biw, bih,
BufferedImage.TYPE_INT_RGB);
Graphics2D big = bimg.createGraphics();
big.drawImage(surf.bimg, 0, 0, null); for (int x = 0; x < w && scale > 0.0; x += siw) { int ww = x + siw < w ? siw : w - x; for (int y = 0; y < h; y += sih) { int hh = y + sih < h ? sih : h - y;
subs.add(bimg.getSubimage(x, y, ww, hh));
pts.add(new Point(x, y));
}
}
}
rotate += rIncr;
scale -= sIncr;
}
@Override publicvoid render(int w, int h, Graphics2D g2) {
AffineTransform saveTx = g2.getTransform();
g2.setColor(myBlue); for (int i = 0; i < subs.size() && scale > 0.0; i++) {
BufferedImage bi = subs.get(i);
Point p = pts.get(i); int ww = bi.getWidth(); int hh = bi.getHeight();
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(rotate), p.x + ww / 2, p.y + hh / 2);
at.translate(p.x, p.y);
at.scale(scale, scale);
@Override publicint getEnd() { return ending;
}
} // End SiE class
/** * Line Effect. Flattened ellipse with lines from the center * to the edge. Expand or collapse the ellipse. Fade in or out * the lines.
*/ staticclass LnE implements Part {
staticfinalint INC = 1; staticfinalint DEC = 2; staticfinalint R = 4; // rotate staticfinalint ZOOM = 8; // zoom staticfinalint AC = 32; // AlphaComposite staticfinalint RI = R | INC; staticfinalint RD = R | DEC; staticfinalint ZOOMI = ZOOM | INC; staticfinalint ZOOMD = ZOOM | DEC; staticfinalint ACI = AC | INC; staticfinalint ACD = AC | DEC; privateint beginning, ending; privatedouble rIncr, rotate; privatedouble zIncr, zoom; private List<Point2D.Double> pts = new ArrayList<Point2D.Double>(); privatefloat alpha, aIncr; privateint type;
@Override publicint getEnd() { return ending;
}
} // End LnE class
/** * Template for Features & Contributors consisting of translating * blue and red rectangles and an image going from transparent to * opaque.
*/ staticclass Temp implements Part {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.