Write a complete working program that draws a hexagon in the middle of the frame
ID: 3734933 • Letter: W
Question
Write a complete working program that draws a hexagon in the middle of the frame. When the user clicks on a hexagon, it then replaces it with 4 smaller hexagons, each of which is a quarter of the size of the original hexagon and has a random color. If user stops clicking (i.e., remains inactive for 15 seconds), all hexagons should start moving in random directions (any speed is fine, overlaps are fine but not preferred). If user presses 'ESC', the frame is cleared and you start all over again. Test your program to draw unique patterns. java
Explanation / Answer
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class DrawHexagon extends JPanel implements MouseListener,
ActionListener {
/**
* An array list of hexagons
*/
ArrayList<Hexagon> hexagons;
// other attributes
static int HEIGHT = 500;
static int WIDTH = 500;
int initialSize = 150;
Random random;
Timer moveTimer;
int timerSpeed = 100; // speed of timer
int initialDelay = 15000; // 15 seconds after a click
/**
* constructor to initialize the panel
*/
public DrawHexagon() {
setSize(new Dimension(WIDTH, HEIGHT));
/**
* initializing the list of hexagons
*/
hexagons = new ArrayList<Hexagon>();
random = new Random();
/**
* defining the timer
*/
moveTimer = new Timer(timerSpeed, this);
/**
* invoking initial setup, (create and draw one hexagon at the middle)
*/
initialSetup();
/**
* adding mouse listener
*/
addMouseListener(this);
}
/**
* method to create a hexagon and return it
*
* @param x
* - center x coordinate
* @param y
* - center y coordinate
* @param size
* - size of hexagon
* @return- a hexagon object
*/
Hexagon createHexagon(int x, int y, int size) {
/**
* Defining a hexagon with random color
*/
Hexagon hexagon = new Hexagon(x, y, size, new Color(
random.nextInt(255), random.nextInt(255), random.nextInt(255)));
for (int i = 0; i < 7; i++) {
double a = Math.PI / 3.0 * i;
hexagon.addPoint((int) (Math.round(x + Math.sin(a) * size)),
(int) (Math.round(y + Math.cos(a) * size)));
}
return hexagon;
}
@Override
public void paint(Graphics g) {
super.paint(g);
/**
* drawing all hexagons
*/
for (Hexagon h : hexagons) {
g.setColor(h.color);
g.fillPolygon(h);
}
}
/**
* method to load the initial hexagon
*/
public void initialSetup() {
hexagons = new ArrayList<Hexagon>();
Hexagon firstHexagon = createHexagon(getWidth() / 2, getHeight() / 2,
initialSize);
/**
* Adding to the list
*/
hexagons.add(firstHexagon);
repaint();
}
public static void main(String[] args) {
/**
* Defining a frame and adding a DrawHexagon object
*/
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final DrawHexagon drawHexagon = new DrawHexagon();
frame.add(drawHexagon);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
/**
* Adding the key listener for ESC key type detection
*/
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent ke) {
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();
if (code == KeyEvent.VK_ESCAPE) {
drawHexagon.moveTimer.stop();
drawHexagon.initialSetup();
}
}
});
}
@Override
public void mouseClicked(MouseEvent me) {
/**
* getting the point of click
*/
Point p = me.getPoint();
/**
* looping through all hexagons
*/
for (Hexagon h : hexagons) {
/**
* checking if the clicked point is inside a hexagon
*/
if (h.contains(p)) {
/**
* creating 4 hexagons
*/
Hexagon topLeft = createHexagon(h.x - h.size / 2, h.y - h.size
/ 2, h.size / 2);
Hexagon topRight = createHexagon(h.x + h.size / 2, h.y - h.size
/ 2, h.size / 2);
Hexagon bottomLeft = createHexagon(h.x - h.size / 2, h.y
+ h.size / 2, h.size / 2);
Hexagon bottomRight = createHexagon(h.x + h.size / 2, h.y
+ h.size / 2, h.size / 2);
/**
* adding to the list
*/
hexagons.add(topLeft);
hexagons.add(topRight);
hexagons.add(bottomLeft);
hexagons.add(bottomRight);
/**
* removing the parent hexagon
*/
hexagons.remove(h);
repaint();
break;
}
}
/**
* resetting the timer each time mouse click happens, so that when it
* becomes idle for 15 seconds, the timer will get executed
*/
moveTimer.stop();
moveTimer.setInitialDelay(initialDelay);
moveTimer.start();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent ae) {
/**
* action listener for the timer
*/
for (Hexagon h : hexagons) {
/**
* checking if the current hexagon is moving
*/
if (!h.isMoving) {
//not moving, assigning a random direction
int randomXdir = -1 + random.nextInt(3); // -1,0 or 1
int randomYdir = -1 + random.nextInt(3);
h.movingX = randomXdir;
h.movingY = randomYdir;
h.isMoving = true;
}
//translating the hexagon to the defined direction
h.translate(h.movingX, h.movingY);
}
repaint();
}
}
/**
* an inner class to represent one hexagon
*/
class Hexagon extends Polygon {
int x;//x coord
int y;//y coord
int size;//hexagon size
Color color;//color of hexagon
boolean isMoving;//denote if it is moving or not
int movingX;//moving direction- X
int movingY;//moving direction- Y
public Hexagon(int x, int y, int size, Color color) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
isMoving = false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.