Dot Class
BlueJ is a great “starter IDE” but eventually you will want more of the features of a more professional IDE. Also, if you only know one IDE, do you really know what an IDE is? It is good to see what is the same and what is different between two or more IDEs to see the strengths and weaknesses in a given context.
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.
Here is a link to making a Dot class starting with making a tester. At the time, Applets were a simple way to write graphics based code, but Applets have since been “deprecated” due to security concerns. You may wish to fast forward to minute 10:45 to see the beginnings of the Dot class.
Below is a full fledged Graphics application that was based on a generic MouseListenerApplication. Writing code that is reusable saves a lot of labor and debugging time.
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) { } }
Here is a bit of starter code to save you time, but feel free to write this from scratch!
/** * * @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 */ }
Things to do:
min
to max
Graphic
objecttoString
method to override the inherited Object
toString()
method.