Super impose image under Cell grid.

I am having difficulty putting an image under a cell grid. Each panel seems to be overriding one other. I have tried using setOpaque each panel separately but it does not work.

Here are two classes Grid and GridPanel , they were posted previously in the forum.

Can someone please help.

publicclass Grid

{

public Grid()

{

GridPanel gridPanel =new GridPanel();

JPanel imagePanel =new JPanel();

imagePanel.setOpaque(false);

ImageIcon icon1 =new ImageIcon ("Worldmap.jpg");

JLabel test =new JLabel(icon1);

test.setOpaque(false);

imagePanel.add(test);

CellSelector cellSelector =new CellSelector(gridPanel);

gridPanel.addMouseListener(cellSelector);

JFrame f =new JFrame("");

Container cont = f.getContentPane();

cont.add(gridPanel);

cont.add(imagePanel);

f.setSize(950,590);

f.setLocation(50,50);

f.setVisible(true);

cont.setBackground(Color.gray);

}

class GridPanelextends JPanel

{

double xInc, yInc;

finalint

GRID_SIZE = 200,

DRAW= 0,

FILL= 1,

PAD= 10;

int[][] cells;

public GridPanel()

{

initCells();

}

protectedvoid paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

double w = getWidth();

double h = getHeight();

xInc = (w - 2*PAD)/GRID_SIZE;

yInc = (h - 2*PAD)/GRID_SIZE;

// row lines

double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x2, y1));

y1 += yInc;

}

// col lines

y1 = PAD;

for(int j = 0; j <= GRID_SIZE; j++)

{

g2.draw(new Line2D.Double(x1, y1, x1, y2));

x1 += xInc;

}

// fill cells

g2.setPaint(Color.red);

for(int row = 0; row < cells.length; row++)

{

for(int col = 0; col < cells[0].length; col++)

{

if(cells[row][col] == FILL)

{

x1 = PAD + col * xInc + 1;

y1 = PAD + row * yInc + 1;

g2.fill(new Rectangle2D.Double(x1, y1, xInc - 1, yInc - 1));

}

}

}

}

Message was edited by:

Linked

[4224 byte] By [Linkeda] at [2007-11-26 14:48:41]
# 1

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class SomeOptions {

private JScrollPane getContent(final BufferedImage image) {

// Our graphic components need this so their parent containers

// will have an idea of the space they need for proper display.

Dimension d = new Dimension(image.getWidth(), image.getHeight());

// Option: Draw the image below the grid in the graphic component.

GridPanel left = new GridPanel(image);

left.setPreferredSize(d);

// Option: Set the grid above the image mounted in a JLabel

// using an OverlayLayout.

JLabel label = new JLabel(new ImageIcon(image));

label.setPreferredSize(d);

GridPanel upperPanel = new GridPanel(null);

upperPanel.setOpaque(false);

JPanel center = new JPanel();

OverlayLayout overlay = new OverlayLayout(center);

center.setLayout(overlay);

center.add(upperPanel);

center.add(label);

// Option: Set the grid above the image in a graphic component.

// This could easily be an outer class. This approach is useful

// for lower-level work such as editing or animating the image.

JPanel imagePanel = new JPanel() {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int x = (getWidth() - image.getWidth())/2;

int y = (getHeight() - image.getHeight())/2;

g.drawImage(image, x, y, this);

}

};

imagePanel.setPreferredSize(d);

upperPanel = new GridPanel(null);

upperPanel.setOpaque(false);

JPanel right = new JPanel();

overlay = new OverlayLayout(right);

right.setLayout(overlay);

right.add(upperPanel);

right.add(imagePanel);

// Assemble components.

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(left);

panel.add(center);

panel.add(right);

return new JScrollPane(panel);

}

public static void main(String[] args) throws IOException {

BufferedImage image = ImageIO.read(new File("images/owls.jpg"));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new SomeOptions().getContent(image));

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

You may want to change the name of GridPanel to avoid name–clashing.

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class GridPanel extends JPanel {

BufferedImage image;

int[][] cells;

final int

GRID_SIZE = 20,

DRAW= 0,

FILL= 1,

PAD= 10;

public GridPanel(BufferedImage image) {

this.image = image;

cells = new int[GRID_SIZE][GRID_SIZE];

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

double w = getWidth();

double h = getHeight();

// If we have an image, draw it centered, under the grid.

if(image != null) {

int x = (int)(w - image.getWidth())/2;

int y = (int)(h - image.getHeight())/2;

g2.drawImage(image, x, y, this);

}

double xInc = (w - 2*PAD)/GRID_SIZE;

double yInc = (h - 2*PAD)/GRID_SIZE;

// row lines

double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;

for(int j = 0; j <= GRID_SIZE; j++) {

g2.draw(new Line2D.Double(x1, y1, x2, y1));

y1 += yInc;

}

// col lines

y1 = PAD;

for(int j = 0; j <= GRID_SIZE; j++) {

g2.draw(new Line2D.Double(x1, y1, x1, y2));

x1 += xInc;

}

// fill cells

g2.setPaint(Color.red);

for(int row = 0; row < cells.length; row++) {

for(int col = 0; col < cells[0].length; col++) {

if(cells[row][col] == FILL) {

x1 = PAD + col * xInc + 1;

y1 = PAD + row * yInc + 1;

g2.fill(new Rectangle2D.Double(x1, y1, xInc - 1, yInc - 1));

}

}

}

}

}

crwooda at 2007-7-8 8:36:31 > top of Java-index,Security,Cryptography...