// file SamplerShell.java // Written by Harriet Fell // January 4, 2002 // This code may be used as a shell for your sampler program. // It opens a graphics window and makes two calls to a function that draws random lines // within a given rectangle import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.image.*; import java.util.Random; public class SamplerShell extends JComponent { // This is not used here but I used a buffered image for individual pixels private BufferedImage bImage; /* The default constructor is fine now but you may need a constructor, e.g. for a new BufferedImage. public SamplerShell() { } */ public void someLines(Graphics2D g, int x0, int y0, int width, int height) { g.setStroke(new BasicStroke(1)); // Line width is 1 Random rnums = new Random(); // A new random number generator // This draw 200 pairs of random lines, one from the left to right of the rectangle // and one from top to bottom. for (int lNum = 0; lNum < 200; lNum++) { // x1 and x2 are for the lines from top to bottom int x1 = rnums.nextInt(width); int x2 = rnums.nextInt(width); // y1 and y2 are for the lines from left to right int y1 = rnums.nextInt(height); int y2 = rnums.nextInt(height); // A new random color is selected for each line Color l1Color = new Color (rnums.nextInt(255), rnums.nextInt(255), rnums.nextInt(255)); g.setPaint(l1Color); g.drawLine(x0 + x1, y0, x0 + x2, y0 + height); // top to bottom line Color l2Color = new Color (rnums.nextInt(255), rnums.nextInt(255), rnums.nextInt(255)); g.setPaint(l2Color); g.drawLine(x0, y0 + y1, x0 + width, y0 + y2); // left to right line } } public void drawPic(Graphics2D g, int x, int y, int width, int height){ //sky Color skyColor = new Color(100, 100, 255); g.setPaint(skyColor); g.fillRect(x, y, width, height); // cloud g.setPaint(Color.white); g.fillOval(x + 5*width/10, y + 2*height/10, 2*width/10, 2*height/10); g.fillOval(x + 7*width/10, y + 2*height/10, 2*width/10, 2*height/10); g.fillOval(x + 6*width/10, y + height/10, 2*width/10, 2*height/10); // house g.setPaint(Color.red); g.fillRect(x + width/10, y + 5*height/10, 6*width/10, 5*height/10); // Door and Window g.setPaint(Color.black); g.fillRect(x + 2*width/10, y + 7*height/10, width/10, 3* height/10); g.fillRect(x + 4*width/10, y + 6*height/10, 2*width/10, 2*height/10); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; int wd = getSize().width; int ht = getSize().height; // some lines by function someLines(g2,0, ht/3,wd/3, ht/3); // another call to some lines // someLines(g2, wd/3, 0, wd/3, ht/3); drawPic(g2, wd/3, 2*ht/3, 2*wd/3, ht/3); drawPic(g2, wd/3, 0, wd/3, ht/3); } public static void main(String[] args){ JFrame f = new JFrame("SamplerShell"); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(new SamplerShell(), BorderLayout.CENTER); f.setSize(600, 622); // 22 pixels for the top border f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); } }