• OK, it's on.
  • Please note that many, many Email Addresses used for spam, are not accepted at registration. Select a respectable Free email.
  • Done now. Domine miserere nobis.

A.I. project

Black Rose

An unbreakable bond
Local time
Yesterday 10:32 PM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
I have been wanting to do an A.I. project for a long time but I have been unable to figure out how programming is done in java. So was was hoping that someone would help me along the way. From what I know there is data in arrays/matrices and that data is manipulated by algorithms. I know more about algorithms then programming. All I need is some help rendering my algorithms on screen. I do not expect any help from anyone that doesn't want to help. But would appreciate it if anyone does help.

right now this is all the code I have left from when my computer crashed. It is supposed to display a grid of colors that randomly change color during intervals of 30fps. It needs those colors in an array 400x400.

Once that is done I will work on entering some algorithms for the program. Once it has the basics it will become clear what I/we do next.


Code:
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

public class Colors (Ghraphics g)extends JComponent {

Random generator = new Random();

for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {

int r = generator.nextInt(6);

graphics.fillRect(x, y, 1, 1);

if(r==0){
graphics.setColor(Color.red);
}
if(r==1){
graphics.setColor(Color.orange);
}
if(r==2){
graphics.setColor(Color.yellow);
}
if(r==3){
graphics.setColor(Color.green);
}
if(r==4){
graphics.setColor(Color.blue);
}
if(r==5){
graphics.setColor(Color.magenta);
}
}
}

int p[][] = new int [10][10];

for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
int r = generator.nextInt(1);
p[x][y] = r;
}
}

for (int x = 100; x < 200; x=10) {
for (int y = 100; y < 200; y=10) {

graphics.setColor(Color.black);
if(p[x][y]==0){
graphics.setColor(Color.black);
graphics.fillRect(x, y, 10, 10);
}
if(p[x][y]==1){
graphics.setColor(Color.green);
graphics.fillRect(x, y, 10, 10);
}
}
}

public static void main(String[] args) {
JFrame f = new JFrame("Colors");
f.add(new Colors());
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
 

Cognisant

cackling in the trenches
Local time
Yesterday 5:32 PM
Joined
Dec 12, 2009
Messages
11,358
---
It is supposed to display a grid of colors that randomly change color during intervals of 30fps. It needs those colors in an array 400x400.
Why?

I have been wanting to do an A.I. project for a long time but I have been unable to figure out how programming is done in java. So was was hoping that someone would help me along the way.
Two words: Pen & Paper.

The most straightforward way of approaching AI is "what do I want it to do?" and "how will it do that?", which is just fine for expert systems, or for dealing with problems in a constrained context, but if you want to make strong AI then I suggest you start reading philosophy books (I found "How to be an Existentialist" very helpful) because you need to know exactly what a mind is before you even have a chance of making one.

Let alone one that's sane :D
 

The_Journey

Redshirt
Local time
Today 5:32 AM
Joined
Feb 12, 2010
Messages
7
---
I have been wanting to do an A.I. project for a long time but I have been unable to figure out how programming is done in java. So was was hoping that someone would help me along the way. From what I know there is data in arrays/matrices and that data is manipulated by algorithms. I know more about algorithms then programming. All I need is some help rendering my algorithms on screen. I do not expect any help from anyone that doesn't want to help. But would appreciate it if anyone does help.

right now this is all the code I have left from when my computer crashed. It is supposed to display a grid of colors that randomly change color during intervals of 30fps. It needs those colors in an array 400x400.

Once that is done I will work on entering some algorithms for the program. Once it has the basics it will become clear what I/we do next.


Code:
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

public class Colors (Ghraphics g)extends JComponent {

Random generator = new Random();

for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {

int r = generator.nextInt(6);

graphics.fillRect(x, y, 1, 1);

if(r==0){
graphics.setColor(Color.red);
}
if(r==1){
graphics.setColor(Color.orange);
}
if(r==2){
graphics.setColor(Color.yellow);
}
if(r==3){
graphics.setColor(Color.green);
}
if(r==4){
graphics.setColor(Color.blue);
}
if(r==5){
graphics.setColor(Color.magenta);
}
}
}

int p[][] = new int [10][10];

for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
int r = generator.nextInt(1);
p[x][y] = r;
}
}

for (int x = 100; x < 200; x=10) {
for (int y = 100; y < 200; y=10) {

graphics.setColor(Color.black);
if(p[x][y]==0){
graphics.setColor(Color.black);
graphics.fillRect(x, y, 10, 10);
}
if(p[x][y]==1){
graphics.setColor(Color.green);
graphics.fillRect(x, y, 10, 10);
}
}
}

public static void main(String[] args) {
JFrame f = new JFrame("Colors");
f.add(new Colors());
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

Your code has a lot of errors.

First, why are there codes that are not in a method? Basically from the beginning of your class statement to your main method there are codes that should be within a method but are not.

What is this line right here supposed to be?

Code:
public class Colors (Ghraphics g)extends JComponent

What is the data type of the variable named graphics?
 

Black Rose

An unbreakable bond
Local time
Yesterday 10:32 PM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
I wish to initiate Array Data[][] before starting thread but I do not understand threads. It would be helpful for some one to run this code and rearrange the code so that Data does not change every time repaint.


Code:
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

public class Colors extends JComponent implements Runnable {

	public void initialize() {
    	new Thread(this).start();
    }
	public void run() {
    	while (true) {
      		repaint();
      	try {
        	Thread.sleep(17);
      	} catch (InterruptedException e) {}
    	}
  	}

	public void paint(Graphics g) {
    	initialize();
    	Graphics2D graphics = (Graphics2D) g;
    	Dimension size = getSize();

		Random generator = new Random();

		int[][] Data;
		Data = new int[300][300];

		for (int x = 0; x < size.width; x++) {
     		for (int y = 0; y < size.height; y++) {
     			int r = generator.nextInt(255);

     			Data[x][y] = r;

     				graphics.setColor(new Color(0,Data[x][y],0));
          			graphics.fillRect(x, y, 1, 1);
     		}
		}

  	}

  	public static void main(String[] args) {
    	JFrame f = new JFrame("Colors");
    	f.add(new Colors());
    	f.setSize(300, 300);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.setVisible(true);
  	}
}
 

hablahdoo

Member
Local time
Today 12:32 AM
Joined
Jan 5, 2011
Messages
67
---
Location
New Hampshire
Ah first of your code has got a problem. Take a look at the sequence of events. In the main method (the starting point of the program) you tell the Swing framework to bring your Colors class to life. It makes it on screen and call paint() once as it appears on screen. paint() is the starting point for everything else that happens.

paint() is defined to: call the method initialize(), then do a bunch of other stuff drawing on the screen.

initialize() is defined to: spawn a new thread that does what you defined in run(). All that it means that it's running in a new thread is that the code is running in parallel like it's off the rails of the current logic doing its own thing.

What the thread does is call paint() through repaint() over and over again. And as you know paint() is the very thing that created the thread in the first place. So you just keep getting more and more threads running alongside each other which eat up more and more system resources (check task manager).

All you need is one thread calling paint over and over, not several thousand. You actually don't need to kick off this thread from paint() at all, it's not a good place for it. There's a method you can define for a class called the constructor that is automatically called when the class is instantiated (made). It looks like public Colors(). So I put the new thread there.

Now to what you actually wanted. I declared the array Data as an instance variable belonging to your instance of Colors. This means it's accessible to any method. Then I made the program a bit more clear, moving data generation out from the paint() method and into a new method generateData().

I'm not even sure if this answers your question. I hope you're sufficiently confused.

Code:
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

public class Colors extends JComponent implements Runnable {
    int[][] Data = new int[300][300];
    
    public Colors() {
        new Thread(this).start();
    }
    
    public void run() {
        while (true) {
              generateData();
              repaint();
              try {
                  Thread.sleep(17);
              } 
              catch (InterruptedException e) {}
        }
      }
    
    public void generateData() {
        Random r = new Random();
        for (int x=0; x<300; x++) {
            for (int y=0; y<300; y++) {
                Data[x][y] = r.nextInt(255);
            }
        }
    }

    public void paint(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        Dimension size = getSize();
        
        for (int x = 0; x < size.width; x++) {
             for (int y = 0; y < size.height; y++) {
                 graphics.setColor(new Color(0,Data[x][y],0));
                  graphics.fillRect(x, y, 1, 1);
             }
        }

      }

      public static void main(String[] args) {
        JFrame f = new JFrame("Colors");
        f.add(new Colors());
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      }
}
 
Top Bottom