is anyone here good with java?
Hello, I am a beginner with Java, post your code, and your issue and or objective of it, so we can help you. Peace.
i needed to separate some methods from one class into another but when i put my attributes private in that other class i don't know how to call them in the other class..i basically need to make an object of the class (in my case): i had private Key key; key =new Key(); and then addKeyListener(key); but now i have a new class called Direction and if i put private Direction key; -this is a new attribute , not the one which is in Direction also when i say key=new Direction(); and addKeyListener(key); -it doesn't work i tried to call it different for exp Direction key1=new Direction(); addKeyListener(key1.key); -but this doesn't work either ....
So, the "addKeyListener" method has a "Key" type parameter? Ordinarily, that method would not take another type, like a "Direction" object. Could that be the issue?
Also, you should know that a class can see it's own private members, but other classes cannot! Suppose that class "A" has a private member, "priv". Then class "B" cannot access "priv". However, class "A" can fetch "priv" for class "B" using a method that returns "priv".
can you give me an example of it?
i have this class inside the Screen class: private class Key implements KeyListener { public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); if(key==KeyEvent.VK_RIGHT && !left) { up=false; down=false; right=true; } if(key==KeyEvent.VK_LEFT && !right) { up=false; down=false; left=true; } if(key==KeyEvent.VK_UP && !down) { left=false; right=false; up=true; } if(key==KeyEvent.VK_DOWN && !up) { left=false; right=false; down=true; } } public void keyReleased(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } public void keyTyped(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } }
so i made the Direction class and said : private class Direction implements KeyListener { everything else stayed the same }
I think it's odd that both "Key" and "Direction" implement "KeyListener" and only one works. Does each class compile on its own?
no everything is connected into one class..and i just call one class through the main method..i can send you the code if you want to check the whole thing out..
If you can post the code, that might make a few things more clear! And for some cool semantic highlighting, use three ` symbols on a line above and a line below your code. It will allow coloring like ``` int x = 0; for (int i = 0; 0 < 200; ++i) System.out.println ( i ); ``` ``` above the code some code ``` below the code
in ide or here?
Sorry, here! The IDE will say, "Hey, Dawn. Whatcha doin'? That's not Java..." OpenStudy has some highlighting support if you do that here.
haha XD i knew that lol :p
:) Haha, and so the IDE's like, "Dawn knows what's up."
i will paste the code
Okay, thanks!
package newpackage.graphics; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import newpackage.entities.Apple; import newpackage.entities.BodyPart; import newpackage.entities.Direction; import newpackage.entities.Snake; public class Screen extends JPanel implements Runnable { public static final int WIDTH=800,HEIGHT=600; private Thread thread; private boolean running=false; private BodyPart b; private ArrayList<BodyPart>snake; private Apple apple; private ArrayList<Apple> apples; private Random r; private int xCoor=10; private int yCoor=10; private int size=5; private boolean right =true,left=false,up=false,down=false; private int ticks=0; private Key key; //private Direction key1; public Screen() { setFocusable(true); key = new Key(); //key1 = new Direction(); addKeyListener(key); //addKeyListener(key1.getKey()); setPreferredSize(new Dimension(WIDTH,HEIGHT)); r=new Random(); //Snake s=new Snake(); //s. start(); thread = new Thread(this, "game loop"); thread.start(); } public void tick() { if(snake.size()==0) { b= new BodyPart(xCoor, yCoor, 10); snake.add(b); } if(apples.size()==0) { //79x79 int xCoor= r.nextInt(60); int yCoor= r.nextInt(60); apple=new Apple(xCoor, yCoor, 10); apples.add(apple); } for(int i=0;i<apples.size();i++) { //kada x i y koord zmije dotaknu poziciju jabuke, velicina se povecava a jabuka brise -don't read this if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) { size++; apples.remove(i); i--; } } //max size=15-don't read this for(int i=0;i<snake.size();i++) { if(xCoor==snake.get(i).getxCoor() && yCoor==snake.get(i).getyCoor()) { if(i != snake.size()-1) { stop(); } } } if(xCoor < 0 || xCoor > 79 || yCoor < 0 || yCoor > 60) { stop(); } ticks++; //brzina kretanja zmije-don't read this //prvobitno je bila postavljena na 250000-don't read this if(ticks>690000) { //pravci u kojima se krece zmija,x-osa (+ i -) i y-psa (+ i -)-don't read this if(right) xCoor++; if(left) xCoor--; if(up) yCoor--; if(down) yCoor++; ticks=0; b=new BodyPart(xCoor, yCoor, 10); snake.add(b); if(snake.size()>size) { snake.remove(0); } } } public void paint(Graphics g) { g.clearRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.lightGray); //DrawLine(int x1, int y1, int x2, int y2) -don't read this //vraca DrawLine za liniju koja pocinje u (x1, y1) i zavrsava se u (x2, y2).-don't read this for(int i=0;i< WIDTH /10;i++) { g.drawLine(i*10, 0, i*10, HEIGHT); } for(int i=0;i< HEIGHT /10;i++) { g.drawLine(0,i*10,WIDTH, i*10); } for(int i=0;i< snake.size();i++) { snake.get(i).draw(g); } for(int i=0;i<apples.size();i++) { apples.get(i).draw(g); } } public void start() { xCoor=10; yCoor=10; size=5; right =true; left=false; up=false; down=false; ticks=0; snake=new ArrayList<BodyPart>(); apples=new ArrayList<Apple>(); running = true; } public void stop() { int i = JOptionPane.showOptionDialog(this, "Do you want to play again?", "GAME OVER", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null); if (i == JOptionPane.YES_OPTION) { //Snake s=new Snake(); //s. start(); } else { System.exit(0); } } public void run() { //dok je pokrenut program, koristi tick metodu i repaint-don't read this while (running) { //Snake s=new Snake(); //s. tick(); repaint(); } } private class Key implements KeyListener { //proverava koje je dugme pritisnuto na tastaturi i to dugme koristi za pravac public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); if(key==KeyEvent.VK_RIGHT && !left) { up=false; down=false; right=true; } if(key==KeyEvent.VK_LEFT && !right) { up=false; down=false; left=true; } if(key==KeyEvent.VK_UP && !down) { left=false; right=false; up=true; } if(key==KeyEvent.VK_DOWN && !up) { left=false; right=false; down=true; } } public void keyReleased(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } public void keyTyped(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } } }
ignore the comments ..i need to separate the methods tick,stop,start and class key
Okay!
I will help later if you do not get it by then. Just putting a comment here so I can find this easily later.
Hi @MDoodler ! If you're free now, I don't mind if you step in!
I want to past that into my text editor, but much of the white space is gone, making it ugly!
white space??
"Whitespace" is just a term for things like tabs, spaces, and new line characters. Not the leters or numbers, but the invisible (or white) stuff between them. For example, I have this: package newpackage.graphics; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import newpackage.entities.Apple; import newpackage.entities.BodyPart; import newpackage.entities.Direction; import newpackage.entities.Snake; public class Screen extends JPanel implements Runnable { public static final int WIDTH=800,HEIGHT=600; private Thread thread; private boolean running=false; private BodyPart b; private ArrayList<BodyPart>snake; private Apple apple; When I get my text editor running, it might not be too hard to fix.
ohh got it..i thought he deleted the spaces xD
Hmm?
i misunderstood it
Okay!
So "Direction" is something in newpackage, and you are importing it, correct?
yes
package newpackage.entities; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Direction implements KeyListener{ private boolean right =true,left=false,up=false,down=false; public Direction key; public Direction getKey() { return key; } public void setKey(Direction key) { this.key = key; } public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); if(key==KeyEvent.VK_RIGHT && !left) { up=false; down=false; right=true; } if(key==KeyEvent.VK_LEFT && !right) { up=false; down=false; left=true; } if(key==KeyEvent.VK_UP && !down) { left=false; right=false; up=true; } if(key==KeyEvent.VK_DOWN && !up) { left=false; right=false; down=true; } } public void keyReleased(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } public void keyTyped(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } }
this should be in direction,not in screen and i should only call it in Screen() constructor
Okay! Do you see that your private variable "key" is of type "Key", and not "Direction"?
i changed it look at the comments in the Screen() and above
Okay, thanks!
i just put it as comments so that the code could work because the option in comments doesn't XD
Sorry I missed that before! My IDE, Eclipse, isn't complaining about ``` addKeyListener(key1); ```
well i don't get errors or exceptions..only when i play my game the snake isn't moving up/down,left/right
Lyrae? what are you writing ?? :OO
I think the problem here is that Key is an non-static inner class of Screen whilst Direction is not. Note how you are using fields declared in Screen inside Key ``` private class Key implements KeyListener { //proverava koje je dugme pritisnuto na tastaturi i to dugme koristi za pravac public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); if(key==KeyEvent.VK_RIGHT && !left) { up=false; // Not declared in Key down=false; // Not declared in Key right=true; // Not declared in Key } .... } ``` this is possible because the declaration of Key is inside of Screen without a static modifier, which makes it an non-satatic nestled class of Screen. In Java a non-static nested class has full access to all members of the class it's nested in. Direction is not nested and hence won't share it's private members as explained by @theEric. To solve this you have to add getters to Direction ``` package newpackage.entities; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Direction implements KeyListener{ private boolean right =true,left=false,up=false,down=false; ... public boolean getRight() { return right; } ...... } ``` and then call them properly from Screen ``` if (key.getRight()) xCoor++; if (key.getLeft()) xCoor--; ... ```
but i want to add the whole Key class in Direction..not just parts and then i want to call it in the Screen() { ... }
Yes that's what I meant, I removed the other methods to keep the post short :P Like this ``` package newpackage.entities; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Direction implements KeyListener{ private boolean right =true,left=false,up=false,down=false; public Direction key; public Direction getKey() { return key; } public void setKey(Direction key) { this.key = key; } public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); if(key==KeyEvent.VK_RIGHT && !left) { up=false; down=false; right=true; } if(key==KeyEvent.VK_LEFT && !right) { up=false; down=false; left=true; } if(key==KeyEvent.VK_UP && !down) { left=false; right=false; up=true; } if(key==KeyEvent.VK_DOWN && !up) { left=false; right=false; down=true; } } public void keyReleased(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } public void keyTyped(KeyEvent e) { //To change body of generated methods, choose Tools | Templates. } /* Getters */ public boolean getRight() { return right; } // And so on .... }
You can then call the getters from Screen ``` if (key.getRight()) doSomething(); ``` ``` if (key1.getRight()) doSomething(); ``` or using some other filed name pointing to an instance of Direction.
The goal is then to have all this up, down, left, and right stuff put into and used from direction, correct? Then similar care would have to be taken when setting up, down, left, and right, if I'm interpreting this correctly. You could use setters for this.I have an issue with the brackets in my source code, still.
Thank you! :) If you don't mind me asking, how did you get the text in its desired format?
I just copy-pasted it from DawnR's posts lol. Probably because we're using different OSes, browsers or something silly like that.
Ah, I see! Thanks :)
@DawnR As a suggestion, use the code format feature with ``` at the top and bottom of the code block. That is what Lyrae did to do: ``` package newpackage.entities; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Direction implements KeyListener{ private boolean right =true,left=false,up=false,down=false; public Direction key; // bla... ``` It is the one on the ~ key, so: \(\text{```}\) package newpackage.entities; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Direction implements KeyListener{ private boolean right =true,left=false,up=false,down=false; public Direction key; // bla... \(\text{```}\) OR, use a code paste site like pastebin or dpaste or github's gist, and so on. They make it a lot easier to copy and paste code to test.
@Lyrae the thing is that in screen i do not need to call right,left,etc only `key` and i have all this `private boolean right =true,left=false,up=false,down=false;` `public Direction key;` `public Direction getKey() {` `return key;` ` }` `public void setKey(Direction key) {` ` this.key = key;` `}` in class Direction i only don't have setters and getters for boolean types which i am not calling in Screen() {}
i just don't see what did you change in the code
I think Lyrae suggested this because you want to move use of the booleans "up", "down", "left", and "right" to "Direction". The booleans that you use now belong to "Screen". Before, "Key" could use them because "Key" is \(\it also\) part of "Sceen" \(\it like\ the\ booleans\). Now, you want "Direction" to have the booleans. So, you gave it booleans. The variable "key1" is a "Direction", and so it has them. BUT! In your screen class, you're never using the booleans of "key1"! You're using the booleans of "Screen". So, you're working with the booleans of "Screen" instead of "Direction", so you won't get good results when you use the function of "Direction". This is an issue in the "start ()" method, and you'll have to \(\sf set\) the booleans of "key1" instead. Likewise, you're not using the booleans in the "Direction" class. I mean, whenever you try to change the coordinates, you're changing them based on the "Screen" booleans. However, the key listener is working with the "Direction" object, "key1". So now you have to make sure that you \(\sf get\) the booleans of "key1". You might be able to see why better if you comment out line 33 or about there, where you have ``` private boolean right =true,left=false,up=false,down=false; ``` This will throw errors wherever the booleans of "Screen" are used.
so how do i keep them in Direction but when i call Direction in Screen how can i only call the key from Direction i wanted to move the tick(),start() and stop() to Snake and also call Snake from Screen..how do i do that?
Join our real-time social learning platform and learn together with your friends!