dot_class
This is an old revision of the document!
Dot Class
Extreme programming refers to the wise tradition to write the code that tests your new object before you actually write the new object. It really does help you define what you need from your new class.
- DotTester.java
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; public class DotTester extends JPanel implements MouseListener { public static int WIDTH=800; public static int HEIGHT=600; private Font titleFont, regularFont; private int x,y; private ArrayList<Dot> dots; public DotTester() { //initialize variables here... titleFont = new Font("Roman", Font.BOLD, 18); regularFont = new Font("Helvetica", Font.PLAIN, 12); x=0; y=0; dots = new ArrayList<Dot>(); } public static void main(String[] args) { DotTester app= new DotTester(); JFrame window = new JFrame("Dot Tester"); window.setSize(WIDTH, HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(app); window.getContentPane().addMouseListener(app); window.setVisible(true); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(),getHeight()); g.setColor(Color.BLUE); g.setFont(titleFont); g.drawString("Dot Tester", 20, 20); g.drawString("X = "+x+" Y = "+y, 20, 100); g.setColor(Color.BLACK); g.setFont(regularFont); g.drawString("Click with your mouse", 20, 40); for (Dot d:dots) d.draw(g); } // update is a workaround to cure Windows screen flicker problem public void update(Graphics g){ paint(g); } /** * These are the Methods needed to implement the MouseListener Interface */ @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { x=e.getX(); y=e.getY(); dots.add(new Dot(x, y)); repaint(); } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }
- Dot.java
/** * * @author First Last * @version date/or date and time * */ public class Dot { private int x, y, size; private Color color; public Dot(int x, int y) { this.x = x; this.y = y; size = 10; } /** * randomInt * @param min * @param max * @return a random integer from min to max (inclusive) */ /** * randomColor * @return random Color */ /** * draw * @param The graphics object to draw itself */ }
dot_class.1663248270.txt.gz · Last modified: 2022/09/15 09:24 by frchris