Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  cant even get a single class in  (Read 3006 times)
0 Members and 1 Guest are viewing this topic.
Offline rouncer

Junior Member





« Posted 2010-04-19 16:58:44 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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?
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #1 - Posted 2010-04-19 17:02:17 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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?


1  
pnt = new vector();


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

Offline rouncer

Junior Member





« Reply #2 - Posted 2010-04-19 17:07:20 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #3 - Posted 2010-04-19 17:16:19 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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);
}

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Online Roquen
« Reply #4 - Posted 2010-04-19 17:16:40 »

1  
2  
3  
4  
5  
6  
7  
8  
 vector set(float x, float y, float z)
 {
  this.x=x;
  this.y=y;
  this.z=z;

  return this;
 }
Offline rouncer

Junior Member





« Reply #5 - Posted 2010-04-19 17:30:03 »

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)
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2010-04-19 18:11:17 »

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

Quote
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)
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.

See my work:
OTC Software
Online Roquen
« Reply #7 - Posted 2010-04-19 18:25:32 »


The memory looks like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
       +-----+
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.
Offline rouncer

Junior Member





« Reply #8 - Posted 2010-04-19 18:33:20 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
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;
    }
   }
  }  
 }
 


 
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #9 - Posted 2010-04-19 19:51:05 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #10 - Posted 2010-04-19 20:20:53 »

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

Offline rouncer

Junior Member





« Reply #11 - Posted 2010-04-19 20:38:59 »

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! Smiley

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.
Online Roquen
« Reply #12 - Posted 2010-04-19 20:43:46 »

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
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #13 - Posted 2010-04-19 21:06:44 »

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.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #14 - Posted 2010-04-20 00:17:48 »

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!

See my work:
OTC Software
Offline Wildern

Junior Member





« Reply #15 - Posted 2010-04-20 02:18:44 »

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

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #16 - Posted 2010-04-20 12:31:04 »

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

Offline rouncer

Junior Member





« Reply #17 - Posted 2010-04-20 13:17:21 »

Hey Nate, thats even got a little networking in it, ill definitely have a good read.
Offline CommanderKeith
« Reply #18 - Posted 2010-04-20 13:27:19 »

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

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

Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #19 - Posted 2010-04-20 13:47:50 »

I hate pressing shift all the time to capitalize variable names.

Have you any idea how ridiculous that sounds?

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline SimonH
« Reply #20 - Posted 2010-04-20 14:20:46 »

1t's l1ke me say1ng 1 hate pressing the 'i' key, so 1 press '1' 1nstead.
Grin 1 d0n't l1ke the 'i' key e1ther - 1 @ls0 h@te the 'a' @nd 'o' keys!

@rouncer: Seriously, naming conventions make a lot of sense, that's why they're used. A few seconds formatting and commenting code now can save you hours of trying to understand/remember what your code does in a years time (I speak from bitter experience!).

People make games and games make people
Offline rouncer

Junior Member





« Reply #21 - Posted 2010-04-20 14:55:21 »

Yeh, you heard me, I hate capitalized variable names.
You wanna know something else, I hate object oriented, Im only using java cause its all there is for web development.

I dont plan on learning much past extending off a main class, just to get over the extreme lack of function pointers, I tell you now.

1  
kiss_my_wammy=true; //I only code like id software used to in vanilla C.


Comes out just as professional as any "overdeveloped" framework, you guys spend so long organizing objects nothing ever gets done.
Online Roquen
« Reply #22 - Posted 2010-04-20 15:32:49 »

Depends on what you mean by "all there is for web..".  JavaScript is becoming more and more viable (esp with HTML5) for instance, and then you can use a prototype based model instead of OO.

1  
kiss_my_wammy = true;


That's the GNU coding convention.  And again, in the java convention it's:
1  
kissMyWammy = true;


Both of which require the same number of shift presses  Tongue, but the java convention requires less characters.  But seriously I think the main thing that people like to see is: capitalized first letter of type defines and lower case first letter of variables.  Having variables defined using the GNU convention won't make your Java hard to read, but using lowercase class name will.

If you're asking for help, the less time it takes someone to figure out your example will result in more responses.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #23 - Posted 2010-04-20 16:30:09 »

1  
kiss_my_wammy=true; //I only code like id software used to in vanilla C.


Comes out just as professional as any "overdeveloped" framework, you guys spend so long organizing objects nothing ever gets done.
What about spaces between symbols?

1  
kiss_my_wammy = true;


Nobody is going to say your way is as professional as this.

See my work:
OTC Software
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #24 - Posted 2010-04-20 16:38:06 »

Yeh, you heard me, I hate capitalized variable names.
You wanna know something else, I hate object oriented, Im only using java cause its all there is for web development.
PHP, ASP, Python, Perl

For that matter you can use C for web development too.

I dont plan on learning much past extending off a main class, just to get over the extreme lack of function pointers, I tell you now.

1  
kiss_my_wammy=true; //I only code like id software used to in vanilla C.


Comes out just as professional as any "overdeveloped" framework, you guys spend so long organizing objects nothing ever gets done.
Yeah and all programmers pasty, pale shinned, squinky eyed shrimps.

I don't know how so many closed minded individuals get into the programming profession.

Offline rouncer

Junior Member





« Reply #25 - Posted 2010-04-20 17:35:45 »

Alright im sorry... I dont *hate* oo, its just I know heaps of ways around it so I kinda never learnt it... I could tho, and its kinda ok... sorry sorry, i dont want to make myself unwelcome here.  sorry again, oo is great...   

I just never space between symbols either, and I only use spaces for tabs, cause otherwise your program reaches the other side of the screen and I hate that.

Like I appreciate the help, I really do, without your help I wouldnt be as far as I am now... besides I've got another question. Smiley

So, I've got a projected cube, no near plane clipping at this stage although my triangle rasterer is allowing outer screen triangles to draw without boundary problems.

Now, id like to add a camera that'll spin around the cube... ive got all my maths matrices set up, its just I've got one problem, the only mouse and keyboard library I've seen is really basic... I mean the keyboard is delaying to the bios setup and the mouse only activates when its over the applets window...  Is there a way to get global control of the input? Because you cant really control the program very well with what I've looked at so far.

Also, its a real pain that it doesnt detect keyboard until you click the applet window... what does everybody else do here?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #26 - Posted 2010-04-20 18:13:21 »

Alright im sorry... I dont *hate* oo, its just I know heaps of ways around it so I kinda never learnt it... I could tho, and its kinda ok... sorry sorry, i dont want to make myself unwelcome here.  sorry again, oo is great...   

I just never space between symbols either, and I only use spaces for tabs, cause otherwise your program reaches the other side of the screen and I hate that.

Like I appreciate the help, I really do, without your help I wouldnt be as far as I am now... besides I've got another question. Smiley

So, I've got a projected cube, no near plane clipping at this stage although my triangle rasterer is allowing outer screen triangles to draw without boundary problems.

Now, id like to add a camera that'll spin around the cube... ive got all my maths matrices set up, its just I've got one problem, the only mouse and keyboard library I've seen is really basic... I mean the keyboard is delaying to the bios setup and the mouse only activates when its over the applets window...  Is there a way to get global control of the input? Because you cant really control the program very well with what I've looked at so far.

Also, its a real pain that it doesnt detect keyboard until you click the applet window... what does everybody else do here?
Don't worry, nobody thinks you're a jerk or anything. And you're not going to make yourself unwelcome by having an opinion. People here just like to debate, especially about programming. And we all love Java and OOP, so there you go. Smiley

As for good keyboard and mouse input, check out the JInput library.

PS - I know "heaps of ways around OO" too, but there's a reason to use OO. I could use lead-based paints, mashed berries, or animal blood to paint my walls, but I think I'll still stick with acrylic paint. Wink

See my work:
OTC Software
Offline DzzD
« Reply #27 - Posted 2010-04-20 18:43:10 »

to request focus without clicking the applet you can use :
1  
 this.requestFocus()


Offline Mr. Gol

Senior Member


Medals: 1



« Reply #28 - Posted 2010-04-20 19:01:22 »

Comes out just as professional as any "overdeveloped" framework, you guys spend so long organizing objects nothing ever gets done.

Frameworks make code reuse easier, and they provide solutions for common problems. There is no point in writing your own form input validation for websites when 1000 better implementations already exist. There is also no point in every company writing their own linked list implementation.

You might want to read Code Complete, it contains some information on this type of programming concepts.
Offline cylab

JGO Ninja


Medals: 38



« Reply #29 - Posted 2010-04-21 10:20:33 »

As for good keyboard and mouse input, check out the JInput library.

The problem with global mouse/keyboard input is, that you won't be able to use it without signing your applet and getting a scary security warning the user has to accept. You probably get a malware warning from antivirus software, since global keyboard input enables global keylogging...

Mathias - I Know What [you] Did Last Summer!
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!