Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  just Learned Interfaces  (Read 1702 times)
0 Members and 1 Guest are viewing this topic.
Offline VIrtueeL
« Posted 2014-03-11 11:10:53 »

thay are really good to know if do alot of things like items/animals/AI

i have always created 100 moveMethods for animals like

public void moveCow(){
}
public void movePig(){
}
public void moveZombie(){
}

now thay all have just one called

public void move(){
}
so mutch easyier

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-03-11 11:11:52 »

Yeah...

Offline Roquen
« Reply #2 - Posted 2014-03-11 12:34:34 »

Humm...you don't need an interface for this just method overloading.  And really you don't even need that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VIrtueeL
« Reply #3 - Posted 2014-03-11 12:49:08 »

what xD?

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2014-03-11 12:50:33 »

You could use a structure where you have a superclass and a child class that extends that superclass and inherits the parent functions. An interface here isn't the best choice.

Offline Varkas
« Reply #5 - Posted 2014-03-11 12:52:49 »

... and if you want to be really obscure you can do all method calls "by name" through reflection.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-03-11 12:56:34 »

How was my answer obscure?

Offline SHC
« Reply #7 - Posted 2014-03-11 12:59:34 »

Interfaces are particularly useful if your code involves a common type which has many implementations. The best example here would be the Java Collections API. There is an interface called
List
and there are a lot of implementations of it like
ArrayList
,
LinkedList
etc.,

As @opiop65 pointed out, interfaces are not the best option here. You could instead go with a superclass with subclasses since you are only seeking method overloading.

Offline Varkas
« Reply #8 - Posted 2014-03-11 13:00:52 »

How was my answer obscure?

It's not been about your answer. It was a (inserious) suggestion to the original poster. Smalltalk programmers won't think it's obscure though, since in smalltalk you just need the method name and no interface or super class.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #9 - Posted 2014-03-11 13:03:54 »

Ah, I'm sorry! Thought that was directed at me and I was confused.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VIrtueeL
« Reply #10 - Posted 2014-03-11 18:48:01 »

it works greate so far lols

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2014-03-11 19:08:58 »

So you're going to ignore everyone that posted here who all have more experience than you? That's wise, let me know how that turns out.

Offline VIrtueeL
« Reply #12 - Posted 2014-03-11 19:11:58 »

am not recoding this

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  
public class Player implements Entity{

   int w = 64;
   int h = 64;
   int damage = 7;
   int health = 100;
   int lastHealth = health - 1;
   boolean isDead = false;
   boolean isDamaged;
   static boolean isMoving = false;
   
   boolean MovingRight = false;
   boolean MovingLeft= false;
   boolean MovingUp= false;
   boolean MovingDown= false;
   
   public static int MouseDetection = 150;
   
   public  String Username = "";
   public static Vector2F pos = new Vector2F(0,0);
   public static Vector2F moveto = new Vector2F(0,0);
   
   public Player(int x, int y) {
      this.pos.x = (int) x;
      this.pos.y = (int) y;
   }
   
   BufferedImage image;
   
   @Override
   public void SetEntityImage(BufferedImage image) {
      this.image = image;
   }
   
   @Override
   public void SetEntityImageWidthAndHeight(int w, int h) {
      this.w = w;
      this.h = h;
   }
   
   @Override
   public void RenderEntity(Graphics2D g2d) {
     
      if(image != null){
         //g2d.drawImage(image,  (int) pos.getWorldLocation().x, (int) pos.getWorldLocation().y, w , h, null);
         //g2d.drawRect((int)pos.getWorldLocation().x, (int)pos.getWorldLocation().x, w, h);
         //g2d.drawRect((int)pos.x, (int)pos.x, w, h);
         
         g2d.drawImage(image, (int)Player.pos.getWorldLocation().x,(int) Player.pos.getWorldLocation().y,  w , h , null);
         g2d.drawRect((int)Player.moveto.getWorldLocation().x - 64 + 32 , (int)Player.moveto.getWorldLocation().y - 64 + 32, 128, 128);
         g2d.drawRect((int)Player.pos.getWorldLocation().x ,(int) Player.pos.getWorldLocation().y, w, h);
         Sys.sendSytemMessage(isMoving +"Rendering");
         MoveToLocation();
      }

   }

   private void MoveToLocation() {
         if(Player.pos.getWorldLocation().x < Player.moveto.getWorldLocation().x)
         {
            if(Math.abs(Player.moveto.x - Player.pos.x) > 1)
            {
               Player.pos.x-=0.2;
               isMoving = true;
            }else{
               isMoving = false;
            }
         }
         else
         {
            if(Math.abs(Player.moveto.x - Player.pos.x) > 1)
            {
               Player.pos.x+=0.2;
               isMoving = true;
            }else{
               isMoving = false;
            }
         }
      if(Player.pos.getWorldLocation().y < Player.moveto.getWorldLocation().y)
      {
         if(Math.abs(Player.moveto.y - Player.pos.y) > 1)
         {
            Player.pos.y-=0.2;
            isMoving = true;
         }else{
            isMoving = false;
         }
      }
      else
      {
         if(Math.abs(Player.moveto.y - Player.pos.y) > 1)
         {
            Player.pos.y+=0.2;
            isMoving = true;
         }else{
            isMoving = false;
         }
      }
   }

   @Override
   public void move(float x,float y) {
      this.pos.x += (int) x;
      this.pos.y += (int) y;
   }

   @Override
   public void damage(int amount) {
      this.damage = amount;
   }

   @Override
   public void hurt(int amount) {
      health -= amount;
   }

   @Override
   public void isDead(boolean isDead) {
      this.isDead = isDead;
   }

   @Override
   public void isDamaged() {
      if(health < lastHealth){
         isDamaged = true;
      }else{
         isDamaged = false;
      }
   }

   @Override
   public void isColliding() {}

   @Override
   public void isMoving() {
      if(isMoving = true){

      }
   }

   @Override
   public Rectangle getBounds() {
      return new Rectangle((int)pos.x, (int)pos.y, w, h);
   }

   @Override
   public Rectangle getWorldBounds() {
      return new Rectangle((int)pos.getWorldLocation().x, (int)pos.getWorldLocation().y, w, h);
   }

   @Override
   public void moveWorldLocation(float x, float y) {
      this.pos.getWorldLocation().x += x;      
      this.pos.getWorldLocation().y += y;      
   }

   @Override
   public void mouseDragged(MouseEvent e) {}

   @Override
   public void mouseMoved(MouseEvent e) {
         int y = e.getY();
         int x = e.getX();
   }

   @Override
   public void mouseClicked(MouseEvent e) {}

   @Override
   public void mouseEntered(MouseEvent e) {}

   @Override
   public void mouseExited(MouseEvent e) {}

   @Override
   public void mousePressed(MouseEvent e) {
      if (SwingUtilities.isRightMouseButton(e)) {
         Vector2F pos = new Vector2F(e.getX(),e.getY());
         Player.moveto.x = pos.getWorldLocation().x;
         Player.moveto.y = pos.getWorldLocation().y;
      }
   }

   @Override
   public void mouseReleased(MouseEvent e) {}


}


i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Longarmx
« Reply #13 - Posted 2014-03-12 00:08:08 »

Thats actually not a lot of code. Plus, better to do it now than later.

Offline BurntPizza
« Reply #14 - Posted 2014-03-12 00:15:06 »

Thats actually not a lot of code. Plus, better to do it now than later.

Less than 200 lines, and looks like it would be half that when properly refactored.
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #15 - Posted 2014-03-12 03:12:34 »

I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline The Lion King
« Reply #16 - Posted 2014-03-12 03:29:14 »

I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.

Google is your friend. This is a very basic and very broad question. I'll give you some basics about it though.

An interface is pretty much a partial template for the class. It forces a class that "implements" an interface to have all the functions that the interface states and forces you to define what they do.

If you know what an abstract/virtual method is. A interface is a class that is made up of only abstract/virtual methods.

for example, you mentioned the runnable interface. The whole interface simply looks like this.

1  
2  
3  
4  
public interface Runnable
{
    public void run();
}


so anything that implements Runnable must have the run() function, but every class defines the run function themselves. This guarantees that any Runnable object has the method run().

Ill give you a simple example.

1  
2  
3  
4  
public interface Drawable
{
    public void draw();
}

Example class that uses drawable:
1  
2  
3  
4  
5  
6  
7  
8  
public class Car implements Drawable
{
   //other car methods
     public void draw()
     {
           //explain how to draw a car
     }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class Game
{
    Drawable[] drawThese;
    public Game()
    {
            drawThese = new Drawable[2];
           drawThese[0] = new Car(); //polymorphism
           drawThese[1] = new otherDrawableThing();
    }
    public void gameLoop()
    {
           //do other game loop stuff here
           for(int i = 0; i < drawThese.length; i++)
                 drawThese[i].draw();     //all drawable objects have this method tho they
                                                    //define them differently
    }
}


edit: there is a lot more to learn. Google. Also note the useful characteristic that you can implement as many interfaces as you want and you can only extend from one class in Java.

"You have to want it more than you want to breath, then you will be successful"
Offline trollwarrior1
« Reply #17 - Posted 2014-03-12 05:48:38 »

I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.

Interface is not a class. When you do:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public interface Runnable {
   public void run();
}

{
   Thread thread = new Thread(new Runnable(){
      public void run(){
         System.out.println("overriding runnable meethod..");
      }
   }).start();
}


new Runnable means that you're creating a new class, which implements runnable.
Offline Roquen
« Reply #18 - Posted 2014-03-12 07:48:46 »

The reason interfaces exist is for cross-cuts.   Draw a picture of a tree.  That represent a class hierarchy.  Now if you need common functionality down the tree in nodes whose common parent are far toward the root then you need an interface to make them type compatible for that functionality.  Or similarly to make type compatible classes that creating a common base class is impossible/impractical because they are out of your control.
Offline Grunnt

JGO Wizard


Medals: 84
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #19 - Posted 2014-03-12 09:21:44 »

Really, guys and gals, this is a question which is extremely simple to find many answers to. Here's the top 3 google results for 'java interface':
- What Is an Interface? (The Java™ Tutorials)
- Interfaces (The Java™ Tutorials)
- Interface (Java) on Wikipedia

Each of which are very clear and useable explanations of what an Interface in Java is.

Offline Roquen
« Reply #20 - Posted 2014-03-12 09:28:39 »

I'd note that "design by interface" has been fashionable for some number of years and is a pretty poor model.
Offline jacobgood1

Senior Newbie





« Reply #21 - Posted 2014-03-15 04:34:14 »

Interfaces are also good for decoupling data and functions.  It can pave the way for making objects that are more like data where one can specify actions independent of a hierarchy.  However, to really understand what I am saying(typing), you might need to get some functional language exposure(not necessary, but it speeds up the process).
Offline Roquen
« Reply #22 - Posted 2014-03-15 11:23:29 »

The same can be said for design by composition.
Pages: [1]
  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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (87 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

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

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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!