Results 1 to 9 of 9
- 01-06-2012, 08:08 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
A square grid which changes colour depending on the array element value
Ok What I want to do is have a GUI, which is essentially a square grid with small squares (because grid will need to be quite big), which change from white to blue when an array element value is 1 and stay or go back to white when a 0 is read (this will be cycled through quite quickly as it is completely automatic). The code I have at the moment prints the (2d array) in the command line interface, with 1's and 0's so it just shows one screen at a time and just ends up looking a right mess really. I've tried doing the GUI myself but get absolutely nowhere and need HELP please!!!
- 01-06-2012, 09:41 PM #2
Re: A square grid which changes colour depending on the array element value
Ok, so what do you have?
- 01-06-2012, 10:14 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: A square grid which changes colour depending on the array element value
Well I have another program which has the square grid but responds to mouse clicks on certain parts of the grid and then changes colour of the small squares accordingly, but I don't know how to correctly merge them together so it works through checking the value of the array constantly, instead.
-
Re: A square grid which changes colour depending on the array element value
I'm confused since there was no mention of any mouse requirements in your first post, but there is in your second post. What exactly do you need, and where exactly are you stuck?
- 01-06-2012, 10:33 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: A square grid which changes colour depending on the array element value
The first post is what I want, the second was what I have in relation to that, Yes I do NOT need mouse clicks, I said I have a second (completely unrelated) program which makes a square grid BUT responds to mouse clicks, and have been completely unsuccessful in merging the 2 programs together. Which Is why I need help, either from scratch or by help merging the 2 programs I have together
Last edited by nh1402; 01-06-2012 at 10:36 PM.
- 01-06-2012, 10:38 PM #6
Re: A square grid which changes colour depending on the array element value
I still haven't seen any code.
- 01-06-2012, 10:40 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: A square grid which changes colour depending on the array element value
What code would you like me to show you?, what I have at the moment?, all of it?
-
Re: A square grid which changes colour depending on the array element value
You need to ask an answerable question, one which lays out a specific answerable question with enough code to support the question and demonstrate any problems. Right now all we're seeing is what you desire, not a fixable problem. Please read my links below on how to create an SSCCE and how to ask answerable questions.
- 01-06-2012, 11:15 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: A square grid which changes colour depending on the array element value
The code above is the second program I was talking about, (fully working) I want that to work by checking a different (from main program) 2d array and if it is a 1, then the corresponding 2d array location (both arrays will have the same size) in the Click program should show the square in that location.Java Code:import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.Color; import java.io.*; class ClickData { int[][] grid; ClickData(int x, int y) {grid = new int[y][x];} public int getX() {return grid[0].length; } public int getY() {return grid.length; } public int getCell(int x, int y) {return grid[y][x];} public void setCell(int x, int y, int v) {grid[y][x] = v;} } class Click { public static void main(String[] args) throws IOException, FileNotFoundException { int squareSize = 10, x = 30, y = 50; String FileName = "outFile.lif"; if (args.length <1) FileName = "outFile.lif"; if (args.length >= 2) x = Integer.parseInt (args[1]); if (args.length >= 3) y =Integer.parseInt (args[3]); if (args.length >= 4) FileName = args[4]; PrintWriter outFile = new PrintWriter("out.lif"); ClickData click = new ClickData(x, y); ClickFrame frame = new ClickFrame(); ClickComponent component = new ClickComponent(click, squareSize); JButton writeButton = new JButton("Write File"); writeButton.addActionListener(new WriteListener(click, frame, outFile)); JButton button = new JButton("Clear"); button.addActionListener(new ClearListener(click, frame)); frame.add(component, BorderLayout.WEST); frame.add(button, BorderLayout.SOUTH); frame.add(writeButton, BorderLayout.NORTH); frame.pack(); } } class ClearListener implements ActionListener { ClickFrame f; ClickData c; ClearListener(ClickData c, ClickFrame f) { this.c = c; this.f = f; } public void actionPerformed(ActionEvent e) { for (int y = 0; y < c.getY(); y++) for (int x = 0; x < c.getX(); x++) c.setCell(x, y, 0); f.repaint(); } } class ClickFrame extends JFrame { ClickFrame() { setTitle("Click me"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } class ClickComponent extends JComponent implements MouseInputListener { private ClickData c; private int squareSize; private Color map[] = {Color.WHITE, Color.Blue}; ClickComponent(ClickData c, int squareSize) { this.c = c; this.squareSize = squareSize; setPreferredSize(new Dimension(c.getX() * squareSize, c.getY() * squareSize)); addMouseMotionListener(this); addMouseListener(this); } protected void paintComponent(Graphics g) { super.paintComponent(g); for (int y = 0; y < c.getY(); y++) for (int x = 0; x < c.getX(); x++) { g.setColor(map[c.getCell(x,y) % map.length]); g.fillRect(x * squareSize,y * squareSize, squareSize, squareSize); } } public void mousePressed(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) { int x = e.getX(), y = e.getY(); System.out.println("Click (" + x + ", " + y + ")"); x /= squareSize; y /= squareSize; if (c.getCell(x,y) ==0) { c.setCell(x, y, c.getCell(x, y) + 1); } else c.setCell(x, y, c.getCell(x, y) - 1); this.repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} }
EDIT: I have tried and failed to get the parts on this program to work with the other one.
If it helps the program I am trying to make is Conway's Game of Life, I have it working fine in the Command line interface works through .lif files made with the life objects blinkers, spaceship, toad etc. It has a random method which loads those .lif files randomly in the grid (2d array) and then the generations change according to the rule, and is done automatically, and works by printing the contents of the array in the command line. By printing either a 1 or 0. depending on if there is an asterik in the .lif file or not.
I want the program to work graphically, and the code I have above is the closest thing I have to the look I am looking for (not the function), the main Life program which I want to use the square grid is quite long and may not be suitable in this code format. I will upon request though, the attachment button wasn't working so I couldn't do it right now.
EDIT:Java Code:import javax.swing.JFrame; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.FileNotFoundException; import java.awt.Color; import java.io.File; import java.util.Scanner; import java.awt.GridLayout; import java.io.*; import java.awt.Dimension; import java.util.Random; class LifeGrid { int[][] grid; LifeGrid(int x, int y) throws FileNotFoundException{ grid = new int [y][x];} public int getX(){ return grid[0].length; } public int getY(){ return grid.length; } public void show() { for (int i = 0; i < grid.length; i++){ for ( int j = 0; j < grid[i].length; j++) { System.out.print(grid[i][j]); } System.out.println(); } System.out.println(); } public int neighbors(int x, int y) { int n = 0; if ( x+1 < getX()) { if (grid[y][x+1] == 1 ) {n++;} } if (x-1 >= 0) { if(grid[y][x-1] == 1 ) { n++;} } if (y + 1 < getY()) { if (grid[y+1][x] == 1) { n++;} } if (y - 1 >= 0) { if (grid[y-1][x] == 1) {n++;} } if (y-1 >= 0 & x-1 >=0 ) { if (grid[y-1][x-1] == 1) n++; } if (y-1 >= 0 & x + 1 < getX()) { if (grid[y-1][x+1] == 1) { n++; } } if (y+1 < getY() & x-1 >= 0) { if ( grid[y+1][x-1] == 1 ) { n++; } } if (y+1 < getY() & x+1 < getX()) { if (grid[y+1][x+1] == 1 ) { n++; } } return n; } public void run() { int[][] lifegrid = new int[getY()][getX()]; for (int i = 0; i < grid.length ; i++) { for (int j = 0 ; j < grid[i].length ; j++) { int n = neighbors(j,i); if ( grid[i][j] == 1 ) { if ( n < 2) { lifegrid[i][j] = 0; } else if ( n > 3) { lifegrid[i][j] = 0; } else { lifegrid[i][j] = 1; } } else { if (n == 3 ) { lifegrid[i][j] = 1; } } } } grid = lifegrid; } public void random(int x, int y)throws FileNotFoundException { Random generator = new Random(); int j = 0, counter = 0, i = 0, k = 0; String[] lo = {"block.lif", "beehive.lif", "loaf.lif", "boat.lif", "blinker.lif", "toad.lif", "beacon.lif", "pulsar.lif", "glider.lif", "spaceship.lif"}; for (k = 0; k < 30; k++) { for (j = 0; j < lo.length; j++){ int rndx = generator.nextInt(x); int rndy = generator.nextInt(y); File n = new File(lo[j]); Scanner sc = new Scanner (n); while (sc.hasNext()) { String a = sc.nextLine(); for (i = 0; i < a.length(); i++) { if (a.charAt (i) == '*') { grid[rndy][rndx] = 1; } else { grid[rndy][rndx] = 0; } } } if (j == lo.length){ j = 0; } } } } public static void main (String[] args) throws IOException, FileNotFoundException { int squareSize = 10, x = 20, y = 20; String FileName = "outFile.lif"; if (args.length <1) FileName = "outFile.lif"; if (args.length >= 2) x = Integer.parseInt (args[1]); if (args.length >= 3) y =Integer.parseInt (args[3]); if (args.length >= 4) FileName = args[4]; LifeGrid life = new LifeGrid(x , y); life.random(x, y); for(int i = 0; i < 25; i++){ life.show(); life.run(); } } }
Here is the program that I want to use the square grid for, sorry for the extra extra long post.Last edited by nh1402; 01-07-2012 at 04:23 PM. Reason: Adding program I want the square grid for
Similar Threads
-
Output a square of "*"'s depending on the width and height a user specifies...?
By onlyone in forum New To JavaReplies: 2Last Post: 10-26-2011, 08:52 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How can I square(^2) the pic in the grid
By racewithferrari in forum New To JavaReplies: 2Last Post: 11-03-2009, 05:27 PM -
How can I square(^2) the pic in the grid
By racewithferrari in forum New To JavaReplies: 1Last Post: 11-01-2009, 10:16 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks