\Computer Science 15-100, Fall 2008
Class Notes: Graphics, Color, and Polygon Methods
Graphics, Color, and Polygon Methods
import java.awt.*;
import javax.swing.*;
class MyGraphics extends JComponent {
public void paint(Graphics page) {
// Place your paint code here!
}
////////////////////////////////////////////// /// END OF YOUR CODE /// /// (you may ignore all the code below!!! /// //////////////////////////////////////////////
public Dimension getPreferredSize() {
int initialWidth = 500;
int initialHeight = 400;
return new Dimension(initialWidth, initialHeight);
}
public static void main(String[] args) {
JComponent jc = newInstance();
JFrame frame = new JFrame(jc.getClass().getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.add(jc);
frame.setContentPane(cp);
frame.pack();
frame.setVisible(true);
}
// Returns an instance of this class as a JComponent. This is necessary so
// students can rename this class without changing the "main" method's body.
public static JComponent newInstance() {
StackTraceElement[] trace = null;
try { throw new RuntimeException(); }
catch (Exception e) { trace = e.getStackTrace(); }
try { return (JComponent)Class.forName(trace[0].getClassName()).newInstance(); }
catch (Exception e) { return null; }
}
} public void paint(Graphics page) {
page.setColor(Color.gray);
page.fillRect(50, 100, 150, 200);
page.setColor(Color.black);
page.drawLine(50, 100, 150, 200); // suprised?
}
public void paint(Graphics page) {
for (int i=0; i<10; i++) {
setLineThickness(page, i);
int x = 100 + 20*i;
page.drawLine(x, 50, x, 150);
}
}
//////////////////////////////////////
/// setLineThickness Helper method
//////////////////////////////////////
public static void setLineThickness(Graphics page, int thickness) {
if (thickness < 0) thickness = 0;
((Graphics2D)page).setStroke(new BasicStroke(thickness));
}
Another Example:
public void paint(Graphics page) {
// See how the same line is drawn differently
// with different line thicknesses
page.setColor(Color.blue);
setLineThickness(page, 20);
page.drawLine(50, 50, 50, 250);
page.setColor(Color.yellow);
setLineThickness(page, 5);
page.drawLine(50, 50, 50, 250);
page.setColor(Color.black);
setLineThickness(page, 1);
page.drawLine(50, 50, 50, 250);
}
public void paint(Graphics page) {
page.fillRect(50, 50, 100, 100);
}
public void paint(Graphics page) {
page.drawRect(50, 50, 100, 100);
page.setColor(Color.blue);
setLineThickness(page, 5);
page.drawRect(100, 100, 100, 100);
}
public void paint(Graphics page) {
page.fillRect(50, 50, 50, 150);
page.setColor(Color.green);
page.fillOval(50, 50, 50, 150);
}
public void paint(Graphics page) {
page.fillRect(50, 50, 50, 150);
page.setColor(Color.green);
page.drawOval(50, 50, 50, 150);
}
public void paint(Graphics page) {
int left = 50, top = 50, width = 50, height = 150;
page.fillRect(left, top, width, height);
int arcWidth = 30, arcHeight = 30;
page.setColor(Color.yellow);
page.fillRoundRect(left, top, width, height, arcWidth, arcHeight);
left += 100;
page.setColor(Color.blue);
page.fillRoundRect(left, top, width, height, arcWidth, arcHeight);
}
public void paint(Graphics page) {
int left = 50, top = 50, width = 50, height = 150;
page.fillRect(left, top, width, height);
int arcWidth = 30, arcHeight = 30;
page.setColor(Color.yellow);
page.drawRoundRect(left, top, width, height, arcWidth, arcHeight);
left += 100;
page.setColor(Color.blue);
page.drawRoundRect(left, top, width, height, arcWidth, arcHeight);
}
public void paint(Graphics page) {
int left = 50, top = 100, width = 100, height = 150;
page.fillRect(left, top, width, height);
int startAngle = 45; // in degrees, where 45 always is the top-right corner!
int extentAngle = 90;
page.setColor(Color.yellow);
page.fillArc(left, top, width, height, startAngle, extentAngle);
startAngle = 180;
extentAngle = 135;
page.setColor(Color.red);
page.fillArc(left, top, width, height, startAngle, extentAngle);
}
public void paint(Graphics page) {
int left = 50, top = 100, width = 100, height = 150;
page.fillRect(left, top, width, height);
int startAngle = 45; // in degrees, where 45 always is the top-right corner!
int extentAngle = 90;
page.setColor(Color.yellow);
page.drawArc(left, top, width, height, startAngle, extentAngle);
startAngle = 180;
extentAngle = 135;
page.setColor(Color.red);
page.drawArc(left, top, width, height, startAngle, extentAngle);
}
public void paint(Graphics page) {
page.fillRect(100, 50, 200, 150);
Polygon p = new Polygon();
p.addPoint(100, 50);
p.addPoint(200, 125);
p.addPoint(300, 50);
page.setColor(Color.green);
page.fillPolygon(p);
}
public void paint(Graphics page) {
page.fillRect(100, 50, 200, 150);
Polygon p = new Polygon();
p.addPoint(100, 50);
p.addPoint(200, 125);
p.addPoint(300, 50);
page.setColor(Color.green);
page.drawPolygon(p);
}
public void paint(Graphics page) {
page.drawString("Carpe Diem", 50, 50);
page.setColor(Color.red);
page.drawLine(50, 50, 100, 50); // surprised?
}
public void paint(Graphics page) {
page.setFont(new Font("Monospaced", Font.PLAIN, 16));
page.drawString("Carpe Diem", 50, 50);
page.setColor(Color.red);
page.setFont(new Font("Serif", Font.BOLD, 32));
page.drawString("Carpe Diem", 50, 100);
page.setColor(Color.blue);
page.setFont(new Font("SansSerif", Font.BOLD|Font.ITALIC, 64));
page.drawString("Carpe Diem", 50, 175);
}
public void paint(Graphics page) {
page.setColor(Color.blue);
page.fillRect(50, 50, 400, 100);
page.setFont(new Font("SansSerif", Font.BOLD, 64));
page.setColor(Color.yellow);
drawCenteredString(page, "Carpe Diem", 50, 50, 400, 100);
}
////////////////////////////////////// /// drawCenteredString Helper methods //////////////////////////////////////
public static int getStringWidth(Graphics page, Font f, String s) {
// Find the size of string s in the font of the Graphics context "page"
FontMetrics fm = page.getFontMetrics(f);
java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, page);
return (int)Math.round(rect.getWidth());
}
public static int getStringHeight(Graphics page, Font f, String s) {
// Find the size of string s in the font of the Graphics context "page"
FontMetrics fm = page.getFontMetrics(f);
java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, page);
return (int)Math.round(rect.getHeight());
}
public static int getStringAscent(Graphics page, Font f, String s) {
// Find the size of string s in the font of the Graphics context "page"
FontMetrics fm = page.getFontMetrics(f);
return fm.getAscent();
}
public static void drawCenteredString(Graphics page, String s, int left, int top, int width, int height) {
Font font = page.getFont();
int textWidth = getStringWidth(page,font,s);
int textHeight = getStringHeight(page,font,s);
int textAscent = getStringAscent(page,font,s);
// Center text horizontally and vertically within provided rectangular bounds
int textX = left + (width - textWidth)/2;
int textY = top + (height - textHeight)/2 + textAscent;
page.drawString(s, textX, textY);
}
public void paint(Graphics page) {
// To run this, first save the image sampleImage.jpg to the current directory
// get the dimensions of the draw area
int width = this.getWidth();
int height = this.getHeight();
// paint the background a pleasing olive green to complement our image
page.setColor(new Color(120,120,50));
page.fillRect(0,0,width,height);
// center the full-sized, non-rotated image in the window
double scale = 1.0;
double rotation = 0.0;
drawCenteredImage(page, "sampleImage.jpg", width/2, height/2, scale, rotation);
// now draw a version of the image with its left-top at (0,0) and scaled to be smaller
scale = 1.0/5.0;
drawImage(page, "sampleImage.jpg", 0, 0, scale, rotation);
// and draw a 1/5th version in the top-right corner rotated by 45 degrees
// This requires that we obtain the size of the rotated image so we
// can compute its resulting left-top
rotation = Math.toRadians(45);
Dimension newSize = getImageSize("sampleImage.jpg", scale, rotation);
int newLeft = width - (int)newSize.getWidth();
int newTop = 0;
drawImage(page, "sampleImage.jpg", newLeft, newTop, scale, rotation);
}
//////////////////////////////////////
/// drawImage and drawCenteredImage and Helper methods
/////////////////////////////////////
// Paint the image in the given file rotated and scaled
public void drawImage(Graphics page, String filename, int left, int top, double scale, double radians) {
Image image = getImageFromFile(filename);
drawImage(page, image, left, top, scale, radians);
}
// Same as drawImage, only here we position the image by its center
public void drawCenteredImage(Graphics page, String filename, int cx, int cy, double scale, double radians) {
Image image = getImageFromFile(filename);
drawCenteredImage(page, image, cx, cy, scale, radians);
}
// Paint the given image rotated and scaled with a top-left at x,y
public void drawImage(Graphics page, Image image, int x, int y, double scale, double radians) {
double imageWidth = image.getWidth(null);
double imageHeight = image.getHeight(null);
Dimension newSize = getImageSize(image, scale, radians);
java.awt.geom.AffineTransform transform = new java.awt.geom.AffineTransform();
transform.translate(x+newSize.getWidth()/2,y+newSize.getHeight()/2); // last (not first!)
transform.rotate(radians);
transform.scale(scale,scale);
transform.translate(-imageWidth/2, -imageHeight/2); // first
((Graphics2D)page).drawImage(image,transform,null);
}
// Same as drawImage, only here we position the image by its center
public void drawCenteredImage(Graphics page, Image image, int cx, int cy, double scale, double radians) {
Dimension newSize = getImageSize(image, scale, radians);
drawImage(page, image, cx - (int)(newSize.getWidth()/2), cy - (int)(newSize.getHeight()/2), scale, radians);
}
// get the size of this image after it has been scaled and rotated
public Dimension getImageSize(String filename, double scale, double radians) {
Image image = getImageFromFile(filename);
return getImageSize(image, scale, radians);
}
// get the size of this image after it has been scaled and rotated
public Dimension getImageSize(Image image, double scale, double radians) {
double imageWidth = image.getWidth(null);
double imageHeight = image.getHeight(null);
java.awt.geom.AffineTransform transform = new java.awt.geom.AffineTransform();
transform.rotate(radians);
transform.scale(scale,scale);
transform.translate(-imageWidth/2, -imageHeight/2); // first
double[] x = { 0, imageWidth, imageWidth , 0 };
double[] y = { 0, 0 , imageHeight, imageHeight };
double minx=0, maxx=0, miny=0, maxy=0;
java.awt.geom.Point2D.Double src = new java.awt.geom.Point2D.Double();
java.awt.geom.Point2D.Double dst = new java.awt.geom.Point2D.Double();
for (int i=0; i<4; i++) {
src.setLocation(x[i],y[i]);
transform.transform(src,dst);
if (i == 0) {
minx = maxx = dst.getX();
miny = maxy = dst.getY();
}
else {
minx = Math.min(dst.getX(),minx);
miny = Math.min(dst.getY(),miny);
maxx = Math.max(dst.getX(),maxx);
maxy = Math.max(dst.getY(),maxy);
}
}
return new Dimension((int)(maxx-minx), (int)(maxy-miny));
}
// Map from filenames to stored images, so we do not have to
// reload the image every time we draw it
public static java.util.HashMap<String,Image> fileToImageMap =
new java.util.HashMap<String,Image>();
// Convert a jpg or png file into an awt Image (this is a quick way;
// we really should use an ImageObserver or some other more reliable way).
public static Image getImageFromFile(String filename) {
if (!(new java.io.File(filename)).exists())
throw new RuntimeException("File does not exist: " + filename);
Image image = fileToImageMap.get(filename);
if (image == null) {
image = new ImageIcon(filename).getImage();
fileToImageMap.put(filename, image);
}
return image;
}
public void paint(Graphics page) {
// For a list of colors, see: http://en.wikipedia.org/wiki/List_of_colors
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color cinnabar = new Color(227, 66, 52);
page.setColor(cinnabar);
page.fillRect(30, 30, 50, 50);
}
public void paint(Graphics page) {
// For a list of colors, see: http://en.wikipedia.org/wiki/List_of_colors
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color semiTransparentCinnabar = new Color(227, 66, 52, 128); page.setColor(semiTransparentCinnabar); page.fillRect(30, 30, 50, 50); }
public void paint(Graphics page) {
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color anotherAmethyst = new Color(153, 102, 204);
page.setColor(anotherAmethyst);
page.fillRect(30, 30, 50, 50);
System.out.println(amethyst == anotherAmethyst); // surprised?
System.out.println(amethyst.equals(anotherAmethyst));
}
public void paint(Graphics page) {
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color brighterAmethyst = amethyst.brighter();
page.setColor(brighterAmethyst);
page.fillRect(30, 30, 50, 50);
}
public void paint(Graphics page) {
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color darkerAmethyst = amethyst.darker();
page.setColor(darkerAmethyst);
page.fillRect(30, 30, 50, 50);
}
public void paint(Graphics page) {
Color amethyst = new Color(153, 102, 204);
page.setColor(amethyst);
page.fillRect(10, 10, 50, 100);
Color semiTransparentCinnabar = new Color(227, 66, 52, 128);
page.setColor(semiTransparentCinnabar);
page.fillRect(30, 30, 50, 50);
System.out.println("RGBA values for semiTransparentCinnabar:");
System.out.println(" red = " + semiTransparentCinnabar.getRed());
System.out.println(" green = " + semiTransparentCinnabar.getGreen());
System.out.println(" blue = " + semiTransparentCinnabar.getBlue());
System.out.println(" alpha = " + semiTransparentCinnabar.getAlpha());
}
public void paint(Graphics page) {
page.fillRect(100, 50, 200, 150);
Polygon p = new Polygon();
p.addPoint(100, 50);
p.addPoint(200, 125);
p.addPoint(300, 50);
page.setColor(Color.black);
page.fillPolygon(p);
Rectangle bounds = p.getBounds();
System.out.println("Polygon's bounds: ");
System.out.println(" left = " + (int)bounds.getX());
System.out.println(" top = " + (int)bounds.getY());
System.out.println(" width = " + (int)bounds.getWidth());
System.out.println(" height = " + (int)bounds.getHeight());
}
public void paint(Graphics page) {
page.fillRect(100, 50, 200, 150);
Polygon p = new Polygon();
p.addPoint(100, 50);
p.addPoint(200, 125);
p.addPoint(300, 50);
page.setColor(Color.black);
page.fillPolygon(p);
// This line starts inside the triangle, ends outside the triangle
page.setColor(Color.yellow);
page.drawLine(150, 75, 150, 100);
System.out.println("Polygon contains...");
System.out.println(" (150, 75) = " + p.contains(150, 75));
System.out.println(" (150, 100) = " + p.contains(150, 100));
System.out.println("Polygon's bounds contains...");
Rectangle bounds = p.getBounds();
System.out.println(" (150, 75) = " + bounds.contains(150, 75));
System.out.println(" (150, 100) = " + bounds.contains(150, 100));
}
public void paint(Graphics page) {
page.fillRect(100, 50, 200, 150);
Polygon p = new Polygon();
p.addPoint(100, 50);
p.addPoint(125, 75);
p.addPoint(100, 100);
page.setColor(Color.blue);
page.fillPolygon(p);
p.translate(50, 50);
page.setColor(Color.yellow);
page.fillPolygon(p);
p.translate(50, -75);
page.setColor(Color.red);
page.fillPolygon(p);
}
public void paint(Graphics page) {
Polygon p1 = new Polygon();
p1.addPoint(50, 50);
p1.addPoint(100, 100);
p1.addPoint(50, 150);
page.setColor(Color.blue);
page.fillPolygon(p1);
Polygon p2 = new Polygon();
p2.addPoint(75, 70);
p2.addPoint(125, 120);
p2.addPoint(175, 70);
page.setColor(Color.red);
page.fillPolygon(p2);
Rectangle bounds1 = p1.getBounds();
Rectangle bounds2 = p2.getBounds();
System.out.println("intersection tests for...");
System.out.println(" polygons: " + shapesIntersect(p1, p2));
System.out.println(" polygon bounds: " + shapesIntersect(bounds1, bounds2));
}
////////////////////////////////////// /// shapesIntersect Helper method //////////////////////////////////////
public static boolean shapesIntersect(Shape s1, Shape s2) {
java.awt.geom.Area area = new java.awt.geom.Area(s1);
area.intersect(new java.awt.geom.Area(s2));
return !area.isEmpty();
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem