To set up a bufferedImage and a timer. The Polkadot class. Details in the attached doc.
Microsoft Word - unit2 - graphics (2) Two-31 Java Unit2 Lab10 Polka Dots Objective To set up a bufferedImage and a timer. The Polkadot class. Background Lines 4-7: we need to import many packages. This class "isa" JPanel. Lines 11 & 12: Why are these constants both static and final? Lines 14-18: six private fields. Line 17 says we will be using a Polkadot object. What is that?! At this point, we don’t need to know! Lines 19-28: constructor. We continue to use an off-screen buffer, a timer, and a private Listener to produce motion. All the action is in Lines 35-42. On Line 41, repaint() calls paintComponent, which actually puts the image on the screen. Usually you only call repaint() once, and only at the end of the actionPerformed method. Specification Load Unit2\Lab10\PolkaDot.java. Complete the resource code; see the next page. Load Unit2\Lab10\PolkaDotPanel.java. Study the code above. Filename Unit2\Lab10\Driver10.java. Compile and run. Polkadots keep appearing, one every second. First, modify the code in Polkadot panel so only one polkadot jumps around the screen. After that works, make 2 polkadots of different sizes and colors jump around the screen. Test Data Go to www.tjhsst.edu/compsci. 4 import javax.swing.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.awt.image.*; 8 public class PolkaDotPanel extends JPanel 9 { 10 //constants 11 private static final int FRAME = 400; 12 private static final Color BACKGROUND = new Color(204, 204, 204); 13 //fields 14 private BufferedImage myImage; 15 private Graphics myBuffer; 16 private Timer t; 17 private Polkadot pd; 18 private int xPos, yPos; 19 public PolkaDotPanel() 20 { 21 myImage = new BufferedImage(FRAME, FRAME, BufferedImage.TYPE_INT_RGB); 22 myBuffer = myImage.getGraphics(); 23 myBuffer.setColor(BACKGROUND); 24 myBuffer.fillRect(0, 0, FRAME, FRAME); 25 pd = new Polkadot(); 26 t = new Timer(1000, new Listener()); 27 t.start(); 28 } 29 public void paintComponent(Graphics g) 30 { 31 g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null); 32 } 33 private class Listener implements ActionListener 34 { 35 public void actionPerformed(ActionEvent e) 36 { 37 /************************** 38 your code goes here 39 **************************/ 40 pd.jump(FRAME, FRAME); 41 pd.draw(myBuffer); 42 repaint(); 43 } 44 } 45 } Two-32 Java Unit2 5 import java.awt.*; 6 public class Polkadot 7 { 8 private double myX; // x coordinate of center 9 private double myY; // y coordinate of center 10 private double myDiameter; 11 private Color myColor; 12 private double myRadius; 14 public Polkadot() 15 { 16 myX = 200; 17 myY = 200; 18 myDiameter = 25; 19 myColor = Color.red; 20 myRadius = myDiameter/2; 21 } 22 public Polkadot(double x, double y, double d, Color c) 23 { 24 myX = x; 25 myY = y; 26 myDiameter = d; 27 myColor = c; 28 myRadius = d/2; 29 } 31 public double getX() 32 { 33 return myX; 34 } 35 public double getY() 36 { 37 _____________ 38 } 39 public double getDiameter() 40 { 41 _____________ 42 } 43 public Color getColor() 44 { 45 return myColor; 46 } 47 public double getRadius() 48 { 49 return myRadius; 50 } 52 public void setX(double x) 53 { 54 myX = x; 55 } 56 public void setY(__________) 57 { 58 ______________ 59 } 60 public void setColor(Color c) 61 { 62 myColor = c; 63 } 64 public void setDiameter(double d) 65 { 66 myDiameter = d; 67 myRadius = d/2; 68 } 69 public void setRadius(double r) 70 { 71 __________________ 72 __________________ 73 } 75 public void jump(int rightEdge,int bottomEdge) 76 { //code 80 } 81 public void draw(Graphics myBuffer) 85 { //code 86 } Discussion Polkadot What can a polkadot do? What do they know? The API below is generated from the code at the right.