So, I’ve been working on a crude tile map editor. Seems I have run into a snag, two actually. First one, involves the placement of the tile image. It only puts one tile on the screen, the moment I attempt to add another, the first is deleted. I know my draw code needs to be modified, bur for the life of me, can’t figure out how. Second, is placement of the tile. I have made several attempts to get the tile to fit inside the square in which the mouse is located. Unfortunately, I’ve deleted the attempted code because it was frustrating me. But what I essentially did, was create a Rectangle array and attempted to add to the array as I created the initial tiles. I then attempted to use a rectangle.contains(mouseX, mouseY) to place tile inside given rectangle. I would usually wind up with a java.awt.event error or the tile would be put in the bottom right corner square and none others(im guessing that was because I used for loops and just putting the rectangle inside the loop made the x,y values equal the last numbers of the loop). If I could get a pointer to the right direction, maybe certain specific things I should look up to have a better understanding of what i’m trying to do.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Rectangle.*;
class DrawPanel extends JPanel
{
private Boolean isVisible = false;
private Boolean isClicked = false;
private int mouseX, mouseY;
public void paintComponent(Graphics g)
{
Image image = new ImageIcon("GridTile.png").getImage();
Image image2 = new ImageIcon("TestTile.gif").getImage();
if(isVisible)
{
for(int y = 0; y < 15; y++)
{
for(int x = 0; x < 15; x++)
{
g.drawImage(image, x * image.getWidth(this), y * image.getHeight(this), this);
}
}
if(isClicked)
{
g.drawImage(image2, mouseX, mouseY, this);
}
}
}
public Boolean getClick()
{
return isClicked;
}
void setClick(Boolean isClicked)
{
this.isClicked = isClicked;
}
public Boolean getBool()
{
return isVisible;
}
void setBool(Boolean isVisible)
{
this.isVisible = isVisible;
}
public int getMouseX()
{
return mouseX;
}
void setMouseX(int mouseX)
{
this.mouseX = mouseX;
}
public int getMouseY()
{
return mouseY;
}
void setMouseY(int mouseY)
{
this.mouseY = mouseY;
}
}
public class FrameWork
{
JFrame frame1;
JFrame frame2;
JButton panelButton;
JButton imageButton;
JButton removeButton;
DrawPanel panel1;
DrawPanel panel2;
public int mouseX;
public int mouseY;
public static void main(String[] args)
{
FrameWork gui = new FrameWork();
gui.go();
}
public void go()
{
frame1 = new JFrame("Pew Pew");
frame2 = new JFrame();
panel1 = new DrawPanel();
panel2 = new DrawPanel();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panelButton = new JButton("Open other frame");
imageButton = new JButton("Click me");
removeButton = new JButton("Remove Pic");
panelButton.addActionListener(new PanelListener());
imageButton.addActionListener(new ImageListener());
removeButton.addActionListener(new RemoveListener());
panel1.addMouseListener(new MouseClick());
frame1.getContentPane().add(panel1);
frame2.getContentPane().add(panel2);
frame1.getContentPane().add(BorderLayout.SOUTH, panelButton);
frame2.getContentPane().add(BorderLayout.SOUTH, imageButton);
frame2.getContentPane().add(BorderLayout.NORTH, removeButton);
frame1.setSize(525, 525);
frame2.setSize(300, 300);
frame1.setVisible(true);
}
class PanelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
frame2.setVisible(true);
}
}
class ImageListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
panel1.setBool(true);
frame1.repaint();
}
}
class RemoveListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
panel1.setBool(false);
frame1.repaint();
}
}
class MouseClick extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
panel1.setClick(true);
panel1.setMouseX(event.getX());
panel1.setMouseY(event.getY());
frame1.repaint();
}
}
}