How to resolve the algorithm Honeycombs step by step in the Java programming language
How to resolve the algorithm Honeycombs step by step in the Java programming language
Table of Contents
Problem Statement
The task is to produce a matrix of 20 hexagon shaped widgets in a honeycomb arrangement. The matrix should be arranged in such a manner that there are five columns of four hexagons. The hexagons in columns one, three and five are aligned horizontally, whereas the hexagons in columns two and four occupy a lower position within the arrangement. Each hexagon should be the same colour, and should display a unique randomly selected single capital letter on the front. The application should now wait for the user to select a hexagon, either by using a pointing device, or by pressing a key that carries a corresponding letter on a hexagon. For platforms that support pointing devices and keyboards, the application should support both methods of selection. A record of the chosen letters should be maintained and the code should be suitably commented, at the point where the the selected letter has been determined. The selected hexagon should now change colour on the display. The cycle repeats until the user has chosen all of the letters. Note that each letter can only be selected once and previously selected hexagons retain their colour after selection. The program terminates when all letters have been chosen. Optionally: output the list of selected letters and show the last selected letter, cater for a different number of columns or a different number of hexagons in each column, cater for two players, (turns alternate and the hexagons change a different colour depending on whether they were selected by player one or player two and records of both players selections are maintained.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Honeycombs step by step in the Java programming language
The provided code is a Java program that creates a simple graphical user interface (GUI) with a hexagonal honeycomb pattern of buttons. Each button is labeled with a letter, and the user can click on the buttons or press the corresponding letter on the keyboard to select them.
The program is structured as follows:
- The
Honeycombs
class is the main class of the program and extends theJFrame
class to create a window. - The
HoneycombsPanel
class extends theJPanel
class and is responsible for drawing the hexagonal honeycomb pattern and handling user input. - The
Hexagon
class extends thePolygon
class and represents a single hexagon in the honeycomb pattern.
When the program is run, the main
method creates a new instance of the Honeycombs
class and sets it to be visible. The HoneycombsPanel
class is added to the Honeycombs
class as the center component, and the window is given a title and made non-resizable. The window is then packed to fit its contents and centered on the screen.
The HoneycombsPanel
class has a preferred size of 600 by 500 pixels and a white background. It is focusable, which means that it can receive keyboard input.
The HoneycombsPanel
class has two event listeners, one for mouse input and one for keyboard input. The mouse event listener listens for mouse clicks and selects the hexagon that is clicked. The keyboard event listener listens for key presses and selects the hexagon that corresponds to the pressed key.
The HoneycombsPanel
class also has an array of Hexagon
objects, which represent the hexagonal honeycomb pattern. The array is initialized with 20 Hexagon
objects, each with a different letter label. The hexagons are arranged in a honeycomb pattern, with 12 hexagons in the top row and 8 hexagons in the bottom row.
The Hexagon
class has a baseColor
and a selectedColor
. The baseColor
is yellow, and the selectedColor
is magenta. The Hexagon
class also has a letter
field, which stores the letter that is labeled on the hexagon.
The Hexagon
class has a constructor that takes three parameters: the x-coordinate of the hexagon's center, the y-coordinate of the hexagon's center, and the half-width of the hexagon. The constructor uses the half-width to calculate the coordinates of the hexagon's six vertices.
The Hexagon
class has a setSelected
method that sets the hasBeenSelected
field to true
. The Hexagon
class also has a draw
method that draws the hexagon and the letter label on the hexagon. The draw
method uses the baseColor
or selectedColor
to fill the hexagon, depending on the value of the hasBeenSelected
field. The draw
method also uses the Color.black
to draw the outline of the hexagon and the Color.red
or Color.black
to draw the letter label, depending on the value of the hasBeenSelected
field.
When the Honeycombs
class is run, the HoneycombsPanel
class is created and added to the Honeycombs
class. The HoneycombsPanel
class creates an array of Hexagon
objects and draws the hexagonal honeycomb pattern. The HoneycombsPanel
class also adds event listeners for mouse input and keyboard input. The Honeycombs
class then packs its contents and centers the window on the screen.
Source code in the java programming language
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Honeycombs extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new Honeycombs();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
});
}
public Honeycombs() {
add(new HoneycombsPanel(), BorderLayout.CENTER);
setTitle("Honeycombs");
setResizable(false);
pack();
setLocationRelativeTo(null);
}
}
class HoneycombsPanel extends JPanel {
Hexagon[] comb;
public HoneycombsPanel() {
setPreferredSize(new Dimension(600, 500));
setBackground(Color.white);
setFocusable(true);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
for (Hexagon hex : comb)
if (hex.contains(e.getX(), e.getY())) {
hex.setSelected();
break;
}
repaint();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
for (Hexagon hex : comb)
if (hex.letter == Character.toUpperCase(e.getKeyChar())) {
hex.setSelected();
break;
}
repaint();
}
});
char[] letters = "LRDGITPFBVOKANUYCESM".toCharArray();
comb = new Hexagon[20];
int x1 = 150, y1 = 100, x2 = 225, y2 = 143, w = 150, h = 87;
for (int i = 0; i < comb.length; i++) {
int x, y;
if (i < 12) {
x = x1 + (i % 3) * w;
y = y1 + (i / 3) * h;
} else {
x = x2 + (i % 2) * w;
y = y2 + ((i - 12) / 2) * h;
}
comb[i] = new Hexagon(x, y, w / 3, letters[i]);
}
requestFocus();
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("SansSerif", Font.BOLD, 30));
g.setStroke(new BasicStroke(3));
for (Hexagon hex : comb)
hex.draw(g);
}
}
class Hexagon extends Polygon {
final Color baseColor = Color.yellow;
final Color selectedColor = Color.magenta;
final char letter;
private boolean hasBeenSelected;
Hexagon(int x, int y, int halfWidth, char c) {
letter = c;
for (int i = 0; i < 6; i++)
addPoint((int) (x + halfWidth * Math.cos(i * Math.PI / 3)),
(int) (y + halfWidth * Math.sin(i * Math.PI / 3)));
getBounds();
}
void setSelected() {
hasBeenSelected = true;
}
void draw(Graphics2D g) {
g.setColor(hasBeenSelected ? selectedColor : baseColor);
g.fillPolygon(this);
g.setColor(Color.black);
g.drawPolygon(this);
g.setColor(hasBeenSelected ? Color.black : Color.red);
drawCenteredString(g, String.valueOf(letter));
}
void drawCenteredString(Graphics2D g, String s) {
FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int dec = fm.getDescent();
int x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2;
int y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2);
g.drawString(s, x, y);
}
}
You may also check:How to resolve the algorithm FizzBuzz step by step in the Janet programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Rust programming language
You may also check:How to resolve the algorithm Leap year step by step in the C programming language
You may also check:How to resolve the algorithm FTP step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Search a list step by step in the DWScript programming language