Hello those of you who are checking out this thread. I’m currently having some repaint problems on my custom JPanel i made to create a 2d game world maker. Here is the flow of the program thus far:
1)New window appears with a blank JPaenl
2)Click generate and then select dimensions
3)Graphics2D draws rectangles to JPanel
4)Click on a box and it will turn it black
The major problem is, is that when I click on a block to make it black the JButton “generate” will create a copy of itself right under “File” in the JMenubar. Then if i resize the window in any way and try to click another square it will completely mess up the drawing of the squares until it gets resized again. here is my code:
package builderapplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BuilderApplet {
public static void main(String[] args) {
ProgFrame pf = new ProgFrame();
pf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pf.setVisible(true);
}
}
class ProgFrame extends JFrame implements ActionListener{
JMenuBar bar;
JMenu file;
JMenuItem save;
JMenuItem clear;
JButton generate;
BuildBoard board;
boolean canDraw;
int blocksWide, blocksHigh;
public ProgFrame(){
super("Terraria Builder Applet");
bar = new JMenuBar();
file = new JMenu("File");
save = new JMenuItem("Save");
clear = new JMenuItem("Clear");
generate = new JButton("Generate");
generate.addActionListener(this);
generate.setBorderPainted(false);
generate.setContentAreaFilled(false);
board = new BuildBoard();
board.setPreferredSize(new Dimension(800, 600));
this.setJMenuBar(bar);
bar.add(file);
file.add(save);
file.add(clear);
bar.add(generate);
add(board);
pack();
}
public void actionPerformed(ActionEvent e){
ConfigureWindow win = new ConfigureWindow();
if(e.getSource() == generate){
win.setLocation(getX()+(getWidth()/2), getY()+(getHeight()/2));
win.pack();
win.setVisible(true);
}
}
public class ConfigureWindow extends JFrame implements ActionListener{
JLabel lblBlockSize;
JLabel lblX;
SpinnerModel widthModel;
JSpinner spnBlockSizeWidth;
SpinnerModel heightModel;
JSpinner spnBlockSizeHeight;
JButton ok;
final int START = 1, MIN = 1, MAX = 800, INC = 1;
public ConfigureWindow(){
super("Configure");
setLayout(new GridLayout(1, 2));
JPanel panel = new JPanel();
lblBlockSize = new JLabel("Block Size");
widthModel = new SpinnerNumberModel(START, MIN, MAX, INC);
spnBlockSizeWidth = new JSpinner(widthModel);
lblX = new JLabel("x");
heightModel = new SpinnerNumberModel(START, MIN, MAX, INC);
spnBlockSizeHeight = new JSpinner(heightModel);
ok = new JButton("OK");
panel.setLayout(new FlowLayout());
add(panel);
panel.add(lblBlockSize);
panel.add(spnBlockSizeWidth);
panel.add(lblX);
panel.add(spnBlockSizeHeight);
panel.add(ok);
ok.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
canDraw = true;
blocksWide = (Integer)spnBlockSizeWidth.getValue();
blocksHigh = (Integer)spnBlockSizeHeight.getValue();
board.initDraw(blocksWide, blocksHigh);
this.setVisible(false);
}
}
}
Then this is the custom panel i created for the editor:
package builderapplet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class BuildBoard extends JPanel implements MouseListener{
boolean canDraw = false;
int width, height;
int blockWidth = 50, blockHeight = 50;
Rectangle[][] rects;
boolean[][] drawOrFill;
public BuildBoard(){
//setLayout(null);
this.setIgnoreRepaint(true); // Commented out of not there is still no effect on this
}
public void initDraw(int width, int height){
this.width = width;
this.height = height;
rects = new Rectangle[width][height];
drawOrFill = new boolean[width][height];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
rects[x][y] = new Rectangle(x*blockWidth, y*blockWidth, blockWidth, blockHeight);
drawOrFill[x][y] = true;
}
}
this.addMouseListener(this);
System.out.println("block height: " + blockWidth + "\n block height: " + blockHeight);
canDraw = true;
repaint();
}
@Override
public void paint(Graphics g){
System.out.println("Paint method called");
Graphics2D g2 = (Graphics2D)g;
g2.setColor(new Color(0, 0, 0, 1f));
if(canDraw){
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
if(drawOrFill[x][y] == true)
g2.drawRect(x*blockWidth, y*blockHeight, blockWidth, blockHeight);
else
g2.fillRect(x*blockWidth, y*blockHeight, blockWidth, blockHeight);
} // End of inner loop
} // End of outer loop
} // end of if statment
} // end of paint method
public void mouseClicked(MouseEvent e) {
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
if(rects[x][y].contains(e.getX(), e.getY())){
drawOrFill[x][y] = false;
}
}
}
repaint(); // When i comment this out it wont repaint when i click but it will not bug out when i resize the window
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public int getBlockWidth(){
return blockWidth;
}
public int getBlockHeight(){
return blockHeight;
}
} // end of class
pictures of it running:
Yes I KNOW it says terraria builder applet but as for right now i have it as a desktop app to test it out, i didn’t meant to name it applet my bad