Skip to Content
Computer Graphics for Java Programmers, Second Edition
book

Computer Graphics for Java Programmers, Second Edition

by Leen Ammeraal, Kang Zhang
March 2007
Intermediate to advanced
386 pages
7h 59m
English
Wiley
Content preview from Computer Graphics for Java Programmers, Second Edition

Appendix F. Hints and Solutions to Exercises

1.1 Numbers of pixels:

g.drawLine (10, 20, 100, 50); // 100 - 10 + 1 = 91 pixels
g.drawRect (10, 10, 8, 5);    // 2 * 8 + 2 * 5 = 26 pixels
g.fillRect (10, 10, 8, 5);    // 8 * 5 = 40 pixels

1.2 Program to draw many squares:

// ManySq.java: This program draws n x n sets, each
//   consisting of k squares, arranged as on a chessboard.
//   Each edge is divided into two parts with ratio
//   (1 - q)  : q. The values of n, k and q are program
              arguments.

import java.awt.*;
import java.awt.event.*;

public class ManySq extends Frame
{  public static void main(String[] args)
   {  if (args.length != 3)
      {  System.out.println("Supply n, k and q as arguments");
         System.exit(1);
} int n = Integer.valueOf(args[0]).intValue(), k = Integer.valueOf(args[1]).intValue(); float q = Float.valueOf(args[2]).floatValue(); new ManySq(n, k, q); } ManySq(int n, int k, float q) { super("ManySq: Many squares"); addWindowListener(new WindowAdapter() {public void windowClosing( WindowEvent e){System.exit(0);}}); add("Center", new CvManySq(n, k, q)); setSize (600, 400); show(); } } class CvManySq extends Canvas { int centerX, centerY, n, k; float p0, q0; CvManySq(int nn, int kk, float qq){n=nn; k=kk; q0=qq; p0 = 1-q0;} int iX(float x){return Math.round(centerX + x);} int iY(float y){return Math.round(centerY - y);} public void paint(Graphics g) { Dimension d = getSize(); int maxX = d.width - 1, maxY = d.height - 1, minMaxXY = Math.min(maxX, maxY); centerX = maxX/2; centerY = maxY/2; float ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java 2D Graphics

Java 2D Graphics

Jonathan Knudsen
Java Swing, 2nd Edition

Java Swing, 2nd Edition

Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole

Publisher Resources

ISBN: 9780470031605Purchase book