-
January 26th, 2013, 12:36 PM
#1
How to I put picture buttons inside a hexgrid
Hi, I'm new to Java programming and I am still learning the basics. I have been asked to make a hexagon grid for an assignment at school. I managed to create the grid, and the hexagons are aware of their neighbors. Now I am trying to put a picture of a door,arrow, and light bulb inside some of the hexagons but not all of them. When I click on the light bulb I want some of the surrounding hexagons to turn yellow and when I click on the bulb again I want the surrounding hexagons to turn back to white(the default color). The door and arrow functions will not serve any purpose yet so Im not concern with them. So the question is, how do I do all this?
-
January 26th, 2013, 12:44 PM
#2
Re: How to I put picture buttons inside a hexgrid
Here is my code so far (I use netbeams):
Here is the main class:
package hexagon.project;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;
public class Main extends GraphicsProgram {
@Override
public void init(){
add(new HexMap(13,13,100,100),0,0);
}
Here is the important hexgrid class:
package hexagon.project;
import acm.graphics.GCompound;
import acm.program.GraphicsProgram;
import java.awt.Color;
import java.util.ArrayList;
/**
*
* @author Brian
*/
public class HexMap extends GCompound {
public HexMap(int rows, int cols, int hexWidth, int hexHeight) {
this.rows = rows;
this.cols = cols;
hexmap = new Hexagons[rows][cols];
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
Hexagons hex = new Hexagons(this, rows, cols, hexWidth, hexHeight);
hexmap[row][col] = hex;
add(hex, col * hexWidth + (row % 2 == 1 ? .5 * hexWidth : 0.0), row * hexHeight * .75);
}
}
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
Hexagons hex = hexmap[row][col];
if (row % 2 == 0) {
addNeighbors(hex, row - 1, col, HexDirection.NE);
addNeighbors(hex, row, col + 1, HexDirection.DE);
addNeighbors(hex, row + 1, col, HexDirection.SE);
addNeighbors(hex, row+1, col - 1, HexDirection.SW);
addNeighbors(hex, row, col - 1, HexDirection.DW);
addNeighbors(hex, row - 1, col - 1, HexDirection.NW);
} else {
addNeighbors(hex, row - 1, col+1, HexDirection.NE);
addNeighbors(hex, row, col + 1, HexDirection.DE);
addNeighbors(hex, row + 1, col+1, HexDirection.SE);
addNeighbors(hex, row+1, col, HexDirection.SW);
addNeighbors(hex, row, col - 1, HexDirection.DW);
addNeighbors(hex, row - 1, col, HexDirection.NW);
}
}
}
}
private void addNeighbors(Hexagons theHex,int row, int col, HexDirection dir) {
if (row >= 0 && row < rows && col >= 0 && col < cols) {
theHex.addNeighbors(dir, hexmap[row][col]);
}
}
private Hexagons[][] hexmap;
private int rows;
private int cols;
private Shapes door;
}
This is the Hexagon class(contains shape of hexs):
package hexagon.project;
import acm.graphics.GCompound;
import acm.graphics.GPolygon;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Hexagons extends GCompound {
public Hexagons(HexMap theMap, int mapRow, int mapCol, int width, int height) {
this.map = theMap;
this.row = mapRow;
this.col = mapCol;
this.width = width;
this.height = height;
this.neighbors=new ArrayList<Hexagons>(6);
for(int i=0;i<6;++i){
neighbors.add(null);
}
GPolygon theHex = new GPolygon();
theHex.addVertex(.5 * width, 0.0); //middle top
theHex.addVertex(width, .25 * height); //right top
theHex.addVertex(width, .75 * height); //right bottom
theHex.addVertex(.5 * width, height); //middle bottom
theHex.addVertex(0.0, .75 * height); //left bottom
theHex.addVertex(0.0, .25 * height); //left top
add(theHex);
this.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
String message= String.format("Mouse clicked on hex(%d,%d)",row,col);
JOptionPane.showMessageDialog(null,message);
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
});
}
public void addNeighbors(HexDirection dir, Hexagons hex){
neighbors.set(dir.getInt(), hex);
}
public Hexagons getNeighbors(HexDirection dir){
return neighbors.get(dir.getInt());
}
private int row;
private int col;
private HexMap map;
private int width;
private int height;
private ArrayList<Hexagons>neighbors;
}
Lastly I have a shape class which have the door, arrow, and bulb images(the code for this class is incomplete:
package hexagon.project;
import acm.graphics.GImage;
public class Shapes {
public Shapes(int width, int height){
GImage door = new GImage("door.bmp");
etc...
}
GImage leftArrow;
GImage rightArrow;
GImage door;
}
-
January 26th, 2013, 12:51 PM
#3
Re: How to I put picture buttons inside a hexgrid
P.S my grid is a 13x13 grid. It also should tell you which one you clicked based on the row and col, although I can't seem to get it to display a dialog message that says anything but row 13, col 13
-
January 29th, 2013, 05:25 PM
#4
Re: How to I put picture buttons inside a hexgrid
It also should tell you which one you clicked based on the row and col, although I can't seem to get it to display a dialog message that says anything but row 13, col 13
Code:
Hexagons hex = new Hexagons(this, rows, cols, hexWidth, hexHeight);
should be:
Code:
Hexagons hex = new Hexagons(this, row, col, hexWidth, hexHeight);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|