Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
java programming - applet
NOTICE: The content of this document should contain java related topics.
This is the java-applet document
Table of contents
1
applet............................................................................................................................ 2
1.1
Composite.java.........................................................................................................2
1.2
Pear.java...................................................................................................................5
1.3
StrokeAndFill.java................................................................................................... 7
1.4
SwingShapeMover.java......................................................................................... 11
1.5
Transform.java....................................................................................................... 15
This is a legal notice, so it is important.
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
Note:
Hopefolly their are not to many errors in the code examples.
1. applet
Hint: This section contains java-applet examples
1.1. Composite.java
/*
* @(#)Composite.java
*/
import
import
import
import
import
import
1.2 98/07/31
java.lang.Integer;
java.awt.*;
java.awt.event.*;
java.awt.font.*;
java.awt.geom.*;
java.awt.image.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.*;
/*
* This applet renders an ellipse overlapping a rectangle with the
compositing rule and
* alpha value selected by the user.
*/
public class Composite extends JApplet implements ItemListener {
CompPanel comp;
JLabel alphaLabel, rulesLabel;
JComboBox alphas, rules;
String alpha = "1.0";
int rule = 0;
// Initializes the layout of the components.
public void init() {
GridBagLayout layOut = new GridBagLayout();
getContentPane().setLayout(layOut);
GridBagConstraints l = new GridBagConstraints();
l.weightx = 1.0;
l.fill = GridBagConstraints.BOTH;
l.gridwidth = GridBagConstraints.RELATIVE;
alphaLabel = new JLabel();
alphaLabel.setText("Alphas");
Font newFont = getFont().deriveFont(1);
alphaLabel.setFont(newFont);
alphaLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(alphaLabel, l);
getContentPane().add(alphaLabel);
GridBagConstraints c = new GridBagConstraints();
getContentPane().setLayout(layOut);
This is a legal notice, so it is important.
Page 2
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
l.gridwidth = GridBagConstraints.REMAINDER;
rulesLabel = new JLabel();
rulesLabel.setText("Rules");
newFont = getFont().deriveFont(1);
rulesLabel.setFont(newFont);
rulesLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(rulesLabel, l);
getContentPane().add(rulesLabel);
GridBagConstraints a = new GridBagConstraints();
a.gridwidth = GridBagConstraints.RELATIVE;
a.weightx = 1.0;
a.fill = GridBagConstraints.BOTH;
alphas = new JComboBox();
layOut.setConstraints(alphas, a);
alphas.addItem("1.0");
alphas.addItem("0.75");
alphas.addItem("0.50");
alphas.addItem("0.25");
alphas.addItem("0.0");
alphas.addItemListener(this);
getContentPane().add(alphas);
a.gridwidth = GridBagConstraints.REMAINDER;
rules = new JComboBox();
layOut.setConstraints(rules, a);
rules.addItem("SRC");
rules.addItem("DST_IN");
rules.addItem("DST_OUT");
rules.addItem("DST_OVER");
rules.addItem("SRC_IN");
rules.addItem("SRC_OVER");
rules.addItem("SRC_OUT");
rules.addItem("CLEAR");
rules.addItemListener(this);
getContentPane().add(rules);
GridBagConstraints fC = new GridBagConstraints();
fC.fill = GridBagConstraints.BOTH;
fC.weightx = 1.0;
fC.weighty = 1.0;
fC.gridwidth = GridBagConstraints.REMAINDER;
comp = new CompPanel();
layOut.setConstraints(comp, fC);
getContentPane().add(comp);
validate();
}
/*
* Detects a change in either of the Choice components. Resets the
variable corresponding
* to the Choice whose state is changed. Invokes changeRule in
CompPanel with the current
* alpha and composite rules.
*/
public void itemStateChanged(ItemEvent e){
if ( e.getStateChange() != ItemEvent.SELECTED ) {
This is a legal notice, so it is important.
Page 3
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
return;
}
Object choice = e.getSource();
if ( choice == alphas ) {
alpha = (String)alphas.getSelectedItem();
} else {
rule = rules.getSelectedIndex();
}
comp.changeRule(alpha, rule);
}
public static void main(String s[]) {
JFrame f = new JFrame("Composite");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new Composite();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(300,300));
f.show();
}
}
class CompPanel extends JPanel {
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
float alpha = 1.0f;
public CompPanel(){}
// Resets the alpha and composite rules with selected items.
public void changeRule(String a, int rule) {
alpha = Float.valueOf(a).floatValue();
ac = AlphaComposite.getInstance(getRule(rule), alpha);
repaint();
}
// Gets the requested compositing rule.
public int getRule(int rule){
int alphaComp = 0;
switch ( rule ) {
case 0: alphaComp = AlphaComposite.SRC; break;
case 1: alphaComp = AlphaComposite.DST_IN; break;
case 2: alphaComp = AlphaComposite.DST_OUT; break;
case 3: alphaComp = AlphaComposite.DST_OVER; break;
case 4: alphaComp = AlphaComposite.SRC_IN; break;
case 5: alphaComp = AlphaComposite.SRC_OVER; break;
case 6: alphaComp = AlphaComposite.SRC_OUT; break;
case 7: alphaComp = AlphaComposite.CLEAR; break;
}
return alphaComp;
}
public void paintComponent(Graphics g) {
super.paintComponent( g );
This is a legal notice, so it is important.
Page 4
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
int w = d.width;
int h = d.height;
// Creates the buffered image.
BufferedImage buffImg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
// Clears the previously drawn image.
g2.setColor(Color.white);
g2.fillRect(0, 0, d.width, d.height);
int rectx = w/4;
int recty = h/4;
// Draws the rectangle and ellipse into the buffered image.
gbi.setColor(new Color(0.0f, 0.0f, 1.0f, 1.0f));
gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
gbi.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
gbi.setComposite(ac);
gbi.fill(new
Ellipse2D.Double(rectx+rectx/2,recty+recty/2,150,100));
// Draws the buffered image.
g2.drawImage(buffImg, null, 0, 0);
}
}
1.2. Pear.java
/*
* @(#)Pear.java
*/
import
import
import
import
import
1.2 98/07/31
java.awt.*;
java.awt.event.*;
java.awt.font.*;
java.awt.geom.*;
javax.swing.*;
/*
* This applet renders a pear, using Constructive Area Geometry (CSG)
methods,
* add, intersect, and subtract.
*/
public class Pear extends JApplet {
Ellipse2D.Double circle, oval, leaf, stem;
Area circ, ov, leaf1, leaf2, st1, st2;
public void init() {
circle = new Ellipse2D.Double();
oval = new Ellipse2D.Double();
This is a legal notice, so it is important.
Page 5
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
leaf = new Ellipse2D.Double();
stem = new Ellipse2D.Double();
circ = new Area(circle);
ov = new Area(oval);
leaf1 = new Area(leaf);
leaf2 = new Area(leaf);
st1 = new Area(stem);
st2 = new Area(stem);
setBackground(Color.white);
}
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
int w = d.width;
int h = d.height;
double ew = w/2;
double eh = h/2;
g2.setColor(Color.green);
// Creates the first leaf by filling the intersection of two
Area objects created from an ellipse.
leaf.setFrame(ew-16, eh-29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf.setFrame(ew-14, eh-47, 30.0, 30.0);
leaf2 = new Area(leaf);
leaf1.intersect(leaf2);
g2.fill(leaf1);
// Creates the second leaf.
leaf.setFrame(ew+1, eh-29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf2.intersect(leaf1);
g2.fill(leaf2);
g2.setColor(Color.black);
// Creates the stem by filling the Area resulting from the
subtraction of two Area objects created from an ellipse.
stem.setFrame(ew, eh-42, 40.0, 40.0);
st1 = new Area(stem);
stem.setFrame(ew+3, eh-47, 50.0, 50.0);
st2 = new Area(stem);
st1.subtract(st2);
g2.fill(st1);
g2.setColor(Color.yellow);
// Creates the pear itself by filling the Area resulting from
the union of two Area objects created by two different ellipses.
circle.setFrame(ew-25, eh, 50.0, 50.0);
oval.setFrame(ew-19, eh-20, 40.0, 70.0);
circ = new Area(circle);
ov = new Area(oval);
circ.add(ov);
g2.fill(circ);
}
This is a legal notice, so it is important.
Page 6
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
public static void main(String s[]) {
JFrame f = new JFrame("Pear");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new Pear();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(150,200));
f.show();
}
}
1.3. StrokeAndFill.java
/*
* @(#)StrokeandFill.java
*/
import
import
import
import
import
import
import
import
import
import
import
1.2 98/07/31
java.lang.Integer;
java.awt.*;
java.awt.event.*;
java.awt.font.*;
java.awt.geom.*;
java.awt.image.*;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
java.awt.event.ItemListener;
java.awt.event.ItemEvent;
javax.swing.*;
/*
* This applet renders a Shape, selected by the user, using the Stroke
and Paint attributes and rendering method
* also selected by the user.
*/
public class StrokeAndFill extends JApplet implements ItemListener {
JLabel primLabel, lineLabel, paintLabel, strokeLabel;
ShapePanel display;
static JComboBox primitive, line, paint, stroke;
int index = 0;
public static boolean no2D = false;
public void init() {
GridBagLayout layOut = new GridBagLayout();
getContentPane().setLayout(layOut);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.fill = GridBagConstraints.BOTH;
primLabel = new JLabel();
primLabel.setText("Primitive");
Font newFont = getFont().deriveFont(1);
This is a legal notice, so it is important.
Page 7
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
primLabel.setFont(newFont);
primLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(primLabel, c);
getContentPane().add(primLabel);
lineLabel = new JLabel();
lineLabel.setText("Lines");
lineLabel.setFont(newFont);
lineLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(lineLabel, c);
getContentPane().add(lineLabel);
c.gridwidth = GridBagConstraints.RELATIVE;
paintLabel = new JLabel();
paintLabel.setText("Paints");
paintLabel.setFont(newFont);
paintLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(paintLabel, c);
getContentPane().add(paintLabel);
c.gridwidth = GridBagConstraints.REMAINDER;
strokeLabel = new JLabel();
strokeLabel.setText("Rendering");
strokeLabel.setFont(newFont);
strokeLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(strokeLabel, c);
getContentPane().add(strokeLabel);
GridBagConstraints ls = new GridBagConstraints();
ls.weightx = 1.0;
ls.fill = GridBagConstraints.BOTH;
primitive = new JComboBox( new Object []{
"rectangle",
"ellipse",
"text"});
primitive.addItemListener(this);
newFont = newFont.deriveFont(0, 14.0f);
primitive.setFont(newFont);
layOut.setConstraints(primitive, ls);
getContentPane().add(primitive);
line = new JComboBox( new Object []{
"thin",
"thick",
"dashed"});
line.addItemListener(this);
line.setFont(newFont);
layOut.setConstraints(line, ls);
getContentPane().add(line);
ls.gridwidth = GridBagConstraints.RELATIVE;
paint = new JComboBox( new Object[]{
"solid",
"gradient",
"polka"});
paint.addItemListener(this);
paint.setFont(newFont);
layOut.setConstraints(paint, ls);
getContentPane().add(paint);
This is a legal notice, so it is important.
Page 8
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
ls.gridwidth = GridBagConstraints.REMAINDER;
stroke = new JComboBox( new Object[]{
"Stroke",
"Fill",
"Stroke & Fill"});
stroke.addItemListener(this);
stroke.setFont(newFont);
layOut.setConstraints(stroke, ls);
getContentPane().add(stroke);
GridBagConstraints sC = new GridBagConstraints();
sC.fill = GridBagConstraints.BOTH;
sC.weightx = 1.0;
sC.weighty = 1.0;
sC.gridwidth = GridBagConstraints.REMAINDER;
display = new ShapePanel();
layOut.setConstraints(display, sC);
display.setBackground(Color.white);
getContentPane().add(display);
validate();
}
public void itemStateChanged(ItemEvent e){
display.renderShape();
}
public static void main( String[] argv ) {
if ( argv.length > 0 && argv[0].equals( "-no2d" ) ) {
StrokeAndFill.no2D = true;
}
JFrame frame = new JFrame( "StrokeAndFill" );
frame.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit( 0 );
}
});
JApplet applet = new StrokeAndFill();
frame.getContentPane().add( BorderLayout.CENTER, applet );
applet.init();
frame.setSize( 450, 250 );
frame.show();
}
}
class ShapePanel extends JPanel {
BasicStroke bstroke = new BasicStroke(3.0f);
int w, h;
Shape shapes[] = new Shape[3];
public ShapePanel(){
setBackground(Color.white);
shapes[0] = new Rectangle(0, 0, 100, 100);
This is a legal notice, so it is important.
Page 9
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
shapes[1] = new Ellipse2D.Double(0.0, 0.0, 100.0, 100.0);
TextLayout textTl = new TextLayout("Text", new Font("Helvetica",
1, 96), new FontRenderContext(null, false, false));
AffineTransform textAt = new AffineTransform();
textAt.translate(0, (float)textTl.getBounds().getHeight());
shapes[2] = textTl.getOutline(textAt);
}
// Invokes the paint method.
public void renderShape() {
repaint();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
if ( !StrokeAndFill.no2D ) {
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
w = d.width;
h = d.height;
int width, height;
// Prints the initial instructions.
String instruct = "Pick a primitive, line style, paint, and
rendering method.";
TextLayout thisTl = new TextLayout(instruct, new
Font("Helvetica", 0, 10), g2.getFontRenderContext());
Rectangle2D bounds = thisTl.getBounds();
width = (int)bounds.getWidth();
thisTl.draw(g2, w/2-width/2, 20);
Stroke oldStroke = g2.getStroke();
// Sets the Stroke.
switch ( StrokeAndFill.line.getSelectedIndex() ) {
case 0 : g2.setStroke(new BasicStroke(3.0f)); break;
case 1 : g2.setStroke(new BasicStroke(8.0f)); break;
case 2 : float dash[] = {10.0f};
g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
break;
}
Paint oldPaint = g2.getPaint();
// Sets the Paint.
switch ( StrokeAndFill.paint.getSelectedIndex() ) {
case 0 : g2.setPaint(Color.blue); break;
case 1 : g2.setPaint(new GradientPaint(0, 0,
Color.lightGray, w-250, h, Color.blue, false));
break;
case 2 : BufferedImage bi = new BufferedImage(5, 5,
BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.setColor(Color.blue);
big.fillRect(0, 0, 5, 5);
big.setColor(Color.lightGray);
This is a legal notice, so it is important.
Page 10
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
big.fillOval(0, 0, 5, 5);
Rectangle r = new Rectangle(0,0,5,5);
g2.setPaint(new TexturePaint(bi, r));
break;
}
// Sets the Shape.
Shape shape =
shapes[StrokeAndFill.primitive.getSelectedIndex()];
Rectangle r = shape.getBounds();
// Sets the selected Shape to the center of the Canvas.
AffineTransform saveXform = g2.getTransform();
AffineTransform toCenterAt = new AffineTransform();
toCenterAt.translate(w/2-(r.width/2), h/2-(r.height/2));
g2.transform(toCenterAt);
// Determines whether to fill, stroke, or fill and stroke.
switch ( StrokeAndFill.stroke.getSelectedIndex() ) {
case 0 : g2.draw(shape); break;
case 1 : g2.fill(shape); break;
case 2 : Graphics2D tempg2 = g2;
g2.fill(shape);
g2.setColor(Color.darkGray);
g2.draw(shape);
g2.setPaint(tempg2.getPaint()); break;
}
g2.setTransform(saveXform);
g2.setStroke(oldStroke);
g2.setPaint(oldPaint);
} else {
g.drawRect(0, 0, 100, 100);
}
}}
1.4. SwingShapeMover.java
/*
* @(#)SwingShapeMover.java
*/
import
import
import
import
1.2 98/07/31
java.awt.*;
java.awt.event.*;
java.awt.image.*;
javax.swing.*;
/*
* This applet allows the user to move a texture painted rectangle
around the applet
* window. The rectangle flickers and draws slowly because this applet
does not use
* double buffering.
*/
public class SwingShapeMover extends JApplet {
static protected JLabel label;
This is a legal notice, so it is important.
Page 11
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
DPanel d;
public void init(){
getContentPane().setLayout(new BorderLayout());
d = new DPanel();
d.setBackground(Color.white);
getContentPane().add(d);
label = new JLabel("Drag rectangle around within the area");
getContentPane().add("South", label);
}
public static void main(String s[]) {
JFrame f = new JFrame("SwingShapeMover");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new SwingShapeMover();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,250));
f.show();
}
}
class DPanel extends JPanel implements MouseListener,
MouseMotionListener{
Rectangle rect = new Rectangle(0, 0, 100, 50);
BufferedImage bi;
Graphics2D big;
// Holds the coordinates of the user's last mousePressed event.
int last_x, last_y;
boolean firstTime = true;
TexturePaint fillPolka, strokePolka;
Rectangle area;
// True if the user pressed, dragged or released the mouse
outside of
// the rectangle; false otherwise.
boolean pressOut = false;
public DPanel(){
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
// Creates the fill texture paint pattern.
bi = new BufferedImage(5, 5,
BufferedImage.TYPE_INT_RGB);
big = bi.createGraphics();
big.setColor(Color.pink);
big.fillRect(0, 0, 7, 7);
big.setColor(Color.cyan);
big.fillOval(0, 0, 3, 3);
This is a legal notice, so it is important.
Page 12
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
Rectangle r = new Rectangle(0,0,5,5);
fillPolka = new TexturePaint(bi, r);
big.dispose();
//Creates the stroke texture paint pattern.
bi = new BufferedImage(5, 5,
BufferedImage.TYPE_INT_RGB);
big = bi.createGraphics();
big.setColor(Color.cyan);
big.fillRect(0, 0, 7, 7);
big.setColor(Color.pink);
big.fillOval(0, 0, 3, 3);
r = new Rectangle(0,0,5,5);
strokePolka = new TexturePaint(bi, r);
big.dispose();
}
// Handles the event of the user pressing down the mouse button.
public void mousePressed(MouseEvent e){
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the
rectangle
// while the user is pressing the mouse.
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the
cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding
// down the mouse button.
public void mouseDragged(MouseEvent e){
if ( !pressOut ) {
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the cursor
on the rectangle and then drag.");
}
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(MouseEvent e){
// Checks whether or not the cursor is inside of the
rectangle
// when the user releases the mouse button.
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
SwingShapeMover.label.setText("First position the
cursor on the rectangle and then drag.");
pressOut = false;
This is a legal notice, so it is important.
Page 13
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
}
}
// This method is required by MouseListener.
public void mouseMoved(MouseEvent e){}
// These methods are required by MouseMotionListener.
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
// Updates the coordinates representing the location of the current
rectangle.
public void updateLocation(MouseEvent e){
rect.setLocation(last_x + e.getX(), last_y + e.getY());
/*
* Updates the label to reflect the location of the
* current rectangle
* if checkRect returns true; otherwise, returns error
message.
*/
if (checkRect()) {
SwingShapeMover.label.setText("Rectangle located at
" +
rect.getX() + ", "
+
rect.getY());
} else {
SwingShapeMover.label.setText("Please don't try to
"+
" drag outside the
area.");
}
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(8.0f));
Dimension dim = getSize();
int w = (int)dim.getWidth();
int h = (int)dim.getHeight();
if ( firstTime ) {
area = new Rectangle(dim);
rect.setLocation(w/2-50, h/2-25);
firstTime = false;
}
// Clears the rectangle that was previously drawn.
g2.setPaint(Color.white);
g2.fillRect(0, 0, w, h);
// Draws and fills the newly positioned rectangle.
g2.setPaint(fillPolka);
g2.fill(rect);
g2.setPaint(strokePolka);
This is a legal notice, so it is important.
Page 14
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
g2.draw(rect);
}
/*
Checks if the rectangle is contained within the applet window. If
the rectangle
is not contained withing the applet window, it is redrawn so that
it is adjacent
to the edge of the window and just inside the window.
*/
boolean checkRect(){
if (area == null) {
return false;
}
if(area.contains(rect.x, rect.y, 100, 50)){
return true;
}
int new_x = rect.x;
int new_y = rect.y;
if((rect.x+100)>area.getWidth()){
new_x = (int)area.getWidth()-99;
}
if(rect.x < 0){
new_x = -1;
}
if((rect.y+50)>area.getHeight()){
new_y = (int)area.getHeight()-49;
}
if(rect.y < 0){
new_y = -1;
}
rect.setLocation(new_x, new_y);
return false;
}
}
1.5. Transform.java
/*
* @(#)Transform.java
*/
import
import
import
import
import
import
import
import
import
import
1.2 98/07/31
java.lang.Integer;
java.awt.*;
java.awt.event.*;
java.awt.font.*;
java.awt.geom.*;
java.awt.image.*;
java.awt.event.ItemListener;
java.awt.event.ItemEvent;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
This is a legal notice, so it is important.
Page 15
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
import javax.swing.*;
/*
* This applet renders a shape, selected by the user, with a
paint,stroke, and rendering method,
* also selected by the user.
*/
public class Transform extends JApplet implements ItemListener,
ActionListener {
JLabel primLabel, lineLabel, paintLabel, transLabel, strokeLabel;
TransPanel display;
static JComboBox primitive, line, paint, trans, stroke;
JButton redraw;
public static boolean no2D = false;
public void init() {
GridBagLayout layOut = new GridBagLayout();
getContentPane().setLayout(layOut);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1.0;
c.fill = GridBagConstraints.BOTH;
primLabel = new JLabel();
primLabel.setText("Primitive");
Font newFont = getFont().deriveFont(1);
primLabel.setFont(newFont);
primLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(primLabel, c);
getContentPane().add(primLabel);
lineLabel = new JLabel();
lineLabel.setText("Lines");
lineLabel.setFont(newFont);
lineLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(lineLabel, c);
getContentPane().add(lineLabel);
paintLabel = new JLabel();
paintLabel.setText("Paints");
paintLabel.setFont(newFont);
paintLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(paintLabel, c);
getContentPane().add(paintLabel);
c.gridwidth = GridBagConstraints.RELATIVE;
transLabel = new JLabel();
transLabel.setText("Transforms");
transLabel.setFont(newFont);
transLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(transLabel, c);
getContentPane().add(transLabel);
c.gridwidth = GridBagConstraints.REMAINDER;
strokeLabel = new JLabel();
strokeLabel.setText("Rendering");
strokeLabel.setFont(newFont);
strokeLabel.setHorizontalAlignment(JLabel.CENTER);
layOut.setConstraints(strokeLabel, c);
This is a legal notice, so it is important.
Page 16
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
getContentPane().add(strokeLabel);
GridBagConstraints ls = new GridBagConstraints();
ls.weightx = 1.0;
ls.fill = GridBagConstraints.BOTH;
primitive = new JComboBox( new Object []{
"rectangle",
"ellipse",
"text"});
primitive.addItemListener(this);
newFont = newFont.deriveFont(0, 14.0f);
primitive.setFont(newFont);
layOut.setConstraints(primitive, ls);
getContentPane().add(primitive);
line = new JComboBox( new Object []{
"thin",
"thick",
"dashed"});
line.addItemListener(this);
line.setFont(newFont);
layOut.setConstraints(line, ls);
getContentPane().add(line);
paint = new JComboBox( new Object[]{
"solid",
"gradient",
"polka"});
paint.addItemListener(this);
paint.setFont(newFont);
layOut.setConstraints(paint, ls);
getContentPane().add(paint);
ls.gridwidth = GridBagConstraints.RELATIVE;
trans = new JComboBox( new Object[]{
"Identity",
"rotate",
"scale",
"shear"});
trans.addItemListener(this);
trans.setFont(newFont);
layOut.setConstraints(trans, ls);
getContentPane().add(trans);
ls.gridwidth = GridBagConstraints.REMAINDER;
stroke = new JComboBox( new Object[]{
"Stroke",
"Fill",
"Stroke & Fill"});
stroke.addItemListener(this);
stroke.setFont(newFont);
layOut.setConstraints(stroke, ls);
getContentPane().add(stroke);
GridBagConstraints button = new GridBagConstraints();
button.gridwidth = GridBagConstraints.REMAINDER;
redraw = new JButton("Redraw");
redraw.addActionListener(this);
This is a legal notice, so it is important.
Page 17
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
redraw.setFont(newFont);
layOut.setConstraints(redraw, button);
getContentPane().add(redraw);
GridBagConstraints tP = new GridBagConstraints();
tP.fill = GridBagConstraints.BOTH;
tP.weightx = 1.0;
tP.weighty = 1.0;
tP.gridwidth = GridBagConstraints.REMAINDER;
display = new TransPanel();
layOut.setConstraints(display, tP);
display.setBackground(Color.white);
getContentPane().add(display);
validate();
}
public void itemStateChanged(ItemEvent e){}
public void actionPerformed(ActionEvent e) {
display.setTrans(trans.getSelectedIndex());
display.renderShape();
}
public static void main( String[] argv ) {
if ( argv.length > 0 && argv[0].equals( "-no2d" ) ) {
Transform.no2D = true;
}
JFrame frame = new JFrame( "Transform" );
frame.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit( 0 );
}
});
JApplet applet = new Transform();
frame.getContentPane().add( BorderLayout.CENTER, applet );
applet.init();
frame.setSize( 550, 400 );
frame.show();
}
}
class TransPanel extends JPanel {
AffineTransform at = new AffineTransform();
int w, h;
Shape shapes[] = new Shape[3];
BufferedImage bi;
boolean firstTime = true;
public TransPanel(){
setBackground(Color.white);
shapes[0] = new Rectangle(0, 0, 100, 100);
This is a legal notice, so it is important.
Page 18
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
shapes[1] = new Ellipse2D.Double(0.0, 0.0, 100.0, 100.0);
TextLayout textTl = new TextLayout("Text", new Font("Helvetica",
1, 96), new FontRenderContext(null, false, false));
AffineTransform textAt = new AffineTransform();
textAt.translate(0, (float)textTl.getBounds().getHeight());
shapes[2] = textTl.getOutline(textAt);
}
public void setTrans(int transIndex) {
// Sets the AffineTransform.
switch ( transIndex ) {
case 0 : at.setToIdentity();
at.translate(w/2, h/2); break;
case 1 : at.rotate(Math.toRadians(45)); break;
case 2 : at.scale(0.5, 0.5); break;
case 3 : at.shear(0.5, 0.0); break;
}
}
public void renderShape() {
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if ( !Transform.no2D ) {
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
w = d.width;
h = d.height;
// Prints out the intructions.
String instruct = "Pick a primitive, line style, paint,
transform,";
TextLayout thisTl = new TextLayout(instruct, new
Font("Helvetica", 0, 10), g2.getFontRenderContext());
float width = (float)thisTl.getBounds().getWidth();
float height = (float)thisTl.getBounds().getHeight();
thisTl.draw(g2, w/2-width/2, 15);
instruct = "and rendering method and click the Redraw button.";
thisTl = new TextLayout(instruct, new Font("Helvetica", 0, 10),
g2.getFontRenderContext());
width = (float)thisTl.getBounds().getWidth();
thisTl.draw(g2, w/2-width/2, height + 17);
// Initialize the transform.
if (firstTime) {
at.setToIdentity();
at.translate(w/2, h/2);
firstTime = false;
}
// Sets the Stroke.
Stroke oldStroke = g2.getStroke();
switch ( Transform.line.getSelectedIndex() ) {
This is a legal notice, so it is important.
Page 19
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
case 0 : g2.setStroke(new BasicStroke(3.0f)); break;
case 1 : g2.setStroke(new BasicStroke(8.0f)); break;
case 2 : float dash[] = {10.0f};
g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
break;
}
// Sets the Paint.
Paint oldPaint = g2.getPaint();
switch ( Transform.paint.getSelectedIndex() ) {
case 0 : g2.setPaint(Color.blue);break;
case 1 : g2.setPaint(new GradientPaint(0, 0, Color.lightGray,
w-250, h, Color.blue, false));
break;
case 2 : BufferedImage buffi = new BufferedImage(15, 15,
BufferedImage.TYPE_INT_RGB);
Graphics2D buffig = buffi.createGraphics();
buffig.setColor(Color.blue);
buffig.fillRect(0, 0, 15, 15);
buffig.setColor(Color.lightGray);
buffig.translate((15/2)-(5/2), (15/2)-(5/2));
buffig.fillOval(0, 0, 7, 7);
Rectangle r = new Rectangle(0,0,25,25);
g2.setPaint(new TexturePaint(buffi, r));
break;
}
// Sets the Shape.
Shape shape = shapes[Transform.primitive.getSelectedIndex()];
Rectangle r = shape.getBounds();
// Sets the selected Shape to the center of the Canvas.
AffineTransform saveXform = g2.getTransform();
AffineTransform toCenterAt = new AffineTransform();
toCenterAt.concatenate(at);
toCenterAt.translate(-(r.width/2), -(r.height/2));
g2.transform(toCenterAt);
// Sets the rendering method.
switch ( Transform.stroke.getSelectedIndex() ) {
case 0 : g2.draw(shape); break;
case 1 : g2.fill(shape); break;
case 2 : Graphics2D tempg2 = g2;
g2.fill(shape);
g2.setColor(Color.darkGray);
g2.draw(shape);
g2.setPaint(tempg2.getPaint()); break;
}
g2.setStroke(oldStroke);
g2.setPaint(oldPaint);
g2.setTransform(saveXform);
}
This is a legal notice, so it is important.
Page 20
Copyright © 2002-2012 hvanbelle All rights reserved.
java programming - applet
}
}
This is a legal notice, so it is important.
This is a legal notice, so it is important.
Page 21
Copyright © 2002-2012 hvanbelle All rights reserved.