Results 1 to 4 of 4
  1. #1
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Problem using "contains"

    EDIT: The issue has now been resolved. Drawing things in different order seems to have worked.

    I've been following a tutorial on YouTube and right now, I'm trying to make a button light up a button when the mouse hovers over it by placing a transparent rectangle over the button. However, in the Store class, when I try to check if a button contains the point Screen.mse, the "contains" method always returns false. Using System.out.println, I've checked the position of point Screen.mse upon a mouse click, and that seems to return the mouse pointer's correct location, so it seems that I'm somehow using "contain" in the wrong way (even though it seems to be almost identical to the YouTube tutorial). Below I've posted the code where I think the problem lies. Thanks for the help in advance. Hopefully I will have figured it out before I get an answer.

    EDIT: Now contains all code from my project. My res folder also contains the correct image files. Displaying images on the screen has proven to work just fine.

    Here are the images in the res folder. Sorry about the large heights of the images.
    Simple "contains" problem-tileset_ground.png
    Simple "contains" problem-tileset_air.png
    Simple "contains" problem-cell.png

    Frame class:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Frame extends JFrame{
        public static String title = "Tower Defense";
        public static Dimension size = new Dimension(700,500);
        
        public Frame(){
            setTitle(title);
            setSize(size);
            setResizable(false);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            init();
        }
        
        public void init(){
            setLayout(new GridLayout(1,1,0,0));
            Screen screen = new Screen(this);
            add(screen);
            setVisible(true);
        }
        
        public static void main(String args[]){
            Frame frame = new Frame();
        }
    }
    Screen class:

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    
    public class Screen extends JPanel implements Runnable {
        
        public Thread thread = new Thread(this);
        
        public static Image[] tileset_ground = new Image[100];
        public static Image[] tileset_air = new Image[100];
        public static Image[] tileset_res = new Image[100];
        
        public static boolean isFirst = true;
        
        public static Point mse = new Point(0,0);
        
        public static Room room;
        public static Save save;
        public static Store store;
        
        public static int myWidth, myHeight;
        
        public Screen(Frame frame) {
            frame.addMouseListener(new KeyHandle());
            frame.addMouseMotionListener(new KeyHandle());
            
            thread.start();
        }
        
        public void define(){
            room = new Room();
            save = new Save();
            store = new Store();
            
            for (int i = 0;i<tileset_ground.length;i++) {
                tileset_ground[i] = new ImageIcon("res/tileset_ground.png").getImage();
                tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(),new CropImageFilter(0,24*i,24,24)));
                tileset_air[i] = new ImageIcon("res/tileset_air.png").getImage();
                tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(),new CropImageFilter(0,24*i,24,24)));
            }
            
            tileset_res[0] = new ImageIcon("res/cell.png").getImage();
            
            save.loadSave(new File("save/mission1.misn"));
        }
        
        public void paintComponent(Graphics g) {
            if (isFirst){
                myWidth = getWidth();
                myHeight = getHeight();
                define();
                isFirst = false;
            }
            
            g.setColor(new Color(10,20,20));
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(new Color(30,30,30));
            g.clearRect(room.block[0][0].x-1, 0, room.blockSize*room.worldWidth+2, room.blockSize*room.worldHeight+1);
            
            room.draw(g);
            store.draw(g);
        }
        public void run(){
            while (true) { 
                if (!isFirst){
                    room.physic();
                }
                repaint();
                try{
                    Thread.sleep(100000);
                }catch(Exception e){}
            }
        }
    
    }
    Room class:

    Java Code:
    import java.awt.*;
    
    public class Room{
        
        public int worldWidth = 12;
        public int worldHeight = 9;
        public int blockSize = 48;
        
        public Block[][] block;
        
        public Room(){
            define();
        }
        
        public void define(){
            block = new Block[worldHeight][worldWidth];
            
            for(int y=0;y<block.length;y++){
                for(int x=0;x<block[0].length;x++){
                    block [y][x] = new Block((Screen.myWidth/2) - ((worldWidth*blockSize)/2) + (x*blockSize),y*blockSize,blockSize,blockSize,Value.groundGrass,Value.airAir);
                }
            }
        }
        
        public void draw(Graphics g){
            for(int y=0;y<block.length;y++){
                for(int x=0;x<block[0].length;x++){
                    block[y][x].draw(g);
                }
            }
        }
        
        public void physic(){
            
        }
    }
    Block class:

    Java Code:
    import java.awt.*;
    
    public class Block extends Rectangle{
    
        public int groundID;
        public int airID;
        
        public Block(int x,int y,int width,int height,int groundID,int airID){
            setBounds(x,y,width,height);
            this.groundID = groundID;
            this.airID = airID;
        }
        
        public void draw(Graphics g){
            g.drawImage(Screen.tileset_ground[groundID],x,y,width,height,null);
            
            if(airID != Value.airAir){
                g.drawImage(Screen.tileset_air[airID],x,y,width,height,null);
            }
        }
        
    }
    Value class:

    Java Code:
    public class Value {
        public static int groundGrass = 0;
        
        public static int airAir = -1;
    }
    Save class:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Save {
        
        public void loadSave(File loadPath){
            try {
                Scanner loadScanner = new Scanner(loadPath);
                
                while(loadScanner.hasNext()){
                    for(int y=0;y<Screen.room.block.length;y++){
                        for(int x=0;x<Screen.room.block[0].length;x++){
                            Screen.room.block[y][x].groundID = loadScanner.nextInt();
                        }
                    }
                    for(int y=0;y<Screen.room.block.length;y++){
                        for(int x=0;x<Screen.room.block[0].length;x++){
                            Screen.room.block[y][x].airID = loadScanner.nextInt();
                        }
                    }
                }
                loadScanner.close();
            } catch(Exception e) { }
        }
    }
    mission1.misn file:

    Java Code:
    2 2 2 2 2 2 2 2 2 2 2 2
    0 0 0 0 0 0 0 0 0 0 0 0
    1 1 1 1 0 0 1 1 1 1 0 0
    0 0 0 1 0 0 1 0 0 1 0 0
    0 0 0 1 0 1 1 0 0 1 0 0
    0 0 0 1 1 1 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 1 1 1
    0 0 0 0 0 0 0 0 0 0 0 0
    2 2 2 2 2 2 2 2 2 2 2 2
    
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    Store class:

    Java Code:
    import java.awt.*;
    
    public class Store {
        public static int shopWidth = 8;
        public static int buttonSize = 40;
        public static int cellSpace = 6;
        public static boolean debugContains = false;
        
        public Rectangle[] button = new Rectangle[shopWidth];
        
        public Store() {
            define();
        }
        
        public void define() {
            for(int i=0;i<button.length;i++){
                button[i] = new Rectangle
                        ((Screen.myWidth/2) - ((shopWidth*(buttonSize+cellSpace))/2) + ((buttonSize+cellSpace)*i),
                        (Screen.room.blockSize*Screen.room.worldHeight)-buttonSize-(Screen.room.blockSize-buttonSize)/2,
                        buttonSize,
                        buttonSize);
                System.out.println("\nblock "+i+":");
                System.out.println(button[i].x);
                System.out.println(button[i].y);
            }
        }
        
        public void draw(Graphics g) {
            for(int i=0;i<button.length;i++){
                if(button[i].contains(Screen.mse)){
                    debugContains = true;
                    g.setColor(new Color(255,255,255,150));
                    g.fillRect(button[i].x,button[i].y,button[i].width,button[i].height);
                }else{
                    debugContains = false;
                }
                g.drawImage(Screen.tileset_res[0],button[i].x, button[i].y, button[i].width, button[i].height, null);
            }
        }
    }
    KeyHandle class:

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    
    public class KeyHandle implements MouseMotionListener, MouseListener {
    
        public void mouseClicked(MouseEvent e) {
            
        }
    
        public void mouseEntered(MouseEvent e) {
            
        }
    
        public void mouseExited(MouseEvent e) {
            
        }
    
        public void mousePressed(MouseEvent e) {
            System.out.println("\n"+Screen.mse.x+", "+Screen.mse.y);
            System.out.println("Mouseover button: "+Store.debugContains);
        }
    
        public void mouseReleased(MouseEvent e) {
            
        }
    
        public void mouseDragged(MouseEvent e) {
            Screen.mse = new Point((e.getX()) - ((Frame.size.width - Screen.myWidth)/2),(e.getY()) - ((Frame.size.height - (Screen.myHeight)) - (Frame.size.width - Screen.myWidth)/2));
        }
    
        public void mouseMoved(MouseEvent e) {
            Screen.mse = new Point((e.getX()) - ((Frame.size.width - Screen.myWidth)/2),(e.getY()) - ((Frame.size.height - (Screen.myHeight)) - (Frame.size.width - Screen.myWidth)/2));
        }
    
    }
    Last edited by ETBunce; 09-18-2013 at 06:09 AM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Simple "contains" problem

    You didn't include a complete code sample, so it is impossible for us to compile and try your code. I suggest you break your large statements down into smaller parts and incrementally print each of the parts. I think you'll find you have numbers you didn't expect because of a logic error.

  3. #3
    ETBunce is offline Member
    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Default Re: Simple "contains" problem

    Quote Originally Posted by quad64bit View Post
    You didn't include a complete code sample, so it is impossible for us to compile and try your code. I suggest you break your large statements down into smaller parts and incrementally print each of the parts. I think you'll find you have numbers you didn't expect because of a logic error.
    I apologize, it's hard for me to tell what parts of my project people might need to see, since most of this code is from the tutorial and isn't even mine, not to mention I'm only starting out with this language. It also makes it tough because code in each class all ties together somehow.

    Back on topic, I'm about to further analyze my code to see what I can do. So far I know that the Rectangles are being created and sized correctly, since I've been able to print out the width, height, and coordinates of each button. Clicking with the mouse prints out the position of the Screen.mse Point, as well as the boolean variable debugContains, which should change to true if the last button is being hovered over, and false if that's not the case. With that tool, the coordinates shown when I click on the top-left of a button prints out just about the same position of that button, which means the Screen.mse Point is being set to the correct position.

    Now I realize this information will probably not be of much use, but it's helped me gather my thoughts on what I've already done. At the moment it seems that "contains" has some strange behavior that I need to identify then work around.

    I'm just going to go ahead and include all of the code I have in my above post. This will probably make it a long page, but I can remove some things later if I need to or if I get asked to make it easier to read.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Simple "contains" problem

    This code it from a youtube series by Ulixava.
    How to make a Java "Tower Defence" game? Part 1 of 3! - "Layouting" - YouTube
    C'mon man. Ask questions about your own code. Don't just copy someone else's.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 01:15 AM
  3. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 09:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •