User Tools

Site Tools


rhythm_game

Differences

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

Link to this comparison view

Next revision
Previous revision
rhythm_game [2023/05/05 12:23] – created frchrisrhythm_game [2023/05/08 16:39] (current) frchris
Line 2: Line 2:
  
 [[https://mathorama.com/apcs/pmwiki.php?n=Main.TimerApplicaiton|Timer Application]] [[https://mathorama.com/apcs/pmwiki.php?n=Main.TimerApplicaiton|Timer Application]]
 +
 +{{::screen_recording_2023-05-08_at_1.31.43_pm.gif?400|}}
 +
 +<code java Dot.java>
 +import java.awt.Color;
 +import java.awt.*;
 +import javax.swing.Timer;
 +import java.awt.event.*;
 +
 +public class Dot 
 +{
 +    private int x, y, size ;
 +    private Color color;
 +     private Timer wait, duration;
 +    boolean visable;
 +    public Dot(int x, int y, int size, Color c, ActionListener app,
 +                   int w, int dur) 
 +    {
 +        this.x = x;
 +        this.y = y;
 +        this.size = size;
 +        color = c;
 +        visable = false;
 +        wait = new Timer(w, app);
 +        duration = new Timer(dur, app);
 +        color = c;
 +        //color = randomColor();
 +    }
 +    public void start(){
 +        wait.start();
 +    }
 +    public Timer getWaitTimer(){
 +        return wait;
 +    }
 +    public Timer getDurationTimer(){
 +        return duration;
 +    }
 +
 +    public boolean isVisable(){ return visable;}
 +    public void setVisable(boolean b){  visable = b;}
 +    public boolean contains (int a, int b)
 +    {
 +        if (!visable) return false;
 +        double r = .5 * size;
 +        double dx = (this.x + r) -a;
 +        double dy = (this.y + r) -b;
 +        //return (int)(Math.sqrt(dx*dx+dy*dy ));
 +        return (dx*dx + dy*dy <= r*r);
 +    }
 +
 +    /**
 +     * randomInt 
 +     * @param min
 +     * @param max
 +     * @return a random integer from min to max (inclusive)
 +     */
 +    public int randomInt(int min, int max){
 +        return min + (int)(Math.random()*(max-min+1));
 +    }
 +
 +    /**
 +     * randomColor
 +     * @return random Color
 +     */
 +    public Color randomColor()
 +    {
 +        return new Color( randomInt(0,255), randomInt(0,255), randomInt(0,255)); 
 +    }
 +
 +    /**
 +     * draw
 +     * @param The graphics object to draw itself
 +     */
 +    public void draw(Graphics g)
 +    {
 +        if (visable) 
 +        {
 +            g.setColor(color);
 +            g.fillOval(x, y, size, size);
 +        }
 +    }
 +
 +    
 +}
 +</code>
 +
 +<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.Timer;
 +import java.awt.event.*;
 +
 +import javax.swing.JFrame;
 +import javax.swing.JPanel;
 +
 +public class DotTester extends JPanel implements MouseListener, ActionListener
 +{    public static int WIDTH=800;
 +    public static int HEIGHT=600;
 +    private Font titleFont, regularFont;
 +    private int x,y;
 +    private Dot[] dots;
 +    private String message;
 +    
 +
 +    public DotTester()
 +    {
 +
 +        //initialize variables here...
 +        titleFont = new Font("Roman", Font.BOLD, 18);
 +        regularFont = new Font("Helvetica", Font.PLAIN, 12);
 +        x=0;
 +        y=0;
 +        message="Hit the red dot";
 +        dots = new Dot[3];
 +        dots[0] = new Dot(50,200, 50, Color.BLUE,  this,400, 350);
 +        dots[1] = new Dot(110,200, 50, Color.BLUE, this, 10, 490);
 +        dots[2] = new Dot(170,200, 50, Color.RED, this, 10, 150);
 +        //wait = new Timer(1000, this);
 +        //duration = new Timer(100, this);
 +        //wait.addActionListener(this);
 +        //duration.addActionListener(this);
 +        dots[0].setVisable(true);
 +        dots[0].start();
 +    }
 +    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.drawString(message, 500,200);
 +        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();
 +        
 +        if (dots[2].contains(x,y) )
 +           message = "hit!";
 +        else
 +           message = "miss";
 +        repaint();
 +    }
 +    @Override
 +    public void mouseEntered(MouseEvent e) {
 +
 +    }
 +    @Override
 +    public void mouseExited(MouseEvent e) {
 +
 +    }
 +    public void actionPerformed(ActionEvent e) {
 +
 +        for(int i=0; i< dots.length; i++)
 +        {
 +            int next = (i+1)%3;
 +            Timer wait = dots[i].getWaitTimer();
 +            Timer duration = dots[i].getDurationTimer();
 +        if (e.getSource()==duration){// time turn off
 +            duration.stop();
 +            dots[next].getWaitTimer().start();
 +            dots[i].setVisable(false);
 +        }
 +        if (e.getSource()==wait)
 +        { 
 +            wait.stop();
 +            duration.start();
 +            dots[i].setVisable(true);
 +            
 +        }
 +       }
 +        repaint();
 +    }
 +
 +
 +}
 +</code>
 +
 +
rhythm_game.1683303790.txt.gz · Last modified: 2023/05/05 12:23 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki