Delivery:
Insert the code that draws a repeated hexagon in a honeycomb structure.


Code:
Tela Class
package esagoni; import java.awt.*; import javax.swing.*; public class Tela extends JPanel{ public void paintComponent(Graphics g) { //used to draw in the window super.paintComponent(g); //the background color is set this.setBackground(Color.WHITE); //the color of g is set, so the color with which the drawings made using g will appear g.setColor(Color.BLACK); //the two vectors x and y are initialized. vector x contains the x-coordinates of the six points //of the hexagon, while vector y contains the y-coordinates of the points. the hexagon //has 6 points so the vectors have 6 elements. int x[] = new int[6]; int y[] = new int[6]; //the vectors are filled with the coordinates of the points vettoreX1(x); vettoreY(y); // for (int i = 0; i<10; i++){ //10 hexagons are drawn, distant 40 horizontally for (int a = 0; a < 10; a++){ esagoniX(x,y,g); } //the point from which to start drawing is moved down by 10 cambioY(x,y,g); //the vector x is filled with other coordinates, so the point from which to start //drawing is moved 10 horizontally, to avoid overlapping hexagons vettoreX2(x); //10 more hexagons are drawn for (int a = 0; a < 10; a++){ esagoniX(x,y,g); } //the point from which to start drawing is moved down by 10 cambioY(x,y,g); //the first vector is reused, with x coordinates equal to //those of the first row vettoreX1(x); } } public int[] vettoreX1 (int x[]){ x[0] = -40; x[1] = x[0]+10; x[2] = x[1]+10; x[3] = x[2]+10; x[4] = x[2]; x[5] = x[1]; return x; } public int[] vettoreX2(int x[]){ x[0] = -20; x[1] = x[0]+10; x[2] = x[1]+10; x[3] = x[2]+10; x[4] = x[2]; x[5] = x[1]; return x; } public int[] vettoreY (int y[]){ y[0] = 10; y[1] = y[0]+10; y[2] = y[1]; y[3] = y[0]; y[4] = y[0]-10; y[5] = y[4]; return y; } public void esagoniX (int x[], int y[], Graphics g){ for (int i = 0; i < x.length; i++){ x[i] = x[i]+40; } //x = vector that contains 6 horizontal coordinates //y = vector that contains 6 vertical coordinates //6 = number of points g.drawPolygon(x, y, 6); } public void cambioY (int x[], int y[], Graphics g){ for (int i = 0; i < y.length; i++){ y[i] = y[i]+10; } } } Esagoni Class
package esagoni; //importing java swing is used to be able to use the components it contains, for //example jframe import javax.swing.*; public class Esagoni { public static void main(String[] args) { //used to change the writing at the top left JFrame t = new JFrame("Esagoni"); //setDefaultCloseOperation() contains in parentheses the operation //that the program performs when the x at the top right is clicked //the writing in parentheses indicates that when the x is clicked, //the default operation //is to close the window t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //the other class is called Tela, so a new Tela object is created Tela p = new Tela(); //t is the JFrame, to which the object p (tela) is added t.add(p); //dimensions of the canvas (the window that opens when the program is executed) t.setSize(1000, 1000); //it was only created, and now it is made visible t.setVisible(true); } } Procedure:
After creating a new project called disegno, we created two classes. The one with the main is Esagoni, while the other is Tela.
To draw the hexagons with the honeycomb structure, you need to draw hexagons. This can be done in two ways:
•g.drawPolygon(obj Polygon)
•g.drawPolygon(x[], y[], i)
The first method allows you to draw a polygon by putting the polygon object in parentheses, while the second allows you to do it with two integer arrays and an integer. The x vector contains the x coordinates of the points and the y vector contains the y coordinates of the points, while i is the number of points of the polygon, i.e. the vertices.
In the honeycomb structure, there are many hexagons in a row, arranged in multiple rows, so you need to draw equal hexagons, in the same row, at a certain horizontal distance.
After drawing the first row, the same thing is done for the second, but in this case the x coordinate of the first point of the hexagon is not the same as that of the first point of the first hexagon of the row below, so you need to start from a different x point. In the third row, instead, the x coordinate of the first point of the first hexagon is equal to that of the first point of the first hexagon of the first row, so the x coordinates of the points are equal every two rows.

