![]() |
import java.awt.*;
/** Primer barvnega prehoda v liku (Gradient fill)
* Java2D in Java 1.2.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class DrawExample2 extends DrawExample1 {
private GradientPaint gradient =
new GradientPaint(0.0F, 0.0F, Color.red, 175.0F,
175.0F, Color.yellow, true);
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0, 0, getSize().width, getSize().height);
drawFilledCircle(g2d);
}
protected void drawFilledCircle(Graphics2D g2d) {
g2d.setPaint(gradient);
g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}
public static void main(String[] args) {
WindowUtil.openInJFrame(new DrawExample2(),380, 400);
}
}

import com.sun.java.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
/** An example of using TexturePaint to fill objects
* with tiled images. Uses the getBufferedImage
* method of ImageUtil to load an Image from a file
* and then turn that into a BufferedImage.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class TiledImages extends JPanel {
private String dir = System.getProperty("user.dir");
private String imageFile1 = dir + "/images/marty.jpg";
private TexturePaint imagePaint1;
private Rectangle2D.Double imageRect;
private String imageFile2 = dir + "/images/bluedrop.gif";
private TexturePaint imagePaint2;
private int[] xPoints = { 30, 700, 400 };
private int[] yPoints = { 30, 30, 600 };
private Polygon imageTriangle = new Polygon(xPoints, yPoints, 3);
public TiledImages() {
BufferedImage image = ImageUtil.getBufferedImage(imageFile1, this);
imageRect = new Rectangle2D.Double(235, 70, image.getWidth(), image.getHeight());
imagePaint1 = new TexturePaint(image, imageRect);
image = ImageUtil.getBufferedImage(imageFile2, this);
imagePaint2 = new TexturePaint(image, new Rectangle(0, 0, 32, 32));
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0, 0, getSize().width, getSize().height);
g2d.setPaint(imagePaint2);
g2d.fill(imageTriangle);
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(5));
g2d.draw(imageTriangle);
g2d.setPaint(imagePaint1);
g2d.fill(imageRect);
g2d.setPaint(Color.black);
g2d.draw(imageRect);
}
public static void main(String[] args) {
WindowUtil.openInJFrame(new TiledImages(),750, 650);
}
}
import java.awt.*;
import java.awt.image.*;
/** A class that simplifies a few common image
* operations, in particular creating a BufferedImage
* from an Image file, and using MediaTracker to
* wait until an image or several images are done
* loading.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ImageUtil {
/** Create an Image from a file, then turn that into a BufferedImage.*/
public static BufferedImage getBufferedImage(String imageFile, Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(c),
image.getHeight(c),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, c);
return(bufferedImage);
}
/** Take an Image associated with a file, and
* wait until it is done loading. Just a
* simple application of MediaTracker. If you
* are loading multiple images, don't use this
* consecutive times, instead use the version
* that takes an array of images.
*/
public static boolean waitForImage(Image image, Component c) {
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
if (tracker.isErrorAny())
return(false);
else
return(true);
}
/** Take some Images associated with files, and
* wait until they are done loading. Just a
* simple application of MediaTracker.
*/
public static boolean waitForImages(Image[] images, Component c) {
MediaTracker tracker = new MediaTracker(c);
for(int i=0; i<images.length; i++)
tracker.addImage(images[i], 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
if (tracker.isErrorAny())
return(false);
else
return(true);
}
}

import com.sun.java.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Transparencies extends JPanel {
private static double gap=10, width=60, offset=20,
deltaX=gap+width+offset;
private Rectangle2D.Double
blueSquare = new Rectangle2D.Double(gap+offset, gap+offset,
width, width),
redSquare = new Rectangle2D.Double(gap, gap, width, width);
private AlphaComposite makeComposite(float alpha) {
int type = AlphaComposite.SRC_OVER;
return(AlphaComposite.getInstance(type, alpha));
}
private void drawSquares(Graphics2D g2d,
float alpha) {
Composite originalComposite = g2d.getComposite();
g2d.setPaint(Color.blue);
g2d.fill(blueSquare);
g2d.setComposite(makeComposite(alpha));
g2d.setPaint(Color.red);
g2d.fill(redSquare);
g2d.setComposite(originalComposite);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0, 0, getSize().width, getSize().height);
for(int i=0; i<11; i++) {
drawSquares(g2d, i*0.1f);
g2d.translate(deltaX, 0);
}
}
public static void main(String[] args) {
String title = "Transparency test: alpha of " +
"the red square ranges from " +
"0.0 at the left to 1.0 at " +
"the right.";
WindowUtil.openInJFrame(new Transparencies(),
(int)(11*deltaX + 2*gap),
(int)(deltaX + 3*gap),
title,
Color.lightGray);
}
}

avtor: Filip
Božič
december,
1998