Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  GUI pathfinder change location of start + end 'tiles'  (Read 320 times)
0 Members and 1 Guest are viewing this topic.
Offline Injustice

Junior Newbie





« Posted 2013-08-27 15:00:11 »

I'm making an A* pathfinding application with a GUI. I have:

  • an enum TileType

1  
START(Color.GREEN), WALL(Color.BLACK), BLANK(new Color(240, 240, 240)), END(Color.RED);


  • a Grid class

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  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
public class Grid extends JPanel {

private final Color lineColour = new Color(200, 200, 200);
private Color defaultColour;
private final int pixelsPerTile;
private final int rows;
private final int columns;
private TileType[][] tiles;

public Grid(int rows, int columns, int pixelsPerTile) {
    this.pixelsPerTile = pixelsPerTile;
    this.rows = rows;
    this.columns = columns;
    this.tiles = new TileType[rows][columns];
    this.setPreferredSize(new Dimension(rows * pixelsPerTile, columns * pixelsPerTile));
    setTile(rows / 2 - 5, columns / 2, TileType.START);
            setTile(rows / 2 + 5, columns / 2, TileType.END);

}

public void resetBoard() {
    for (int i = 0; i < tiles.length; i++) {
        for (int j = 0; j < tiles[0].length; j++) {
            if (getTile(i, j) != TileType.START && getTile(i, j) != TileType.END) {
                tiles[i][j] = null;
            }
        }
    }
}

public void removeTile(int x, int y) {
    tiles[x][y] = TileType.BLANK;
}

public TileType getTile(int x, int y) {
    return tiles[x][y];
}

public Point getTile(boolean start) {
    for (int x = 0; x < tiles.length; x++) {
        for (int y = 0; y < tiles[0].length; y++) {
            if (tiles[x][y] == (start ? TileType.START : TileType.END)) {
                return new Point(x, y);
            }
        }
    }
    return null;
}

public void setTile(int x, int y, TileType tile) {
    if (getTile(x, y) != TileType.START && getTile(x, y) != TileType.END) {
        tiles[x][y] = tile;
    }
}

@Override
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    for (int x = 0; x < columns; x++) {
        for (int y = 0; y < columns; y++) {
            if (getTile(x, y) != null) {
                g.setColor(getTile(x, y).getColour());
                g.fillRect(x * pixelsPerTile, y * pixelsPerTile, pixelsPerTile, pixelsPerTile);
            }
        }
    }
    // have the grid appear on top of blank tiles
    g.setColor(lineColour);
    for (int x = 0; x < columns; x++) {
        for (int y = 0; y < rows; y++) {
            g.drawLine(x * pixelsPerTile, 0, x * pixelsPerTile, getHeight());
            g.drawLine(0, y * pixelsPerTile, getWidth(), y * pixelsPerTile);
        }
    }
}

public int getPixelsPerTile() {
    return pixelsPerTile;
}

public int getRows() {
    return rows;
}

public int getColumns() {
    return columns;
}
}

  • a Frame class

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  
public class Frame extends JFrame {

private Grid grid;

public Frame() {
    grid = new Grid(33, 33, 15); // odd so there is a definitive middle point
    this.setAutoRequestFocus(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("A* Path finder");
    this.setBounds(new Rectangle((grid.getRows()) * grid.getPixelsPerTile(),
            (grid.getColumns() + 2) * grid.getPixelsPerTile()));
    this.setResizable(true);
    this.add(grid);
    this.setVisible(true);
    this.getContentPane().addMouseListener(new MouseLsntr()); // fixes alignment problems
    ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(5);
    se.scheduleAtFixedRate(new RedrawGrid(), 0L, 20L, TimeUnit.MILLISECONDS);
}

private class RedrawGrid implements Runnable {

    @Override
    public void run() {
        grid.repaint();
    }
}

private class MouseLsntr implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent me) {
        int x = me.getX() / grid.getPixelsPerTile();
        int y = me.getY() / grid.getPixelsPerTile();
        switch (me.getButton()) {
            case MouseEvent.BUTTON1:
                if (grid.getTile(x, y) != TileType.WALL) {
                    grid.setTile(x, y, TileType.WALL);
                    System.out.println(String.format("(%d, %d) (%d, %d) wall", x, y, x, y));
                } else {
                    grid.removeTile(x, y);
                    System.out.println(String.format("(%d, %d) (%d, %d) blank", x, y, x, y));
                }
                break;
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }        
}
}


  • and obviously a main class which calls Frame()

When the user left clicks on a grid, they make a WALL at that location. If it's already a WALL, it becomes BLANK. I want the user to be able to move the START and END tile by clicking on it and then dragging to the location they want to stop at, which I'm having trouble doing. Any help would be appreciated since I've been stumped about this for a while now :L
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-08-27 15:04:54 »

On mouse press calculate the tile {x1,y1}, and on mouse release calculate it again {x2, y2}. If it's different, set the start to {x1,y1} and the end to {x2,y2}.

If you're talking also about implementing A*, here's a fun tutorial:

http://wiki.gamegardens.com/Path_Finding_Tutorial

And here's a JS interactive webpage with A* and a few others:

http://qiao.github.io/PathFinding.js/visual/

Have fun!

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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (101 views)
2014-11-29 21:32:03

toopeicgaming1999 (160 views)
2014-11-26 15:22:04

toopeicgaming1999 (163 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!