Block Class

Anyone know how to make a basic block class and how to size it (blocks like in minecraft). Im also looking for a way to have the blocks selected in the “main” method or wherever it gets selected.

A Class is basically just a collection of data.

What kind of data do you think a “Block” class should have?

I got that, but like I dont know how to set it up and I dont know how to “call” it from my main class

Your main class, or the “public static void main(String[] args)” method?

The “public static void main(…)” method doesn’t have anything to do with the class that it’s in, it’s just the starting point of a java program.

I highly recommend you read the java beginner tutorials http://docs.oracle.com/javase/tutorial/java/index.html.

“Setting it up” or Creating a class is fairly simple - this is all explained in the tutorials.

class Block {
 int width = 0;
 int height = 0;

 // Constructor
 public Block(int width, int height) {
  this.width = width;
  this.height = height;
  System.out.println("My area is: " + getArea() );
 }

 // Method
 private getArea() {
  return width * height;
 }


 // Testing the class
 public static void main(String[] argv) {
  new Block();
 }

}

I know that, its just I want to know how to call the block in rendering or wherever you call it into.

http://docs.oracle.com/javase/tutorial/2d/images/index.html

Thanks, ill look into that, any help was needed :slight_smile: and this will work in a 3d world right?

I recommend you read this useful post for beginners http://www.java-gaming.org/topics/hello/24411/msg/205121/view.html#msg205121

Ive looked into that already, its just that I dont know what to do now and Im stuck on block creation.

If I may be blunt. The lack of substance in your questions suggest, to me, that you do not have a good hold on programming basics. Therefore I suggest you start there and in the mean time shelve your current idea.

You use the word “block” as if it should mean something. I presume it’s a class. Instantiating a class is very easy. You just put the “new” keyword before the class name Ie.

new Block();

Nobody can understand what you’re trying to do or how you’re attempting to do it and why it’s not working considering your vague abstract questions.

Don’t jump to 3D world yet, it’s serious bussiness. Make it 2D first.

Well if I were to make it 2d first, it would take a long time to transfer it over to 3d when thats done wouldnt it?

That’s the road. Of course it’s good to have speed progress but if we’re not really ready for this, you’ll spend more looooong time to debug and make it work.

I suppose that is true, but its going well except that I am only having problems on specifying a block type and all, and Im also having problems sending the different blocks to the rendering class that I have setup.

The block class can be modiifed so they’re rendering themselves. It’ll encapsuled complex function to draw various kind of block. So you just need to pass what they need to draw.

What do you mean by that? ???

Example method that most “renderable” things should have in java 2d


public void render(Graphics g)   // The graphics object is passed to your block by the canvas/jpanel or whatever. 
{
     render stuff here   
     .
     . 
     .
     g.fillrect(x1,y1,x2,y2); //simple drawing of a rectangle. 
     .
     .
     .
}

You should look at the api for the graphics and Graphics2D objects and then look at how to setup your own custom canvas, Frame, or JPanel to do the rendering. If you know how to do that then rendering a simple square/block is trivial. ;D

Going from 2D to 3D is rather difficult early on. Get good at 2D then go to 3D. Its not that hard to switch it just takes practice…lots of it…and an ungodly amount of tutorial reading…I still suck at 3D :cranky: But I will get better as long as I keep working at it. ;D

So don’t be discouraged by asking nooby questions but I would do a quick google search before posting because what you seem to be asking is done many times in tutorials that you can find via google.

Hope this helps.

You could have a 3d array with your blocks stored in it. You can change one of the blocks’ type, and the rendering code will be able to recognize that the one block is different and be able to render that block different.

You could also pass the rendering function a different type for that one block, and it will be rendered different.

If you want to be able to select a certain block, you will have to set up a coordinate system. As for rendering the actual blocks, you will have to know OpenGL and a bit of math in order to render at a certain angle and position.

I do agree with jonjava and StumpyStreet; know 2d cold, and then start getting into 3d.

Bye the way, what 3d engine are you using?

~Longarmx

Oh, i get it, I think, and Im using LWJGL right now but if needed I could switch I guess. And how would you even go about setting up a coordiante system? Im looking into that but I cant find anything :frowning:

thanks a lot, I really appreciate it :smiley: