Drawing on a JPanel

Ok, I have figured out how to draw a grid on a JPanel. Now, I want it so that when a person clicks on a square, it changes the color of that square.

But, all the tests that I have done to try to do this have failed. If someone can point out how to do this that would be great. Here is the code I have so far:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

import javax.swing.JPanel;

publicclass GraphicTestextends JFrame{

privateint DISPLAY_WIDTH = 600;

privateint DISPLAY_HEIGHT = 600;

privateint MAP_WIDTH = 900;

privateint MAP_HEIGHT = 900;

privatestaticfinalint MAP_SQUARE = 15;

private DisplayArea da =new DisplayArea(new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT));

privateint y = -1;

privateint x = -1;

publicstaticvoid main(String[] args){

new GraphicTest();

}

public GraphicTest(){

super("Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setResizable(true);

getContentPane().setLayout(null);

getContentPane().add(da);

setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);

setVisible(true);

}

publicclass DisplayAreaextends JPanel{

public DisplayArea(Rectangle bounds){

setLayout(null);

setBounds(bounds);

setOpaque(false);

addMouseListener(new MouseListener(){

publicvoid mouseClicked(MouseEvent e){

determineGridLocation(e.getX(), e.getY());

}

publicvoid mouseEntered(MouseEvent e){

}

publicvoid mouseExited(MouseEvent e){

}

publicvoid mousePressed(MouseEvent e){

}

publicvoid mouseReleased(MouseEvent e){

}

});

}

publicvoid paintComponent(Graphics g){

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.BLACK);

for (int x = 0; x < getWidth(); x += MAP_SQUARE){

for (int y = 0; y < getHeight(); y += MAP_SQUARE){

g2d.drawLine(x, y, x, y + MAP_SQUARE);

g2d.drawLine(x, y, x + MAP_SQUARE, y);

}

}

}

//public void paint(Graphics g){

//Graphics2D g2d = (Graphics2D) g;

//g2d.setColor(Color.BLUE);

//

//if (x > -1 && y > -1){

//g2d.drawRect((x*MAP_SQUARE), (y*MAP_SQUARE), MAP_SQUARE, MAP_SQUARE);

//}

//}

}

privatevoid determineGridLocation(int x,int y){

System.out.println("X: " + x +" Y: " + y);

float xf =new Float(x).floatValue() /new Float(MAP_SQUARE).floatValue();

float yf =new Float(y).floatValue() /new Float(MAP_SQUARE).floatValue();

int xi =new Float(xf).intValue();

int yi =new Float(yf).intValue();

x = xi;

y = yi;

da.repaint();

this.getContentPane().repaint();

System.out.println("Location: " + xi +"," + yi);

}

}

[6711 byte] By [BobDoleLovesYoua] at [2007-10-1 10:29:59]
# 1

I wouldn't claim to be an expert on this, but in general you need to put all the drawing inside the paintComponent() method. If you put that commented-out code into the end of paintComponent, then I suspect your blue square will appear. Something like this maybe?public void paintComponent(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.BLACK);

for (int x = 0; x < getWidth(); x += MAP_SQUARE) {

for (int y = 0; y < getHeight(); y += MAP_SQUARE) {

g2d.drawLine(x, y, x, y + MAP_SQUARE);

g2d.drawLine(x, y, x + MAP_SQUARE, y);

}

}

g2d.setColor(Color.BLUE);

if (x > -1 && y > -1){

g2d.drawRect((x*MAP_SQUARE), (y*MAP_SQUARE), MAP_SQUARE, MAP_SQUARE);

}

Might be a good idea to rename x and y to something different, to reduce confusion with the x and y in the for loops.

Regards,

Tim

TimRyanNZa at 2007-7-10 2:57:25 > top of Java-index,Other Topics,Java Game Development...
# 2

You could also make your grid drawing code faster by doing it like this:for (int x = 0; x < getWidth(); x += MAP_SQUARE) {

g2d.drawLine(x, 0, x, getHeight());

}

for (int y = 0; y < getHeight(); y += MAP_SQUARE) {

g2d.drawLine(0, y, getWidth(), y);

}

Regards,

Tim

TimRyanNZa at 2007-7-10 2:57:25 > top of Java-index,Other Topics,Java Game Development...
# 3

TIm,

THanks for the help. Unfortunately the blue square still doesn't show up. I think the problem is that the component isn't redrawing itself. I have tried repaint() revalidate() and validate() to try to do this.

But, thanks for your help with the drawing of the grid. When I finished the code for it I realised I was drawing a bunch of little lines and needed to improve that. You did that for me. ;) Thanks.

Bob Dole

BobDoleLovesYoua at 2007-7-10 2:57:25 > top of Java-index,Other Topics,Java Game Development...
# 4

Just figured it out. It was an issue of calling repaint() in the wrong spot. Here is the code for it to work. It's bloated, but that is easily fixed.

Tim, Thanks for the help. You got me thinking and that did the job.

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class GraphicTest extends JFrame {

private int DISPLAY_WIDTH = 600;

private int DISPLAY_HEIGHT = 600;

private int MAP_WIDTH = 900;

private int MAP_HEIGHT = 900;

private static final int MAP_SQUARE = 15;

private DisplayArea da = new DisplayArea(new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT));

private int y = -1;

private int x = -1;

public static void main(String[] args) {

new GraphicTest();

}

public GraphicTest() {

super("Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setResizable(true);

getContentPane().setLayout(null);

getContentPane().add(da);

setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);

setVisible(true);

}

public class DisplayArea extends JPanel {

public DisplayArea(Rectangle bounds) {

setLayout(null);

setBounds(bounds);

setOpaque(false);

addMouseListener(new MouseListener() {

public void mouseClicked(MouseEvent e) {

x = e.getX();

y = e.getY();

System.out.println("X: " + x + " Y: " + y);

float xf = new Float(x).floatValue() / new Float(MAP_SQUARE).floatValue();

float yf = new Float(y).floatValue() / new Float(MAP_SQUARE).floatValue();

int xi = new Float(xf).intValue();

int yi = new Float(yf).intValue();

x = xi;

y = yi;

System.out.println("Location: " + xi + "," + yi);

repaint();

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

});

}

public void paintComponent(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.BLACK);

for (int x = 0; x < getWidth(); x += MAP_SQUARE) {

g2d.drawLine(x, 0, x, getHeight());

}

for (int y = 0; y < getHeight(); y += MAP_SQUARE) {

g2d.drawLine(0, y, getWidth(), y);

}

g2d.setColor(Color.BLUE);

if (x > -1 && y > -1) {

System.out.println("Here we are: " + x + "" + y);

g2d.drawRect((x * MAP_SQUARE), (y * MAP_SQUARE), MAP_SQUARE, MAP_SQUARE);

g2d.fillRect((x * MAP_SQUARE), (y * MAP_SQUARE), MAP_SQUARE, MAP_SQUARE);

x = -1;

y = -1;

}

}

}

}

BobDoleLovesYoua at 2007-7-10 2:57:25 > top of Java-index,Other Topics,Java Game Development...