cant even get a single class in


class vector
{
 float x;
 float y;
 float z;
}

vector pnt;

pnt.x=0;


im doing this and its giving me a null pointer exception… what could i be doing wrong?


pnt = new vector();

maybe study a little more Java before trying to write games ?

a million thanks from this poor kicked dog here, look man i can code games its just this is my first day with java. give me a break, ive read up on polymorphism and everything now, its just I have to finally do the typing in and im a little shakey, but ill get there.

Unfortunately I dont have a book to read about it, all I have is the net, so im looking for a tiny bit of help to get me started.

another thing…
please can you tell me the way to write this out in java properly?


 vector set(float x, float y, float z)
 {
  vector vec;
  vec.x=x;
  vec.y=y;
  vec.z=z;
  return vec;
 }

and how do you make an array of classes… you dont go pnt=new vector[8] cause that gives me the null pointer exception again.


class vector
{
 float x;
 float y;
 float z;

   void set(float x, float y, float z)
   {
         this.x=x;
         this.y=y;
         this.z=z;
   }

}

public static void main(String args[]) {
   vector pnt;
   pnt = new vector();
   pnt.set(0, 0, 0);
}



 vector set(float x, float y, float z)
 {
  this.x=x;
  this.y=y;
  this.z=z;

  return this;
 }

Let me just thank you guys for helping me, really greatful…

Ok I got how to use an array of classes now, you have to construct them one at a time.

But dont you think its a bit overkill to put set vector inside the vector class? because how much ram are you wasting!
Is there a quick struct you can use instead of a full class… or is using classes in this way fine? (i mean for vectors, especially when theres lots of them)

You should probably get used to Java Naming Conventions before you get too comfortable doing it the “wrong” way.

No that’s not very wasteful at all, in any language that I know of. It is indeed a bit faster in C to use a struct, but in Java absolutely everything except primitives are objects. That means that structs don’t exist in Java at all.

The memory looks like this:


       +-----+
vec -> |class| -> (Vector's def, including method table)
       +-----+
       |  x  |
       +-----+
       |  y  |
       +-----+
       |  z  |
       +-----+

So the number of instance methods you have does not effect memory usage. If you want to treat these more like a struct, you can always make the variables public and directly access them.

Ah thankyou for clearing that up for me, both Demonpants and Roquen. There doesnt seem to be such a thing as a static array either, everything has to be references.

Heres my little demo program in full so far (if you have the time to look, if not dont worry about it), all it does is render flat 2d triangles to the screen, can you tell me anything I could do to improve it?
The next thing im implementing is a rotating cube, after I get the basics of the 3d matrix and vector maths into it.


import java.applet.Applet;
import java.awt.Graphics;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.applet.*;
import java.awt.*;
import java.util.*;

class vector
{
 float x;
 float y;
 float z;

 vector set(float x, float y, float z)
 {
  this.x=x;
  this.y=y;
  this.z=z;
  return this;
 }
}

class tri
{
 int[] i;

 void tri()
 {
  i=new int[3]; 
 }
 
}

class matrix
{
 float[][] m;
 
 void matrix()
 {
  m=new float[4][4];
 }

 vector mulvec(matrix mat, vector vec)
 {
  vector vec=new vec();
  
  return vec;
 }
 matrix mulmat(matrix mat1, vector mat2)
 {
  matrix mat=new mat();
  
  return mat;
 }
 
 
}


public class ball extends Applet implements Runnable
{
 int width=800;
 int height=600;
 int scroll=0;
 BufferedImage image;
 int[] outPixels;
 Random rnd;
 
 matrix view;
 matrix proj;
 matrix vp;
 
 vector pnt[];
 tri    tr[]; 
 private Image dbImage;
 private Graphics dbg;
 
 public void init()
 {
  image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  outPixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

  view=new matrix();
  proj=new matrix();
  vp=new matrix();
  
  pnt=new vector[8];
  int i;
  for(i=0;i<8;i++) pnt[i]=new vector(); 
  
  tr=new tri[12];
  for(i=0;i<12;i++) tr[i]=new tri(); 
  
  rnd = new Random();

     
  
  pnt[0].set(-1,1,-1);
  pnt[1].set(1,1,-1);
  pnt[2].set(1,-1,-1);
  pnt[3].set(-1,-1,-1);
  pnt[4].set(-1,1,1);
  pnt[5].set(1,1,1);
  pnt[6].set(1,-1,1);
  pnt[7].set(-1,-1,1);
  

 }

 public void start ()
 {
  Thread th = new Thread (this);
  th.start ();
 }

 public void stop()
 {
 }

 public void destroy()
 {
 }

 public void run ()
 {
  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  while (true)
  {
//   int i;
//   for(i=0;i<width*height;i++)
//   {
//    outPixels[i]=0xFF000000;
//   }
	  
	  
   raster_tri(Math.abs(rnd.nextInt()%800),Math.abs(rnd.nextInt()%600)
		     ,Math.abs(rnd.nextInt()%800),Math.abs(rnd.nextInt()%600)
		     ,Math.abs(rnd.nextInt()%800),Math.abs(rnd.nextInt()%600)
		     ,(255<<24)+((Math.abs(rnd.nextInt()%256))<<16)+((Math.abs(rnd.nextInt()%256))<<8)+(Math.abs(rnd.nextInt()%256)));

   /*
    project the cube.
    draw the tris
    */
   
   
   repaint();

   try
   {
	Thread.sleep (16);
   }
   catch (InterruptedException ex)
   {
   }

   Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  }
 }

 public void update (Graphics g)
 {
  if (dbImage == null)
  {	
   dbImage = createImage (width, height);
   dbg = dbImage.getGraphics ();
  }

  paint (dbg);

  g.drawImage (dbImage, 0, 0, this);
 }

 public void paint (Graphics g)
 {
  g.drawImage(image, 0, 0, width, height, null);
 }






 public void raster_tri(int x0, int y0, int x1, int y1, int x2, int y2, int col)
 { 
  int swap;
  
  if(y1<y0){swap=x0;x0=x1;x1=swap;swap=y0;y0=y1;y1=swap;}
  if(y2<y1){swap=x1;x1=x2;x2=swap;swap=y1;y1=y2;y2=swap;}
  if(y1<y0){swap=x0;x0=x1;x1=swap;swap=y0;y0=y1;y1=swap;}
  
  int xrat0;
  int xrat1;
  int sx0;
  int sx1;

  int i; 
  int j;

  if(y2-y0>0)
  { 
   xrat1=((x2-x0)<<16)/(y2-y0);
   sx1=x0<<16;

   if(y1-y0>0)
   {
    xrat0=((x1-x0)<<16)/(y1-y0);
    sx0=x0<<16; 
	   

    for(i=y0;i<y1;i++)
    {
     if(sx0>sx1)
     {
   	  for(j=sx1>>16;j<sx0>>16;j++) 
      {
 	   outPixels[i*width+j]=col; 
	  }
     }
     else
     {
 	  for(j=sx0>>16;j<sx1>>16;j++) 
	  {
	   outPixels[i*width+j]=col;
	  }
     }
    
     sx0+=xrat0;
     sx1+=xrat1;
    } 
   }
  
  
   if(y2-y1>0)
   {
    sx0=x1<<16;
    xrat0=((x2-x1)<<16)/(y2-y1);
  
    for(i=y1;i<y2;i++)
    {
     if(sx0>sx1)
     {
  	  for(j=sx1>>16;j<sx0>>16;j++)  
	  {
	   outPixels[i*width+j]=col;
	  }
     }
     else
     {
  	  for(j=sx0>>16;j<sx1>>16;j++) 
	  {
       outPixels[i*width+j]=col;
 	  }
     }
    
     sx0+=xrat0;
     sx1+=xrat1;
    }
   } 
  }  
 }
 


 
}

I would strongly recommend going through The Java Tutorial. Do the entire thing from start to finish. This will answer almost all the questions you have right now.

Also people are not going to want to look at your code unless it reads well. That means using Java naming conventions.

tens of things could be improved, here the two first I have seen :
in your main loop you should not do a fixed pause ( like the Thread.sleep(16) you do )
dont call repaint() rather do something like draw ( this.getGraphics () )

also as said above you should use Java naming convention

I did give tutorials a good read, but I dont want to be stuck in books for too long I want to dive in and do it now! :slight_smile:

Ill look into upgrading the main loop, you definitely think it needs work…

ok. Thats enough questions now, I think ill be ok for a while.

But changing my coding style I dont want to do, I like how I code, I hate pressing shift all the time to capitalize variable names.

The main thing people are used to seeing in java is the first letter capitalized for classes…so you don’t have to worry about hitting shift too much. :wink:

Frankly having your own ‘style’ may be fine and well, until you are doing teamwork or go for a job application. Then you may have to struggle to re-adjust… Taking an individualistic view of this issue seems shortsighted to me. These are basic things that are there for a very good reason: making code more readable to yourself and to others.

And being in a professional environment means you’re going to have to change your coding style frequently to match whatever project or language you’ve jumped into. So you’d best get used to it!

You could take a college programming course in Java for free from Stanford

I recommend this online book:
http://math.hws.edu/javanotes/

Hey Nate, thats even got a little networking in it, ill definitely have a good read.

Wow that’s cool! My there are a lot of lectures, 24. Engineers must do a lot of hours

[quote=“rouncer,post:12,topic:35191”]
Have you any idea how ridiculous that sounds?

1t’s l1ke me say1ng 1 hate pressing the ‘i’ key, so 1 press ‘1’ 1nstead.