User Tools

Site Tools


resources_for_dotclicker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
resources_for_dotclicker [2022/05/09 15:39] – created frchrisresources_for_dotclicker [2022/05/09 15:42] (current) frchris
Line 1: Line 1:
 **Dot Clicker** **Dot Clicker**
  
-Here is a dot that has a "is hit" method+Here is a dot that has a "is hit" method.  If you want an image instead, take a look at this [[https://mathorama.com/apcs/pmwiki.php?n=Main.ImageMovementApplication|Image Movement Example]]
 <code> <code>
 import java.awt.Graphics; import java.awt.Graphics;
Line 46: Line 46:
  
 <code> <code>
 +import java.awt.Color;
 +import java.awt.Font;
 import java.awt.Graphics; import java.awt.Graphics;
-import java.awt.Color+import java.awt.event.MouseEvent; 
-public class Dot+import java.awt.event.MouseListener; 
 +import java.util.ArrayList; 
 +import javax.swing.JFrame; 
 +import javax.swing.JPanel; 
 +import javax.swing.Timer; 
 +import java.awt.event.ActionEvent; 
 +import java.awt.event.ActionListener
 +public class DotClicker extends JPanel implements MouseListener, ActionListener
 { {
-    private int x,y,size+    public static int WIDTH=800; 
-    /** +    public static int HEIGHT=600; 
-     * Constructor for objects of class Dot +    private Font titleFont, regularFont; 
-     */ +    private int x,y, hits, misses
-    public Dot()+    private Timer spawn, expire; 
 +    private ArrayList<Dot> dots; 
 +    public DotClicker()
     {     {
-        50+(int)(500*Math.random()); +        titleFont new Font("Roman", Font.BOLD, 18)
-        50+(int)(300*Math.random()); +        regularFont = new Font("Helvetica", Font.PLAIN, 12); 
-        size = 10 +(int)(20*Math.random());+        spawn = new Timer(1000,this); 
 +        expire new Timer(3000,this)
 +        dots = new ArrayList<Dot>(); 
 +        dots.add(new Dot()); 
 +        spawn.start()
 +        expire.start();
     }     }
  
-    public Dot(int x,int y,int size+    public static void main(String[] args) { 
-    { +        DotClicker app= new DotClicker(); 
-        this.x = x+        JFrame window = new JFrame("Mouse Listener Application"); 
-        this.y=y+        window.setSize(WIDTHHEIGHT); 
-        this.size = size;+        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 +        window.getContentPane().add(app)
 +        window.getContentPane().addMouseListener(app)
 +        window.setVisible(true);
     }     }
  
-    public void draw(Graphics g){+    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 Clicker Starter Code", 20, 20);
         g.setColor(Color.BLACK);         g.setColor(Color.BLACK);
-        g.fillOval(x,y,size,size); +        g.setFont(regularFont); 
-    } +        g.drawString("Hits: "+hits2040); 
 +        g.drawString("Misses: "+misses20, 60); 
 +        //Draw objects 
 +        for(Dot d:dots) 
 +             d.draw(g);  
 +    }     
 +    
     /**     /**
-     returns true if (x0,y0) is inside the dot+     These are the Methods needed to implement the MouseListener Interface
      */      */
-    public boolean isHit(int x0, int y0+    public void mouseClicked(MouseEvent e{} 
-    { +    public void mousePressed(MouseEvent e) {} 
-        double x2 x0 - (x+size/2.0); +    public void mouseReleased(MouseEvent e) 
-        double y2 y0 - (y+size/2.0); +        x=e.getX(); 
-        return x2*x2 + y2*y2 - size*size <0;// pythaorean theorem+        y=e.getY(); 
 +        for(int i=dots.size()-1; i >= 0; i--) 
 +        { 
 +            if (dots.get(i).isHit(x,y)) 
 +            { 
 +                hits++; 
 +                dots.remove(i); 
 +                repaint(); 
 +            } 
 +        
 +        repaint();
     }     }
 +    public void mouseEntered(MouseEvent e) {}
 +    public void mouseExited(MouseEvent e) { }
 +    public void actionPerformed(ActionEvent e) {
 +      if (e.getSource()==spawn){
 +     dots.add( new Dot() );
 +      }else if (dots.size()>0){// remove oldest
 +          dots.remove(0);
 +          misses++;
 +      }
 +      repaint();
 + }
 } }
- 
 </code> </code>
resources_for_dotclicker.1652125175.txt.gz · Last modified: 2022/05/09 15:39 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki