dot_class
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
dot_class [2020/09/15 10:06] – created frchris | dot_class [2022/09/15 09:48] (current) – [Dot class] frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
**Dot Class** | **Dot Class** | ||
+ | |||
+ | BlueJ is a great " | ||
+ | |||
+ | |||
+ | |||
+ | Extreme programming refers to the wise tradition to write the code that tests your new object before you actually write the new object. | ||
+ | |||
+ | Here is a [[http:// | ||
+ | |||
+ | Below is a full fledged Graphics application that was based on a generic [[https:// | ||
+ | |||
+ | <code java 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< | ||
+ | |||
+ | |||
+ | public DotTester() | ||
+ | { | ||
+ | |||
+ | // | ||
+ | titleFont = new Font(" | ||
+ | regularFont = new Font(" | ||
+ | x=0; | ||
+ | y=0; | ||
+ | dots = new ArrayList< | ||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | DotTester app= new DotTester(); | ||
+ | JFrame window = new JFrame(" | ||
+ | window.setSize(WIDTH, | ||
+ | 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, | ||
+ | g.setColor(Color.BLUE); | ||
+ | g.setFont(titleFont); | ||
+ | g.drawString(" | ||
+ | g.drawString(" | ||
+ | g.setColor(Color.BLACK); | ||
+ | g.setFont(regularFont); | ||
+ | g.drawString(" | ||
+ | |||
+ | 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 class==== | ||
+ | |||
+ | Here is a bit of starter code to save you time, but feel free to write this from scratch! | ||
+ | |||
+ | <code java 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 | ||
+ | */ | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Things to do: | ||
+ | - Write a two parameter constructor | ||
+ | - Write a method that returns a random number that is in the range of '' | ||
+ | - Write a method that returns a random Color | ||
+ | - Write a method that will draw itself on a '' | ||
+ | - Write a '' | ||
+ | - Write overload your constructor a few times | ||
+ |
dot_class.1600178769.txt.gz · Last modified: 2020/09/15 10:06 by frchris