rhythm_game
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rhythm_game [2023/05/05 12:23] – created frchris | rhythm_game [2023/05/08 16:39] (current) – frchris | ||
---|---|---|---|
Line 2: | Line 2: | ||
[[https:// | [[https:// | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | <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; | ||
+ | | ||
+ | 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, | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * draw | ||
+ | * @param The graphics object to draw itself | ||
+ | */ | ||
+ | public void draw(Graphics g) | ||
+ | { | ||
+ | if (visable) | ||
+ | { | ||
+ | g.setColor(color); | ||
+ | g.fillOval(x, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <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, | ||
+ | { 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() | ||
+ | { | ||
+ | |||
+ | // | ||
+ | titleFont = new Font(" | ||
+ | regularFont = new Font(" | ||
+ | x=0; | ||
+ | y=0; | ||
+ | message=" | ||
+ | dots = new Dot[3]; | ||
+ | dots[0] = new Dot(50,200, 50, Color.BLUE, | ||
+ | dots[1] = new Dot(110, | ||
+ | dots[2] = new Dot(170, | ||
+ | //wait = new Timer(1000, this); | ||
+ | //duration = new Timer(100, this); | ||
+ | // | ||
+ | // | ||
+ | dots[0].setVisable(true); | ||
+ | dots[0].start(); | ||
+ | } | ||
+ | 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.drawString(message, | ||
+ | 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(); | ||
+ | | ||
+ | if (dots[2].contains(x, | ||
+ | | ||
+ | else | ||
+ | | ||
+ | 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; | ||
+ | { | ||
+ | int next = (i+1)%3; | ||
+ | Timer wait = dots[i].getWaitTimer(); | ||
+ | Timer duration = dots[i].getDurationTimer(); | ||
+ | if (e.getSource()==duration){// | ||
+ | duration.stop(); | ||
+ | dots[next].getWaitTimer().start(); | ||
+ | dots[i].setVisable(false); | ||
+ | } | ||
+ | if (e.getSource()==wait) | ||
+ | { | ||
+ | wait.stop(); | ||
+ | duration.start(); | ||
+ | dots[i].setVisable(true); | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | repaint(); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ |
rhythm_game.1683303790.txt.gz · Last modified: 2023/05/05 12:23 by frchris