Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Gameboard extends JPanel implements MouseListener {
- public Color smallTarget = Color.RED;
- public Color mediumTarget = Color.WHITE;
- public Color largeTarget = Color.WHITE;
- public Color slowSpeed = Color.RED;
- public Color mediumSpeed = Color.WHITE;
- public Color fastSpeed = Color.WHITE;
- public boolean startButton = false;
- public static int targetSize = 0;
- public static int speed = 0;
- public int width = getWidth();
- public int height = getHeight();
- // public static Time time;
- private Target targetInstance;
- private TargetGroup targetGroup;
- private MouseEvent e;
- Time time = new Time();
- static Gameboard panel = new Gameboard();
- public void paint(Graphics g)
- {
- super.paint(g);
- int width = getWidth();
- int height = getHeight();
- Color background = Color.GRAY;
- double ratioX = (double)width/300.0;
- double ratioY = (double)height/200.0;
- Graphics2D g2 = (Graphics2D)g;
- g2.scale(ratioX, ratioY);
- setBackground(background);
- // Target size text
- if (startButton == false)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Times New Roman", Font.PLAIN, 12));
- g.drawString("Size of Targets", 50, 40);
- // Small size Target button
- g.setColor(smallTarget);
- g.fillRect(50, 50, 20, 20);
- // Medium size Target button
- g.setColor(mediumTarget);
- g.fillRect(90, 50, 30, 30);
- // Large size Target button
- g.setColor(largeTarget);
- g.fillRect(140, 50, 40, 40);
- // Slow speed button
- g.setColor(slowSpeed);
- g.setFont(new Font ("", Font.BOLD, 8));
- g.drawString("Slow", 55, 142);
- g.drawRect(50, 130, 30, 20); // 50 + 30 = 80
- // Speed text
- g.setColor(Color.WHITE);
- g.setFont(new Font("Times New Roman", Font.BOLD, 12));
- g.drawString("Speed", 50, 110);
- // Slow speed button
- g.setColor(slowSpeed);
- g.setFont(new Font ("", Font.BOLD, 8));
- g.drawString("Slow", 55, 142);
- g.drawRect(50, 130, 30, 20); // 50 + 30 = 80
- // Medium speed button
- g.setColor(mediumSpeed);
- g.setFont(new Font ("", Font.PLAIN, 8));
- g.drawString("Medium", 105, 142);
- g.drawRect(100, 130, 40, 20); // 100 + 40 = 140
- // Fast speed button
- g.setColor(fastSpeed);
- g.setFont(new Font ("", Font.PLAIN, 8));
- g.drawString("Fast", 167, 142);
- g.drawRect(160, 130, 30, 20); // 160 + 30 = 190
- // Start button
- g.setColor(Color.WHITE);
- g.setFont(new Font ("", Font.PLAIN, 8));
- g.drawString("Start!", 215, 100);
- g.drawRect(210, 90, 30, 15); // 210 + 30 = 240
- }
- if (startButton)
- {
- background = Color.GRAY;
- // g.clearRect(0, 0, width, height);
- setSize(targetSize);
- // setSpeed(speed);
- // targetGroup.draw(g);
- repaint();
- }
- if (startButton)
- {
- try {
- time.start(g);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void setSize(int size)
- {
- targetGroup = new TargetGroup(size);
- }
- // public void setSpeed(int speed)
- // {
- // time = new Time(speed);
- // }
- public static void main(String[] args)
- {
- JFrame w = new JFrame("Gameboard");
- w.setBounds(300, 300, 800, 600);
- w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // panel.setBackground(Color.GRAY);
- w.add(panel);
- w.setResizable(true);
- w.setVisible(true);
- panel.addMouseListener(panel);
- }
- @Override
- public void mouseClicked(MouseEvent e)
- {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- int width = getWidth();
- int height = getHeight();
- double ratioX = width/300.0;
- double ratioY = height/200.0;
- int mouseX = e.getX();
- int mouseY = e.getY();
- if (startButton)
- {
- targetGroup.checksClick(mouseX, mouseY);
- return; // prevents everything below from running
- }
- // If user clicked small Target button
- if (startButton == false && mouseX >= (int)(50 * ratioX) && mouseX <= (int)(70 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(70 * ratioY)) // If cursor clicked on small Target button
- {
- smallTarget = Color.RED;
- mediumTarget = Color.WHITE;
- largeTarget = Color.WHITE;
- // If user clicked medium Target button
- } else if (startButton == false && mouseX >= (int)(90 * ratioX) && mouseX <= (int)(120 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(80 * ratioY)) // If cursor clicked on medium Target button
- {
- smallTarget = Color.WHITE;
- mediumTarget = Color.RED;
- largeTarget = Color.WHITE;
- // If user clicked large Target button
- } else if (startButton == false && mouseX >= (int)(140 * ratioX) && mouseX <= (int)(180 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(90 * ratioY)) // If cursor clicked on large Target setting
- {
- smallTarget = Color.WHITE;
- mediumTarget = Color.WHITE;
- largeTarget = Color.RED;
- // If user clicked slow speed button
- } else if (startButton == false && mouseX >= (int)(50 * ratioX) && mouseX <= (int)(80 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
- {
- slowSpeed = Color.RED;
- mediumSpeed = Color.WHITE;
- fastSpeed = Color.WHITE;
- // If user clicked medium speed button
- } else if (startButton == false && mouseX >= (int)(100 * ratioX) && mouseX <= (int)(140 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
- {
- slowSpeed = Color.WHITE;
- mediumSpeed = Color.RED;
- fastSpeed = Color.WHITE;
- // If user clicked fast speed button
- } else if (startButton == false && mouseX >= (int)(160 * ratioX) && mouseX <= (int)(190 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
- {
- slowSpeed = Color.WHITE;
- mediumSpeed = Color.WHITE;
- fastSpeed = Color.RED;
- // If user clicked start button
- } else if (startButton == false && mouseX >= (int)(210 * ratioX) && mouseX <= (int)(240 * ratioX) && mouseY >= (int)(90 * ratioY) && mouseY <= (int)(105 * ratioY))
- {
- startButton = true;
- // sets targetSize value
- if (smallTarget == Color.RED)
- {
- targetSize = 20;
- } else if (mediumTarget == Color.RED)
- {
- targetSize = 30;
- } else if (largeTarget == Color.RED)
- {
- targetSize = 40;
- }
- // sets speed value
- if (slowSpeed == Color.RED)
- {
- speed = 1500;
- } else if (mediumSpeed == Color.RED)
- {
- speed = 1000;
- } else if (fastSpeed == Color.RED)
- {
- speed = 750;
- }
- }
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement