Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  MouseMotionListener with tiles  (Read 1247 times)
0 Members and 1 Guest are viewing this topic.
Offline lordberzerker97

Senior Newbie





« Posted 2011-12-22 06:01:55 »

 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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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.
Offline philfrei
« Reply #1 - Posted 2011-12-22 08:19:29 »

Quote
Please give some suggestions or code.

Suggestion: you could try using a debugger.
Code: put these in your program a key spots:

Quote
System.out.println("your variable name = " + yourVariable);

Sorry, I couldn't resist. Shocked

"It's after the end of the world! Don't you know that yet?"
Offline ReBirth
« Reply #2 - Posted 2011-12-22 10:55:41 »

Your code is soooo many Cheesy my suggestion:

- get ready a rectangle on your tile size (for yellow highlight)
- put global variable x,y or Point class to store pointer's current location
- on rendering, draw your rectangle based to that point

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline counterp

Senior Devvie


Medals: 11



« Reply #3 - Posted 2011-12-22 13:40:21 »

You're doing

1  
... = new Rectangle(x, y); // You pass x and y as arguments


Which makes a new rectangle object with a WIDTH of x and a HEIGHT of y, it does not set the rectangle's x coordinate to x and y coordinate to y.

What you want is

1  
... = new Rectangle(x, y, width, height);
Offline lordberzerker97

Senior Newbie





« Reply #4 - Posted 2011-12-22 16:39:17 »

 Um I don't think you guys understand the problem with my program. You see the problem is that only the first tile will highlight not the others for some reason. It also lags a little so the responding is slow. So can you guys help me please?... Cry Cry Cry
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« Reply #5 - Posted 2011-12-22 17:27:16 »

I got 'java.lang.NoClassDefFoundError:' loads, no idea why, had to re-arrange a lot to sort that. Got it working now, and I see what you mean, it's very slow and only the first tile, attempting to sort it now.

edit: I think I know why it's taking so long. The paint method for Maps class, you're creating new rectangles every time you paint. Why not create them beforehand, and just draw them? I put a system.currentTimeMillis() at the beginning, got the time again at the end and subtracted to see how long it takes, and this paint method alone takes almost 700ms! (0.7 seconds).

edit2: as for why it only works for the first triangle, from GameCanvas we get this

1  
2  
3  
4  
5  
6  
hx = e.getX();
hy = e.getY();

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){

The last line being the bit where if its true, it'll draw the yellow box you want. I added some System.out.println() as Philfrei suggested, and look

hx: 204, hy: 223
x: 0, y: 0

the x and y of
1  
rrect[i].x and rrect[i].y
, which you took from
1  
rrect[i] = new Rectangle(m.rect[i].x,m.rect[i].y);
is 0.

This is what counterp was saying. If you create a rectangle like you did, with merely two arguments
1  
Rectangle(m.rect[i].x,m.rect[i].y);
-> http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Rectangle.html

"Rectangle(int width, int height)
          Constructs a new Rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are specified by the arguments of the same name."


Therefore, the rectangle you are creating always has a x and y position of 0,0.  Hence why only the first square works.

Also, you've got a few more problems. The paint method in the Maps class where it actually creates all the rectangles you're using to check, is creating them wrong. I made it print out all the x and y of rectangles created by

1  
rect[r] = new Rectangle(x,y,50,50);


and they all had an x and y of 9. So even when you fix the problem of using a Rectangle constructor with only 2 arguments, you'll need to have a look at this too.

I think.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #6 - Posted 2011-12-22 18:06:31 »

Go back and look at counterp's post as well.  As he mentions, in one place in your code you're creating a Rectangle instance, passing x- and y-values to the constructor when it expects width and height as arguments instead.
Offline lordberzerker97

Senior Newbie





« Reply #7 - Posted 2011-12-22 19:12:12 »

 Ok so i fixed the 2nd problem but not the 3rd(Rectangles x and y equalling 9) or 1st(the lag problem).

Can you show me the source code?
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« Reply #8 - Posted 2011-12-22 19:35:35 »

I have no code. Delete that entire paint method for maps. Create a new method or change the constructor so they the rectangles are created elsewhere, and not every time you paint. Then re-do the paint method to only draw what's appropriate. You might be better off storing the maps rect array in a multidimensional rect[][], so then you have columns and rows, up to you.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-12-22 20:59:14 »

Uhhh is anyone not noticing the 5 nested for loops in Maps.paint?
@OP, the reason it takes 700 milliseconds to run that method is because your loops run a total of 50 * 50 * 10 * 100 times = 2,500,000 times!! Sounds like overkill to me. As JESTERRRRRR suggested, try completely reworking that method.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lordberzerker97

Senior Newbie





« Reply #10 - Posted 2011-12-22 22:36:44 »

Yea I fixed the lag problem Cheesy  Grin Grin Grin Grin Grin Grin Grin
Offline lordberzerker97

Senior Newbie





« Reply #11 - Posted 2011-12-22 23:08:40 »

 Ok so I re-did all the classes that basically needed to be fixed. After that I found out that the x and y coordinates of the Rectangles and Objects: Grass and Rock Were not working correctly. The problem with the highlighting is still and issue. Although the lag problem has been fixed. So basically that's the only problem.

 So do you guys know any kind of loop that is used to fill the whole screen?(I prefer a 'for' loop.)
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-12-23 00:08:45 »

What do you mean "to fill the whole screen"? Could you be a little more specific?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (55 views)
2014-11-13 12:34:49

digdugdiggy (34 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (49 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!