SasseMan 49 Junior Poster

Hi!
I'm having an issue with maven I can't figure out.

I have two local projects, project1 and project2. Project1 has project2 as a dependecy as you would have any dependecy in maven declared. Everytime I make a change in project2 I have to install it into the local repository and run mvn eclipse:eclipse on project1 to get it up to date. When i debug project1 (in eclipse) and hit project2 code, the source is not avaliable (of course, unless I install with sources).

So what I want is project1 to know that project2 is a local project so that I don't have to install project2 every time I make a change. Is this possible? How is it done?

Thanks!

/S

SasseMan 49 Junior Poster

First step to unit testing is to make your code testable. I'll give you one example. In your mouseReleased method you have a lot of code that does some calculations and set some variables. What you could do here to make the code more testable is to move out the code into a new method that takes some input and procudes some output that is used to set the variables you want to set. It is best if the method does not have any side effects, i.e. everything that the method does has a local scope.

Then when you write your unit test you can test that single method to see if the input produces the desired output using relevant input data and expected outcome.

This is just a quick observation of your code, I'm not sure if what i suggested is applicable in this exact case. You can of course unit test without doing this but it might make life paintful as you will need to mock mouse events etc...

What you need to do is to download junit (junit4 preferably), how it is used depends on your dev environment. To write a junit test, simply create a test class, call it whatever you like and create your test method. In junit4 you annotate your method to tell junit that it is a test. For example

public class ATestClass {

    // This is one test, you can make as many as you like
    @Test
    public void testSomething() { …
SasseMan 49 Junior Poster

This is a super non-trivial problem. Not just a matter of providing an algorithm. But I'm guessing your looking for a library providing eye tracking implementations. A quick google of "eye tracking java" gave me this:

http://lmgtfy.com/?q=eye+tracking+java

which lead me to this:

http://code.google.com/p/experteyes/

which might be interesting.

SasseMan 49 Junior Poster

Hi!
First off, use of JApplet is prefferable over Applet since it supports Swing component architecture. So let your BallWorld class extend JApplet instead, it is essentially the same thing.

Then you should maybe override the init() method in your BallWorld class to set up you applet. You should instansiate your variables in init() instead of where you declare them.

I noticed that you call Thread.sleep in your paint method and then call repaint. This is not how you should set up animations. Have a look at the class javax.swing.Timer which lets you define a timer that will call a method a given number of times per second or something. You can then directly call paint or repaint each time the timer calls your callback method.

I don't know exactly why you get the error. Do you have a stack trace?

SasseMan 49 Junior Poster

Well, for example a get method that, if the object that one asks for is null, creates that objects, and inserts it into some datastructure, populates its values etc..

I've also come across get methods that persists stuff to the database as a side effect. Theres nothing inherently wrong with this, but its simply bad code practice and makes everyone confused.

SasseMan 49 Junior Poster

Can you be more specific? How do you setup your JTable? I'm pretty sure there is no limitation to how many rows a JTable can display.

SasseMan 49 Junior Poster

something like this should work...

File rootDir = new File("/Path/to/dir/");
File[] filesAndDirectories = rootDir.listFiles();

the filesAndDirectories array contains everything in rootDir. You can search for filename given a regexp or something, go down to another directory etc...

SasseMan 49 Junior Poster

Hi!
I'm working with a huge software system with a lot of people being involved in the coding through many years. I'm noticing more and more that there is a lot of bad code, especially get methods here and there with huge side effects, which you totally not expect.

So it crossed my mind that it would be great with a unit test that detects side effects in get methods and reports them so we can clean up that bad code. But it seems kind of difficult, you would have to analyze the code somehow and make sure that nothing but returning of objects takes place.

Anyone have any experience of this and how to detect bad coding practices like these?

Thanks!

/S

SasseMan 49 Junior Poster

read:

http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

under "Writing a Custom Cell Renderer"

SasseMan 49 Junior Poster

First of, do you have to implement your own list? There a many list implementations in java.

Recursive printing could look something like this.

public void print(SinglyLinkedList<E> list) {
   if ( list.size() == 0 ) return;
   // print first element in list
   // call print with a new list where the first element is removed from input list.
}

To print the list in reverse, you just need to switch the print and call line.

In the reverse list case you could maybe do something like this. You want to take out the first element and append it to the end of the rest of the reversed list.

public <E> SinglyLinkedList reverse(SinglyLinkedList<E> list) {
   if ( list.size() == 0 ) return empty list;
   reverse( rest of list ).appendToEnd( firstElement );
}
SasseMan 49 Junior Poster

Hi!
I think the title is quite clear, but is there a way (I'm using Xcode 4) to auto generate the methods defined in a c++ header file to a cpp file? I just want empty methods that are compatible with the definitions in the header. Can't find anything on this.

Thanks!

SasseMan 49 Junior Poster

Hello Daddy! A couple of days might actually be enough. Gonna try this. Thanks!

SasseMan 49 Junior Poster

Hmm, it sound like it might work, but would I really be able to shift the time as much as I want back and forward?

SasseMan 49 Junior Poster

I think you can use AspectJ to inject code anywhere, but will require some setup, which I want to avoid. I found something on replacing bytecode that might do the trick also.

http://blogs.captechconsulting.com/blog/david-tiller/not-so-secret-java-agents-part-1

SasseMan 49 Junior Poster

Yes I know that would work, but It's not possible. Its a huge system used in production with jars that it depend on that I don't have the sources to.

SasseMan 49 Junior Poster

I would have to change all Date references to point to the new Date class.

SasseMan 49 Junior Poster

Writing my own date class insn't an option at the moment. there's a lot of code already creating date and Calendar object in various ways that I don't know anything about. I want to set the system date and time externally without touching any code that accesses dates.

SasseMan 49 Junior Poster

Hi!
I was wondering if there is a way to mock Date and Calendar objects for testing purposes. In other words I want to programatically be able to change what new Date() and new Calendar() etc. returns independent of the system date. Something like setting the date for the jvm or something. I have no clue how it could be done.

Any thoughts?

SasseMan 49 Junior Poster

I work at a software company that makes retail enterprise systems. I started hereright out of the university and have been working for about a year. The first couple of moths they gave me some time to familiarize myself with the systems and the code. I started of with minimal help/education from other engineers and then a lot of bugfixing, which they thought was a good way for me to learn and for them to evaluate if I was good enough.

Now after the learning period the work can be quite varying. Our requirements and roadmap team gathers info from different sources (customers, engineers...) and prioritizes work that needs to be done. In our team we look at the roadmap and plan sprints (we work with scrum). At the scrum meetings we divide up the work between ourselves and then do the work. It can be anything from bug fixing, buissness logic implementation, country customization, refactoring old (bad) code, researching new ideas, making implementation proposals, updating/writing documents, helping customer customiziation teams, making unit tests and other tests...

Our boss (The product manager) doesn't tell us directly what to do, he is responsible for what comes into the release/sprints and other stuff. While planning sprints we try to break down tasks into subtaskt and determine as good as we can the time it will take, and we only put in as much tasks in the sprint that we think we have time to do. More often that not we …

SasseMan 49 Junior Poster

I'm searching for a java library/framework that makes printing easier. By easier I mean something that makes it possible to layout and format text/images and other elements in a document by defining layouts in html/xml or something. I also want the layout to be decoupled from rendering so the document can be printed to a screen, a4 printer, receipt-printer, or in general any arbitrary device that can show a document (But mostly different kind of printers).

Does anyone know of something like this? Are there any widely used libraries? Javas internal printing capabalities doesn't do much good. Have googled it for some time now and can't find anything that seems good enough. Any thoughts?

/thanks

SasseMan 49 Junior Poster

Call what from which other class?

SasseMan 49 Junior Poster

You should explain what it is your trying to do. This makes no sense.

SasseMan 49 Junior Poster

What do you mean by ArrayList of three classes?

You could create an ArrayList which hold Objects. Or maybe an interface class that the three classes have incommon.

public interface GeneralType {
   public void someMethod();
}

// Use Object as the generic type and cast to something else later.
List<Object> theList = new ArrayList<Object>();

// Use an Interface as the generic type which the three classes inherit from.
List<GeneralType> the List = new ArrayList<GeneralType>();
SasseMan 49 Junior Poster

Oh, I must have started answering before the posts were posted, did not see them.

SasseMan 49 Junior Poster

Your problem description is a little unclear, but, first I'm noticing that the program only will terminate if "N" is typed, everything else is going to result in another game loop.

Secondly, the number you're guessing is going to be the same for every replay since a new number is not generated in the loop. You need to put the random number generation inside the while loop.

Can you elaborate on exactly what the problem is?

SasseMan 49 Junior Poster

Well there is a java matlab interface:

http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html

I haven't tried it myself but it looks promising. What is that you want to do with matlab that needs more user interface than matlab provides? Matlab has alot of plotting and presentation tools. You will still need to have a matlab session running on the computer, so the program wont be standalone.

SasseMan 49 Junior Poster

The first thing I'm noticing is that all your code is en the paint method. This method should only be overriden if you want to do custom painting like the painting you are doing in the beginning of the method. You should not add swing components to layouts etc. in this method. You should do it once when the program is started.

So move out the layout code for the radiobuttons and other swing components to another method that is called once by the constructor or something. The layout of the components will conform to their parent containers depending on what layout manager you use.

The paint(Graphics g) method will be called constantly while you are resizing a window or anytime anything needs to be painted onto the screen. So if you add new components for each time the method is called, you will get a hell of a lot buttons. And will probably be out of memory pretty fast.

On another note, you should preferrably override paintComponent(Graphics g) instead of paint(Graphics g). paint paints borders and children and stuff, and is calling paintComponent, while paintComponent is only responsible for painting itself.

SasseMan 49 Junior Poster

Something like this...

String input = "123";
int parsedInput = 0;
try {
   parsedInput = Integer.parseInt(input);
} catch(NumberFormatException nfe) {
   // Handle incorrect input
}
stultuske commented: no idea why this should be negative, since it is an answer to what the OP originally asked +14
SasseMan 49 Junior Poster

I took a shortcuts by assuming that the length of a label is never gonna get wider than the viewport, if it does, we'll burn that bridge when we come to it. I stumbled upon another weird thing while trying to layout components using GridBagLayout for one of the cards in the cardLayout. The GridBagLayouts contents where set to fill hor. and ver. and the scrollpane/viewport had no preferred size or max size set. What happened was that the card panels in the card layout started getting wider and wider. I Overrode paintComponent just to print out getBounds().width. The values where growing constantly every time paintComponent was called. Bug? Feature? Must one always set preferred size?

SasseMan 49 Junior Poster

Hi!
I have a JScrollPane that contains a JPanel with a CardLayout, which in turn contains two JPanels with some labels and stuff. I want the contents of the ScrollPane/ViewPort to never exceed the width of the viewport.

The problem I'm having is that when a label holds a lot of text, the components inside of the scroll are not completely visible, they dissapear a little on each side. Is there a way of forcing a max size for the scrollpane or something so its contens never gets larger that the scrollspanes horizontal width?

mKorbel commented: nice qurstio +11
SasseMan 49 Junior Poster

Can it be a plugin that you don't have in the new version of netbeans? Check what plugins that are active in the old version etc...

SasseMan 49 Junior Poster

Hi!
I'm having some problems with my build files while running junit tests via ant. The build worked fine until a couple of days ago when I got a "process fork failed" error message. Further down in the stack trace i got:

Caused by: java.io.IOException: Cannot run program "C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe" (in directory "C:\p4work\user\product\aProduct\stdproduct\6.9-MAINT\_tmp_derived\dist"): CreateProcess error=87, The parameter is incorrect

From what I understad so far, the arguments passed to javac are way to long for windows to handle.

I googled some and found this:

http://huionn.wordpress.com/2010/03/26/ant-javac-failed-with-%E2%80%9Cerror87-the-parameter-is-incorrect%E2%80%9D/

Apparently there is a way to pass arguments in a file, but my problems is how to do this with junit in ant? Another solution is to try to shorten the arguements that are passed.

Does anyone recognize this problem?

SasseMan 49 Junior Poster

Sounds advanced. Do you want to implements some kind of robot with AI that walks your dog?

SasseMan 49 Junior Poster

You will have to elaborate the problem.

SasseMan 49 Junior Poster

Yes, I know that. I was actually thinking about how to set up testing in ant to filter out which classes that I want to be tested and not. There was an ant task defining exactly that, which I changed to fit my needs.

SasseMan 49 Junior Poster

Hi!
I'm looking for a way to only run a few tests instead of all existings JUnit test cases in our automated build, which uses ant. I can't add @ignore annotations in the code since it would take to much time. Is there a way to define which test cases/classes Junit should run during the build process?

Thanks!

SasseMan 49 Junior Poster

If your goal is to represent a number with n significant decimals, you should use BigDecimal instead of Double.

If it's simply a formating issue, have a look at NumberFormat and its subclasses (DecimalFormat).

SasseMan 49 Junior Poster

You compile in the order of depenedency, I'm not sure if javac handles that in some way, try it or google.

If ClassA depends on ClassB, compile ClassA first and then ClassB.

To run you need your main class, i.e. the class where you have the method "public static void main(int[] args)", if that is ClassB then you write...

java ClassB

SasseMan 49 Junior Poster

cOrRuPtG3n3t!x: Your advice on was so bad that I felt obliged to give some more useful input on how to code in java. Do you always dismiss comments that you feel are hurting your credibility?

SasseMan 49 Junior Poster

Sorry I now see a constructor "create". I would still go for a matrix class.

DavidKroukamp commented: uhm its solved? -1
SasseMan 49 Junior Poster

I don't know what you mean here when you say constructor but I see no constructors in your code.

A constructor resides in a class with the same name as the class. The default constructor is a constructor with no input parameters. In my opinion you should write a matrix class, something like this...

public class Matrix {
   // Array that hold the values of the matrix
   private int[][] array;

   // default constructor, instansiates a 3x3 identity matrix
   public Matrix() {
      array = new int[3][3];
      // set array values to 1
   }

   //other constructor, instansiates nxm identity matrix
   public Matrix(int n, int m) {
      array = new int[n][m];
      // set array values to 1
   }

   // method for matrix multiplication
   // Should trow an exception or something if the matrixes are of incompatible dimensions
   public Matrix multiply(Matrix anotherMatrix) throws IncompatibleMatrixException  {
      // return a new matrix that is a multiplication of this matrix by anotherMatrix...
   }

   // More matrix operation methods, for instance methods for setting the values of the matrix.
   public void setValue(int n, int m, int value) {
      array[n][m] = value;
   }

}

Then in your code you can create and operate on matrixes like this...

// Create two 3x3 matrixes.
Matrix a = new Matrix();           // default constructor
Matrix b = new Matrix(3, 3);     // constructor with parameters

// Set some values
a.setValue(1, 2, 73);
b.setValue(0, 1, 66 );

// Multiply the matrixes
Matrix c = a.multiply(b);
SasseMan 49 Junior Poster

You of course also have to import some Junit classes in both cases.

SasseMan 49 Junior Poster

Well, the basics are quite simple. In junit 4 you annotate your test methods like the following. Your class doesn't have to extend anything.

public class SimpleTest {
   @Test
   public void aTestCase() {
      //Some test code...
   }
}

In Junit 3 you method name has to end with Test and the class has to extend TestCase.

public class SimpleTest extends TestCase {
   public void aSimpleTest() {
      //Some test code...
   }
}

I would recommend starting with Junit 4 right away. It's easier in my opinion. Especially when setting up test suites. Just google Junit 4 tutorial.

Regarding netbeans and other IDE:s, There are probably plugins that make creation of test classes and stuff a bit faster, google that.

SasseMan 49 Junior Poster

The work done in the Runnable passed to SwingUtilities.invokeLater is just setting of some text in labels and some other minor stuff. I guess that they used invoke later because that piece of code doesn't run on the EDT. Don't think SwingWorker will be neccesary.

Have changed to revalidate and submitted for testing. Lets see if that was the problem.

SasseMan 49 Junior Poster

Hi!
I'm sitting with a gui bug that I have been stuck with for a while. When a "view" is changed from one "view" to another, the new "view" doesn't get painted properly. The previous view is visibile randomly in the background.

I can't reproduce this in the dev environment so its hard to debug, but It can easily be reproduced in a remote testlab which I don't have access to and can't remote debug from.

So I was going through the code and noticed this:

proteted void updatebuttonsToModel() {
   // some code...
   SwingUtilities.invokeLater(new Runnable() {
      // some gui changes...     
      validate()
      repaint()
   }
}

I am thinking about if changing validate to revalidate() might do me any good? What are the differences beteween these two? It sound like they should do the samt thing. The javadocs doesn't say much. Could this possibly be the problem?

SasseMan 49 Junior Poster

In your method displayText(String s) you construct a JFrame and JTextArea for every call you make to it, which is for ever line you read.

You should only construct these once, and hold the text you want to show in the textArea in a variable and just call textArea.setText(theText).

SasseMan 49 Junior Poster

Sorry, I meant CubicCurve2D.Double

SasseMan 49 Junior Poster

I'm no neatbeans guy but try googling the producer consumer pattern, where your input form is the producer and the gauge and result components consumers.

http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

SasseMan 49 Junior Poster
SasseMan 49 Junior Poster

Have you looked at the Shape interface, its implementing classes, and uses? I think QuadCurve2D.Double is a cubic bezier curve.