Author Topic: [Java]Multiple Functions, Static Functions, Declaring Functions as Objects  (Read 13920 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
@nemo: Does that code make 64 panels or just draw a grid using graphics?

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
It just draws a grid on a single JPanel.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
It just draws a grid on a single JPanel.

Yes, what I need to do is much harder, I have to draw 64 panels :S

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
It just draws a grid on a single JPanel.

Yes, what I need to do is much harder, I have to draw 64 panels :S
In which case, I could give you code, but because of the JPanel having a border, it won't be 640x640.  What exactly are you trying to do?

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
It just draws a grid on a single JPanel.

Yes, what I need to do is much harder, I have to draw 64 panels :S

modified. that code gives each panel a random color too.


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
nemo, I get the code and it looks awesome, but nothing happens when I run it, did you try it? I tried it:

MainClass.java
Code: [Select]
import java.awt.*;
import javax.swing.*;
import java.util.Random;
 
public class MainClass extends JFrame{
 
        private NPanel[][] panels = new NPanel[8][8];
 
        public MainClass(String s){
                super(s);
                setSize(8 * 64 + 8, 8 * 64 + 34);
                setDefaultCloseOperation(3);
                Random rand = new Random();
                GridLayout layout = new GridLayout(8, 8);
                getContentPane().setLayout(layout);
                for(int row = 0; row < 8; row++)
                        for(int col = 0; col < 8; col++){
                                panels[row][col] = new NPanel(
                        new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
                        getContentPane().add(panels[row][col]);
                        }
               
        }
        public static void main(String[] args){
                new MainClass("Test");
        }
}
class NPanel extends JPanel{
        public NPanel(Color c){
                setBackground(c);
        }
        public void paintComponent(Graphics g){
                super.paintComponent(g);
        }
}

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Change it to this and run it:
Code: [Select]
import java.awt.*;
import javax.swing.*;
import java.util.Random;
 
public class MainClass extends JFrame{
 
        private NPanel[][] panels = new NPanel[8][8];
 
        public MainClass(String s){
                super(s);
                setSize(8 * 64 + 8, 8 * 64 + 34);
                setDefaultCloseOperation(3);
                Random rand = new Random();
                GridLayout layout = new GridLayout(8, 8);
                getContentPane().setLayout(layout);
                for(int row = 0; row < 8; row++)
                        for(int col = 0; col < 8; col++){
                                panels[row][col] = new NPanel(
                        new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
                        getContentPane().add(panels[row][col]);
                        }
                this.setVisible(true);
        }
        public static void main(String[] args){
                new MainClass("Test");
        }
}
class NPanel extends JPanel{
        public NPanel(Color c){
                setBackground(c);
        }
        public void paintComponent(Graphics g){
                super.paintComponent(g);
        }
}

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Thanks much both of you.

Now all I have left to do (and sorry for your time) is to add a function to each panel (a mouse click event). No I'm not asking you to do it for me, just to tell me how to make an event for the first panel, and I'll do the same for the others.

Thanks in advance

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
I won't even tell you that.  I will tell you to look at the javadoc for JPanel, which is a subclass of Component, so look at that javadoc here.  I would also look at the addMouseListener method which takes a MouseListener object.  The javadoc of which can be found here

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
oops. i forgot setVisible(true).

yeah, you'll either want to make NPanel just implement MouseListener and all the methods in that interface, or make a new class which extends MouseAdapter and just overrides mouseClicked(MouseEvent e){/*your code on mouse click*/};
also, a hint, MouseEvent's have a method getX() and getY() to tell where the user clicked the mouse within a component (e.g. JPanel)