10x10 Grid Java - How To Add Numbers To Squares
Mar 12, 2015
So I need to create a 10x10 grid for a snakes and ladders board, the board loads but i cant figure out how to add numbers to the squares. Here is my code :
import java.awt.*;
import javax.swing.*;
public class Board
{
//Creating the array
int[][] numbers=new int[10][10];
public static int rows = 10;
public static int columns = 10;
public static Color col1 = Color.green;
[Code] .....
View Replies
ADVERTISEMENT
Oct 22, 2014
so for my computer science class, we have the following problem:
In your documentation be sure to say which loop you thought was easier to implement for this program and why Write a program that produces the following outputs. The first line of output must be written using a For-Loop. The second, using a While-Loop and the third using a Do-Loop.
You look at the program and smile because you see that the series is just the numbers 1-10 squared...just one line is in reverse order. No problem! As you start typing you discover that the number 8 key on your keyboard is not working. For this program you are unable to use the multiplication key
So, back to the drawing board. By staring at the sequence a light comes on and you are able to quickly finish this program WITHOUT using mulitiplication (or the Math class/ solve with arithmetic operators +, - or / ) .
Sample output:
For Loop
1 4 9 16 25 36 49 64 81 100
While Loop
100 81 64 49 36 25 16 9 4 1
Do While Loop
1 4 9 16 25 36 49 64 81 100
I have the "for" done, but I did it with a "for" in the beginning but then had a multitude of nested "if" loops so that I could do 7*7=7+7+7+7+7+7+7
View Replies
View Related
Jun 19, 2014
I know I can calculate the sum of squares as such:
// SumSquares.java: calculate the sum of two squares
class SumSquares {
static int sumSquares(int a, int B)/>/> {
int asquare;
int bsquare;
[Code] ....
But how can I modify the code so that it inputs a list of integer values in the range of -100 to 100 from the keyboard and computes the sum of the squares input values. And how would I go about using exception handling to ensure that the input values are in range and are legal integers.
View Replies
View Related
Jun 19, 2014
I know I can calculate the sum of squares as such:
// SumSquares.java: calculate the sum of two squares
class SumSquares {
static int sumSquares(int a, int B)/>/> {
int asquare;
int bsquare;
[Code] .....
But how can I modify the code so that it inputs a list of integer values in the range of -100 to 100 from the keyboard and computes the sum of the squares input values. And how would I go about using exception handling to ensure that the input values are in range and are legal integers.
View Replies
View Related
Sep 10, 2014
I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimensional array. nothing graphical just plain co ordinates.
View Replies
View Related
Mar 26, 2015
I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...
Here's my code:
package sqrroot;
public class SqrRoot {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double sroot, rerr;
int count = 0;
for(double num = 1.0; num <= 100.0; num++){
[Code] ....
and here is the output:
run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.
[Code] ....
There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)
Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?
View Replies
View Related
Feb 20, 2014
Square:
package assignment1;
import java.awt.*;
public class Square
{
private double x,y; // Current position of the square.
private Color color; // The color of the square.
private int side; // The side of the square
public Square() // constructor
[Code] .....
This is the square class, it draws an orange square that moves around in the applet viewer, works fine and as it supposed to do.
Ship:
package assignment1;
import java.awt.*;
public class Ship
{
private Square[] fleetMembers; // declare fleetMembers as an array of Square
private int total = 5; // Max number of squares in fleet
private double x,y;
Ship() // Constructor
[Code] ....
This is my FleetOfShips code. It is supposed to be a set of ships moving around together, but for unknown to me reasons it doesn't work. It does compile without any problems but when I run the applet it doesn't do anything.
View Replies
View Related
Nov 12, 2014
I'm having trouble filling in the blank squares on a Minesweeper game. What I am trying to is, if a user clicks a square and that square(s) does not have a bomb in it, or adjacent to it, should be set to an empty square.
Here's my recursive method below:
public void revealZeros(){
for(int x = -1; x <= 1; x++)
for (int y = -1; y <= 1; y++){
Square zeroSquare = (Square)board.getSquareAt(xLocation+x, yLocation+y);
if (!(zeroSquare.SquareHasBomb)){
setImage("images/empty.jpg");
}
}
revealZeros();
}
I am traversing around the square that the user clicks on with the use of two for-loops. Getting the square at that location and then applying an empty square image. On that square.
The function should then recurse itself and go onto the next square, if the next square does not have a bomb or does not have an adjacent bomb.
I keep on getting an infinite recursion problem
View Replies
View Related
May 13, 2014
I need to write a method that accepts an array of ints and squares each element of the array. No creating new arrays and no returning any values.
public void squareInts(int[] ints) {
for(int i = 0; i < ints.length; i++) {
ints[i] = (ints[i] * ints[i]);
}
}
View Replies
View Related
Jan 13, 2015
I am making a basic calculator using SWING.I want my JTextField to stretch across the top, above my buttons. All I can seem to get is it be the same size as one of my buttons.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
[code]...
View Replies
View Related
Mar 13, 2014
How to implement GridLayout. In my applet, I want to make a grid of 2 rows and 2 columns. In each grid I want to add a Label and a TextField. I want the background to be red.
So my code would be?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class GridLayoutApplet extends Applet implements ActionListener{
// construct components
Label fNameLabel = new Label("First Name");
TextField fNameField = new TextField(20);
[Code] .....
I have read about panels and frames but, it is all confusing to me. How can you add a label and a TextField to one square of the grid?
View Replies
View Related
Nov 22, 2014
I am currently trying to write a method for checking to see if an "agent" in a cell (place in a 2d array) is satisfied. So, the assignment is a 2D array that can have a blank space, an O, or a X as a character that fills a particular space. An agent is satisfied if it has similar characters around it in a percentage that is higher than the threshold. For example, if the threshold was 60% and a cell that had an X in it, had 3 X's around it and 2 O's, then it would be satisfied because 3/5>=60%. For my code, I tried using if and else statements to isolate circumstances in the corner of the grid, the edges, and everything in between.
public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){
char check= tissue[row][col];
if (check==' '){
return true;
}
else{
int countx = 0;
int count = 0;
[Code] ....
View Replies
View Related
Aug 19, 2014
So I'm making a calculator using JFrame and that stuff and I need figuring out the GridLayouts. I'm not doing this for homework or anything just for fun. I don't need all the grid layouts.
View Replies
View Related
Sep 15, 2014
The point is to use a binary search to determine how many steps it would take to get to int X, int Y in a grid that is N by N. Picture a parking lot that is square and your car is at (x,y). You can honk your horn from your key to determine the direction of the car. I need to return the amount of steps to get to the car. You can't step diagonally. I am currently getting an error that causes an infinite loop and I can't fix it.
public class ParkingLot {
public int search(int N, int X, int Y) {
int minX = 0, maxY = N, minY = 0, maxX = N;
int num = 0;
int curX, curY;
int newCurX, newCurY;
curX = (minX + maxX)/2;
curY = (minY + maxY)/2;
while (curX != X || curY != Y)
[code]....
View Replies
View Related
Nov 14, 2014
I am new to java and i am trying to make a Java application which prints a diamond in a square grid of dots whose side length is input to the application.When you run the code is should be like this:
..*..
.*.*.
*...*
.*.*.
..*..
My java code print this:
..*..
.***.
*****
.***.
..*..
Here is my code:
class Main {
public static void main(String args[]) {
System.out.println("#Enter size of Diamond :");
int longestRow = BIO.getInt();
for(int row=1 ; row<=longestRow ; ++row)
[Code] .....
View Replies
View Related
May 27, 2015
I wanted to know which RAD TOOL should I use to make the grid. I used the label but it's not interesting like the game *. I m starting to make the game and can't think how to make the grid.
Thought of using the buttons but can't add image to it.
what should I use to make the grid.
* what I did earlier Is place 9 label in grid form (just to test) and nine buttons one for each. When u click a button, image is shown in respective label... But it doesn't feel like u r playing game..
View Replies
View Related
Jun 5, 2014
I have a question about updating a grid during runtime in GridWorld. I'm making a game called Flood-It (basically, you click on squares to change their color and attempt to get all of the squares the same color in the grid) and I'm having trouble with changing the grid size. I made my own world class, called CellWorld.
import info.gridworld.actor.*;
import java.util.ArrayList;
import info.gridworld.grid.Location;
import java.awt.Color;
import javax.swing.JOptionPane;
[code]...
Now, let me narrow down the issue. It is in the runner class:
world.setGrid(new BoundedGrid<Actor>(world.getLength(),world.getWidth()));
Whenever a user wants to change the grid size after playing the game, this is supposed to set the grid to the new updated size, but it never changes in the actual game, i.e. the user just won a 2x2 game, attempts to change the size to 10x10, but the grid stays 2x2. By debug testing, I can say for certain that everything else works, such as the maxStepCalc and loading the grid. The only issue is the new grid not showing up.
View Replies
View Related
Mar 28, 2014
I am making a RTS game in Pure Java, and no libraries. So pretty much I want a constructor like so
Java Code: new Grid(40, 40) mh_sh_highlight_all('java');
That will create a 40x40 grid of a certain size per cell. Then I would like to fill the empty cells, and also be able to place things on top of them.
Also, I would like a tilt similar to Clash of Clans. How do I do this?
View Replies
View Related
Nov 19, 2014
Is it possible to put Jlabels in to a grid Layout on a Panel in a Jinternal frame
View Replies
View Related
Aug 22, 2014
Working with some code that displays the random movement of an object such as Move 1: (0,1) Move 2: (10,99)?
I need these methods in the class
void initialize( int xPos, int yPos)
void moveLeft()
void moveRight()
void moveUp()
void moveDown()
int getXPosition()
int getYPosition()
View Replies
View Related
Mar 16, 2015
how to create coordinates in a two-dimensional grid, in the end it shall look like A5, F6 and so on. The aim is to place 3 DotComs in this grid by coincidence. To do this one uses two arrays. One array represents one DotCom, the other represents the grid's size, in this case 49 (7x7).The code ends like this:
ArrayList<String> alphaCells = new ArrayList<String>();
...
int x = 0;
int row = 0;
int column = 0;
while (x<comSize) {
grid[coords[x]] = 1; // mark master grid points as used
row = (int)(coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row));
x++;
}
return alphaCells;
}
why row and column are calculated like this. Furthermore I don't understand why the column is used to generate a character, because columns are marked with numbers and rows are marked with characters.
View Replies
View Related
May 20, 2014
Java Code:
import java.awt.GridLayout;
import javax.swing.*;
public class Screen
{
JButton start;
JButton reset;
JButton box[][] = new JButton[20][20];
[Code] ....
I am trying to place the buttons on the bottom. I tried a few different things but the grid layout keeps grabbing it and making them a part of the grid at the button.
View Replies
View Related
May 9, 2014
I have a get grid method that gets input from the user. I also have a display grid method that is supposed to display the get grid method but it doesn't. What do I do to make the display grid method work properly. I am not allowed to use a main method.
private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
public void getGrid ()
{
System.out.println();
System.out.print ("Enter Row 1: ");
a1 = i.nextInt();
a2 = i.nextInt();
a3 = i.nextInt();
a4 = i.nextInt();
System.out.print ("Enter Row 2: ");
b1 = i.nextInt();
b2 = i.nextInt();
b3 = i.nextInt();
b4 = i.nextInt();
[code]....
View Replies
View Related
Sep 10, 2014
i am to add jpanels to an arraylist such that it forms a grid like a 2d array. when i add the first colunm and have to iterate and store the next sequence of jpanels it still stores it in get(0), instead of get(1).
[/ temp.add(jPanel9);
temp.add(jPanel10);
temp.add(jPanel11);
temp.add(jPanel12);
temp.add(jPanel13);
temp.add(jPanel14);
temp.add(jPanel15);
[Code]...
View Replies
View Related
Dec 16, 2014
I am attempting to make a 5x5 bingo board. My array generates 75 numbers(15 per bingo letter) and displays them in a random order. What I am having trouble figuring out is how to fill the 5x5 grid with 25 numbers from my array. And yes, my code is probably much longer and much more redundant than it needs to be. Also, I think I'm using cells and grids terribly wrong (possibly the entire java language) but I'm not sure.
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.GridLayout;
import javax.swing.JButton;
public class bingoboard extends JFrame
[code]...
View Replies
View Related
Oct 21, 2014
I did the update to JavaFX 8.0 and the grid lines of LineCharts have become dashed. I want them to be solid like they were before the update.
View Replies
View Related