Hey all!! There seems to be a problem with my program. The problem is that when the mouse is over a tile it wont draw a yellow rectangle around it unless its the top, first tile.
Here is some code:
The window/JFrame.
package Holder;
import javax.swing.*;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
GameCanvas GC = new GameCanvas();
public Window(){
this.setTitle("Stellar Factions");
this.setSize(507,529);
this.add(GC);
this.setResizable(false);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
public static void main(String [] args){
new Window();
}
}
The gameCanvas/where I load all the images/UI.
package Holder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameCanvas extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
Maps m = new Maps();
private Image dbImage;
private Graphics dbGraphics;
private Rectangle[] rrect = new Rectangle[100];
int hx,hy,bx,by;
boolean clicked = false;
public GameCanvas(){
this.setFocusable(true);
this.requestFocus(true);
this.addMouseMotionListener(new MouseAdapter(){
public void mouseMoved(MouseEvent e){
hx = e.getX();
hy = e.getY();
for(int i = 0;i < 100;i++){
rrect[i] = new Rectangle(m.rect[i].x,m.rect[i].y);
if(hx > rrect[i].x && hx < rrect[i].x + 50 && hy > rrect[i].y && hy < rrect[i].y + 50){
clicked = true;
bx = rrect[i].x;
by = rrect[i].y;
break;
}else{clicked = false;}}}
public void mouseDragged(MouseEvent e){}
});
Thread t = new Thread(this);
t.start();
}
public void paint(Graphics g){
dbImage = createImage(getWidth(),getHeight());
dbGraphics = dbImage.getGraphics();
paintComponent(dbGraphics);
g.drawImage(dbImage,0,0,this);
}
public void paintComponent(Graphics g){
m.paint(g);
if(clicked){
g.setColor(Color.YELLOW);
g.drawRect(bx,by,50,50);
}
repaint();
}
public void run(){
while(true){
try{
Thread.sleep(20);
}catch(Exception e){
System.out.println("!!!ERROR!!!");
}}}
}
The tile map code
package Holder;
import java.awt.*;
public class Maps {
public final int arrayNum = 100;
Grass[] grass = new Grass[arrayNum/2];
Rocks[] rocks = new Rocks[arrayNum/2];
Rectangle[] rect = new Rectangle[arrayNum];
int[][] map = {{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,1,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,1,1,0,0,0,0},
{0,0,0,1,0,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}};
public void paint(Graphics g){
for(int i = 0;i < grass.length;i++){
for(int y = 0;y < map.length;y++){
for(int x = 0;x < map[y].length;x++){
for(int r = 0;r < 50;r++){
rect[r] = new Rectangle(x,y,50,50);
if(map[y][x] == 0){
grass[i] = new Grass(rect[r].x*50,rect[r].y*50);
grass[i].paint(g);
}}
for(int r = 50;r < 100;r++){
rect[r] = new Rectangle(x,y,50,50);
if(map[y][x] == 1){
rocks[i] = new Rocks(rect[r].x*50,rect[r].y*50);
rocks[i].paint(g);
}}
}}}
}
}
The rock object.
package Holder;
import java.awt.*;
public class Rocks {
int x,y;
public Rocks(int x,int y){
this.x = x;
this.y = y;
}
public void paint(Graphics g){
g.setColor(Color.GRAY);
g.fillRect(x,y,50,50);
g.setColor(Color.BLACK);
g.drawRect(x,y,50,50);
}
}
The Grass object.
package Holder;
import java.awt.*;
public class Grass {
int x,y;
public Grass(int x,int y){
this.x = x;
this.y = y;
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(x,y,50,50);
g.setColor(Color.BLACK);
g.drawRect(x,y,50,50);
}
}
Please give some suggestions or code.