mercredi 28 janvier 2015

How to display array data in a grid?

I 've been stuck on this for ages and it's for classwork. Basically I'm trying to display array data into a text box, which will then be updated randomly. Please bear in mind I started coding recently and I barely get what I'm even doing.



import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
import java.io.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.util.Collections;
import java.util.*;

/**
* Write a description of class myWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class myWorld extends World

{
/**
* Constructor for objects of class myWorld.
*
*/
public myWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);


String[] list = new String[10];



int count = 0;
try{
Scanner s = new Scanner(new BufferedReader(new FileReader("words.txt")));
while(s.hasNext()) {
String item = s.next();
item.trim();
list[count] = item;
count++;

}

s.close();

for(int x = 0; x < 1; x++){




}




}
catch(IOException ex)
{
System.out.println("Cannot read file");
System.exit(0);

}
grid grid1 = new grid();
addObject(grid1, 200, 200);

Random rgen = new Random();
for(int i=0; i<1; i++){
int randomPosition = rgen.nextInt(list.length);
String temp = list[i];
list[i] = list[randomPosition];
list[randomPosition] = temp;
System.out.println(list[0] + " " + list[1] + " " + list[2]);
System.out.println(list[3] + " " + list[4] + " " + list[5]);
System.out.println(list[6] + " " + list[7] + " " + list[8]);
}


}


}


That is my myWorld code. Below is the code for the grid.



import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
/**
* Write a description of class grid here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class grid extends Actor
{
/**
* Act - do whatever the grid wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()

{

{


GreenfootImage imgGrid = new GreenfootImage(355, 355);

imgGrid.setColor(new Color (0, 0, 255));
imgGrid.drawRect(0, 0, 350, 350);
imgGrid.drawLine(0, 350/3, 350, 350/3);
imgGrid.drawLine(0, (350/3)*2, 350, (350/3)*2);
imgGrid.drawLine(350/3, 0, 350/3, 350);
imgGrid.drawLine((350/3*2), 0, (350/3*2), 350);
Font font = new Font("Wide Latin", Font.BOLD, 25);
imgGrid.setFont(font);
imgGrid.drawString("night", 10, 30);
imgGrid.drawString("cell2", 125, 30);
imgGrid.drawString("cell3", 250, 30);
imgGrid.drawString("cell4", 10, 150);
imgGrid.drawString("cell5", 125, 150);
imgGrid.drawString("cell6", 250, 150);
imgGrid.drawString("cell7", 10, 275);
imgGrid.drawString("cell8", 125, 275);
imgGrid.drawString("cell9", 250, 275);
setImage(imgGrid);
}
}


}




Aucun commentaire:

Enregistrer un commentaire