0 Members and 3 Guests are viewing this topic.
It just draws a grid on a single JPanel.
Quote from: graphmastur on March 12, 2011, 09:23:37 pmIt just draws a grid on a single JPanel.Yes, what I need to do is much harder, I have to draw 64 panels :S
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); }}
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); }}