Go to Post Never be afraid to admit your ignorance, and ask someone something. You may just learn something new... - kmcclary [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-02-2011, 09:42
ksanger's Avatar
ksanger ksanger is offline
Registered User
FRC #0211 (MaK)
 
Join Date: Mar 2010
Rookie Year: 2010
Location: Rochester NY
Posts: 62
ksanger is on a distinguished road
Joystick(int,int,int) has protected access

Java Help;

We are copying examples out of WPI Robotics Library User’s Guide.pdf which state that we may define a new Joystick(int USBPort#, int #ofAxis, int #ofButtons) but when we do so Netbeans gives us an error stating that

"Joystick(int,int,int) has protected access in edu.wpi.first.pwilibj.Joystick ..."

Alt Ent shows hints which repeats the same message.

First what does this mean, and second how do we fix it. Our team wants to use a gamepad... And we were hoping to be able to determine what the gamepad response is tonight.

We did find Joystic.java and see that Joystick( int port, int numAxisTypes, int numButtonTypes){} is defined as protected. Can we just change this to public? Is this a bug? (note changing it to public and saving the file hasn't worked yet.) (note again, quite Netbeans, restarted, and now have no error message). This is extremely frustrating as the documentation didn't work the first time and I don't know if what I did to fix it is correct, nor if it broke something else?

New questions;
1) If we change a library function should we create a new copy of it and put it in our current project instead of using the library?
2) Why didn't Netbeans see the library change until we restarted it?
3) How do we determine if our change doesn't cause additional problems and break something else?
Reply With Quote
  #2   Spotlight this post!  
Unread 10-02-2011, 09:54
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,082
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Joystick(int,int,int) has protected access

You can make your own class that extends Joystick, and then call the protected super class constructor in your derived class' public constructor. Something like:

Code:
class MyJoystick extends Joystick
{
  public MyJoystick(int port)
  {
    super(port, NUM_AXES, NUM_BUTTONS);
  }
}
Reply With Quote
  #3   Spotlight this post!  
Unread 10-02-2011, 10:04
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 132
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: Joystick(int,int,int) has protected access

Quote:
Originally Posted by ksanger View Post
Java Help;

We are copying examples out of WPI Robotics Library User’s Guide.pdf which state that we may define a new Joystick(int USBPort#, int #ofAxis, int #ofButtons) but when we do so Netbeans gives us an error stating that

"Joystick(int,int,int) has protected access in edu.wpi.first.pwilibj.Joystick ..."

Alt Ent shows hints which repeats the same message.

First what does this mean, and second how do we fix it. Our team wants to use a gamepad... And we were hoping to be able to determine what the gamepad response is tonight.
protected means you can only access it if your class derives from the class it is defined in,

Quote:
We did find Joystic.java and see that Joystick( int port, int numAxisTypes, int numButtonTypes){} is defined as protected. Can we just change this to public? Is this a bug? (note changing it to public and saving the file hasn't worked yet.) (note again, quite Netbeans, restarted, and now have no error message). This is extremely frustrating as the documentation didn't work the first time and I don't know if what I did to fix it is correct, nor if it broke something else?
When you change the library you have to rebuild it not just save it. When you first start netbeans it does a full build. You can change anything you want, but be careful:
When updates come out your changes are over-written.
Chances are the code that is there is better than what you will replace it with. That isn't a poke at you, and there are bugs here and there, but in general the WPI guys do a good job.
I changed a few things here and there, including making things public. But be careful and prepared to maintain it yourself.

Quote:
New questions;
1) If we change a library function should we create a new copy of it and put it in our current project instead of using the library?
I would usually so that they aren't over-written with updates.
Quote:
2) Why didn't Netbeans see the library change until we restarted it?
Answered above.
Quote:
3) How do we determine if our change doesn't cause additional problems and break something else?
That is indeed the question!
If you aren't comfortable that you are either fixing a definte bug, or creating a new functionality, you should leave it alone. You can always extend the Joystick or HID classes and not worry that you've caused an unforeseen side effect.
(BTW when you do find actual bugs post them to Source Forge so that they can get fixed for everyone)
Good Luck & happy Java-ing
Reply With Quote
  #4   Spotlight this post!  
Unread 12-02-2011, 08:00
ksanger's Avatar
ksanger ksanger is offline
Registered User
FRC #0211 (MaK)
 
Join Date: Mar 2010
Rookie Year: 2010
Location: Rochester NY
Posts: 62
ksanger is on a distinguished road
Thanks. Re: Joystick(int,int,int) has protected access

Thanks guys;

I'll look into how and why to extend a class. Never done that before. I assume I need more than Jared341's example?
Reply With Quote
  #5   Spotlight this post!  
Unread 14-02-2011, 22:05
ksanger's Avatar
ksanger ksanger is offline
Registered User
FRC #0211 (MaK)
 
Join Date: Mar 2010
Rookie Year: 2010
Location: Rochester NY
Posts: 62
ksanger is on a distinguished road
Re: Joystick(int,int,int) has protected access

Being a newbe to JAVA and not a programmer I didn't realize that super() calls the initiating function with the additional arguments. This probably won't be clear to non-JAVA programmers either. So in addition to looking up Java extends class you have to look up the keyword super().
Reply With Quote
  #6   Spotlight this post!  
Unread 16-02-2011, 11:27
dbeckwith's Avatar
dbeckwith dbeckwith is offline
Lead Programmer
AKA: Daniel Beckwith
FRC #3205 (The Patriots)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: USA
Posts: 84
dbeckwith is an unknown quantity at this point
Re: Joystick(int,int,int) has protected access

The reason why that constructor is protected is that you're not supposed to use it if you are just making a Joystick object (which assumes you are using an Attack 3 joystick, so it sets up the axes and buttons according to that joystick). But if you are trying to make a Joystick object that is not for an Attack 3, then Jared's suggestion would be the right thing to do. Make your own subclass of Joystick to represent your gamepad. In that case you be representing your own custom Joystick, so you would need to customize the axes and buttons.
Some more complete example code might be:
Code:
import edu.wpi.first.wpilibj.Joystick;

public class Gamepad extends Joystick {
  
  final static int NUM_AXES = 3; // these would be the default values for the
  final static int NUM_BUTTONS = 11; // Attack 3 joysticks included in the KOP
  
  Gamepad(int USBport) {
    super(USBport, NUM_AXES, NUM_BUTTONS); // initialize Joystick with the provided USB port
                                           // and the constants for number of buttons and axes
  }
  
}
Extending the Joystick class basically makes it so Gamepad inherits all the methods and variables that Joystick has (and allows you to access protected methods and variables), but you can add in more functionality.
__________________
q = (2*b) | ~(2*b);

if (life.getLemons() != null) this.lemonade = new Drink(life.getLemons());
else throw new NoLemonsException("What now?");


Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 00:40.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi