My work continues, the problems do as well. This time, it is more of an “is this a silly way to go about it” question.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
class DrawPanel extends JPanel
{
private boolean isVisible = false;
private boolean tileShow = false;
Image grid, image2, dirt, grass, brick;
private ArrayList<Point> dirtPoints = new ArrayList<Point>();
private ArrayList<Point> testPoints = new ArrayList<Point>();
private ArrayList<Point> grassPoints = new ArrayList<Point>();
private ArrayList<Point> brickPoints = new ArrayList<Point>();
private int tileSelect;
private BufferedImage loadImage;
public DrawPanel()
{
grid = new ImageIcon("GridTile.png").getImage();
image2 = new ImageIcon("TestTile.gif").getImage();
dirt = new ImageIcon("dirt.png").getImage();
grass = new ImageIcon("grass.png").getImage();
brick = new ImageIcon("brick.jpg").getImage();
try{
loadImage = ImageIO.read(getClass().getResource("Zelda_Chipset.png"));
} catch(IOException e){
System.out.println("Image not found");
}
}
public void mouseClicked(int mouseX, int mouseY, boolean remove)
{
//Gets index values
int inX = (int)Math.floor(mouseX / grid.getWidth(this));
int inY = (int)Math.floor(mouseY / grid.getHeight(this));
//Adds/Removes location points for adding tiles to map editor
switch(tileSelect)
{
case 0:
if(remove)
testPoints.remove(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
else
testPoints.add(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
break;
case 1:
if(remove)
dirtPoints.remove(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
else
dirtPoints.add(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
break;
case 2:
if(remove)
grassPoints.remove(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
else
grassPoints.add(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
break;
case 3:
if(remove)
brickPoints.remove(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
else
brickPoints.add(new Point(inX * grid.getWidth(this), inY * grid.getHeight(this)));
break;
}
}
public void paintComponent(Graphics g)
{
//Displays grid to map editor panel
if(isVisible)
{
for(int y = 0; y < this.getWidth(); y++)
{
for(int x = 0; x < this.getHeight(); x++)
{
g.drawImage(grid, x * grid.getWidth(this), y * grid.getHeight(this), this);
}
}
}
tileDrawer(g);
//Displays chipset to tile selector panel
if(tileShow)
{
g.drawImage(loadImage, 0, 32, this);
}
}
public void tileDrawer(Graphics g)
{
//Draws image at each saved position
for(Point dirtPoint : dirtPoints)
{
g.drawImage(dirt, (int)dirtPoint.getX(), (int)dirtPoint.getY(), this);
}
for(Point testPoint : testPoints)
{
g.drawImage(image2, (int)testPoint.getX(), (int)testPoint.getY(), this);
}
for(Point grassPoint : grassPoints)
{
g.drawImage(grass, (int)grassPoint.getX(), (int)grassPoint.getY(), this);
}
for(Point brickPoint : brickPoints)
{
g.drawImage(brick, (int)brickPoint.getX(), (int)brickPoint.getY(), this);
}
}
public boolean getBool()
{
return isVisible;
}
void setBool(boolean isVisible)
{
this.isVisible = isVisible;
}
public int getTileSelect()
{
return tileSelect;
}
void setTileSelect(int tileSelect)
{
this.tileSelect = tileSelect;
}
public boolean getTileShow()
{
return tileShow;
}
void setTileShow(boolean tileShow)
{
this.tileShow = tileShow;
}
}
public class FrameWork
{
JFrame frame1;
JFrame frame2;
JButton panelButton;
JButton gridButton;
JButton removeButton;
DrawPanel panel1;
DrawPanel panel2;
public static void main(String[] args)
{
FrameWork gui = new FrameWork();
gui.go();
}
public void go()
{
//initialize frames and panels
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);
//initialize buttons
panelButton = new JButton("Open Tile Selector");
gridButton = new JButton("Place Grid");
removeButton = new JButton("Remove Grid");
//set action events to buttons
panelButton.addActionListener(new PanelListener());
gridButton.addActionListener(new GridListener());
removeButton.addActionListener(new RemoveListener());
panel1.addMouseListener(new MouseClick());
frame2.addKeyListener(new KeyClick());
//add buttons to panel2
panel2.add(gridButton);
gridButton.setFocusable(false);
panel2.add(removeButton);
removeButton.setFocusable(false);
//add panels to frames
frame1.getContentPane().add(panel1);
frame2.getContentPane().add(panel2);
//add buttons to frames
frame1.getContentPane().add(BorderLayout.SOUTH, panelButton);
//frame setup
frame1.setSize(525, 525);
frame2.setSize(496, 327);
frame1.setVisible(true);
frame2.setFocusable(true);
}
//opens tile selector
class PanelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
frame2.setVisible(true);
panel2.setTileShow(true);
}
}
//turns grid on from tile selector panel
class GridListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
panel1.setBool(true);
frame1.repaint();
}
}
//Removes Grid
class RemoveListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
panel1.setBool(false);
frame1.repaint();
}
}
//Get mouse info
class MouseClick extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
if(event.getButton() == MouseEvent.BUTTON3)
{
panel1.mouseClicked(event.getX(), event.getY(), true);
}
else
{
panel1.mouseClicked(event.getX(), event.getY(), false);
}
frame1.repaint();
}
}
//Get keyboard info
class KeyClick extends KeyAdapter
{
public void keyPressed(KeyEvent event)
{
int key = event.getKeyCode();
switch(key)
{
case KeyEvent.VK_0:
panel1.setTileSelect(0);
frame1.repaint();
break;
case KeyEvent.VK_1:
panel1.setTileSelect(1);
break;
case KeyEvent.VK_2:
panel1.setTileSelect(2);
break;
case KeyEvent.VK_3:
panel1.setTileSelect(3);
break;
}
}
}
}
My editor. Worked great before, when I only had a few tiles. Create locations for each type of tile and add them to the screen. Recently added a chipset, and looking over it, realized I would have to create a point location for each tile(something like 400+ for a 480x256 chipset with 16x16 tiles) based on my current method of doing things. I had originally just created general location points and added images to them, but could not figure out how to switch between different images for each position. If I changed the image selector, all the currently drawn tiles changed as well. I understood why, just not how to fix it apart from doing different position points for each image.
Is there a more efficient method of doing this?