Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Big issue with repaint and my world builder app  (Read 1118 times)
0 Members and 1 Guest are viewing this topic.
Offline HighFlyCoder

Senior Newbie





« Posted 2011-06-17 03:51:23 »

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:

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  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
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:

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  
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
Offline gouessej
« Reply #1 - Posted 2011-06-17 09:58:13 »

Hi

I have done something similar in one of my free open source applications (JFPSM) without such problems. I click onto a block and its color is changed. I use an image in paintComponent to draw the map. I call repaint() inside my MouseAdapter. Shouldn't you override paintComponent() rather than paint()?

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-06-17 23:47:51 »

First of all, if you are doing custom painting, you shouldn't be extending JPanel, but instead extend JComponent.
Secondly, as gouessej said, you should override paintComponent(Graphics), not paint(Graphics) since in Swing, paint(Graphics) handles the double buffering and also calls other important methods.
Thirdly, calling setIgnoreRepaint(true) will cause all repaint() calls to be ignored. I think you already know this but just a heads up.
As for the "generate" button suddenly appearing at a place where it shouldn't, that's pretty weird. I'll try to run your program and replicate it.

EDIT: I do believe the problem might be using a second JFrame. Try using JDialog instead.

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.

CopyableCougar4 (13 views)
2014-12-28 02:10:29

BurntPizza (17 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

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

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

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

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

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

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

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