I have code for a hexgrid(below)which has been graciously provided by kevglass. I am wondering how to display a background Image ’ 'Below ’ ’ the grid. Code would be wonderful…
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
/**
* A simple test class to draw a hexagon grid. Currently this class
* draws directly onto the background of the JFrame. This would be nicer
* if it was pulles out into a seperate JPanel or canvas.
*
* This uses pure Java 2D, not accelerated.
*
* Note: This could be done with java.awt.Polygon much more simply.
* Note: This code isn't nice :)
*
* @author Kevin Glass
*/
public class Puzzle extends JFrame {
/** The controlable size of the hexagons */
private static final int SIZE = 15;
/** The width of the grid (in hexagons) */
private static int WIDTH = 10;
/** The height of the grid (in hexagons) */
private static int HEIGHT = 40;
/**
* Creates the new puzzle class. This will create the window and
* cause it to be displayed
*/
public Puzzle() {
// call the super class to set the title
super("Steel Platoon Combat -Chris Gillis(poisonousparrot)-kevglass");
// this add a listener that will exit the program is the window
// created here is closed
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// stop the program
System.exit(0);
}
});
// set the size of the window
setSize(700,700);
// make the frame visible to the user
setVisible(true);
}
/**
* The AWT callback for drawing this frame. This is called by the AWT system
* when drawing this frame. You must use the graphics context "g" to draw
* what ever you want to appear in the window. In this case a hexagaon
* grid.
*
* @param g The graphics context on which to draw this windows contents
*/
public void paint(Graphics g) {
// Fill in the background white, just in case it isn't already
g.setColor(Color.white);
g.fillRect(0,0,1000,1000);
// move the graphics drawing to 50,50 somewhere that we can see
// it
g.translate(50,50);
// cycle through the grid cells drawing the hexagons
for (int y=0;y<HEIGHT;y++) {
for (int x=0;x<WIDTH;x++) {
int offset = 0;
// every odd row must be offset a bit
// to make the tesalate
if (y % 2 != 0) {
offset = (int) (SIZE*2);
}
drawHex(g,(x*(SIZE*4))+offset,y*(SIZE));
}
}
}
/**
* Draws an individual hexgaon on the graphics context supplied,
* centered on the specified coordinates.
*
* @param g The graphics context on which to draw the hexagon
* @param xp The center x position
* @param yp The center y position
*/
private void drawHex(Graphics g,int xp,int yp) {
// set the colour to black for drawing the hexagons
g.setColor(Color.black);
// draw each line of the hexagon
g.drawLine((int) (xp-(SIZE*1.5)),(int) yp,(int) (xp-(SIZE*0.5)),(int) (yp-(SIZE)));
g.drawLine((int) (xp-(SIZE*1.5)),(int) yp,(int) (xp-(SIZE*0.5)),(int) (yp+(SIZE)));
g.drawLine((int) (xp+(SIZE*1.5)),(int) yp,(int) (xp+(SIZE*0.5)),(int) (yp-(SIZE)));
g.drawLine((int) (xp+(SIZE*1.5)),(int) yp,(int) (xp+(SIZE*0.5)),(int) (yp+(SIZE)));
g.drawLine((int) (xp-(SIZE*0.5)),(int) yp-(SIZE),(int) (xp+(SIZE*0.5)),(int) yp-(SIZE));
g.drawLine((int) (xp-(SIZE*0.5)),(int) yp+(SIZE),(int) (xp+(SIZE*0.5)),(int) yp+(SIZE));
}
/**
* Entry point into the program, just creates a new display
*
* @param argv The arguments passed into the program
*/
public static void main(String[] args) {
new Puzzle();
}
}
Is it possible? How can I do it?