BestJewSinceJC 700 Posting Maven

I was doing a bit of reading, and according to what I read, GUIs can be set up and realized in any thread, but doing more GUI work in a normal thread (non-EDT) after a call to setVisible can be dangerous. The explanation given is as follows:

The example constructs the GUI in the main thread. In general, you can construct (but not show) a GUI in any thread, as long as you don't make any calls that refer to or affect already realized components. The components in the GUI are realized by the pack call. Immediately afterward, the components in the GUI are shown with the setVisible (or show) call. Technically the setVisible call is unsafe, because the components have already been realized by the pack call. However, because the program doesn't already have a visible GUI, it's exceedingly unlikely that a paint request will occur before setVisible returns.

I couldn't find any more information on it. How could a paint request possibly occur though. . it can't occur programmatically (before setVisible returns) unless it's in a different thread, which isn't the case here, and it can't occur by user action since the window isn't visible yet. I guess that's why it says exceedingly unlikely. But anyway, why is it unsafe if a paint request occurs before setVisible returns? I'm very curious

:)

BestJewSinceJC 700 Posting Maven

I don't know how to play Backgammon but I imagine since GNU's Backgammon game is open source (right?) I suppose you could use file input/output to play each other.

BestJewSinceJC 700 Posting Maven

Read in the first three lines and throw them away. Using fscanf, you can read in doubles with %lf, floats with %f, and you can use %s to read in Strings. %s will get rid of the first word I believe (first thing matches by the delimiter pattern, which is usually whitespace). So you can use fscanf with that in mind to help you reach the part of the file you actually want.

Also, try this advice at your own risk, I haven't programmed in C in a while!

BestJewSinceJC 700 Posting Maven

Read this. I have a feeling it will help you, since it says it is a tutorial on loading and displaying images in Java. :)

http://java.sun.com/docs/books/tutorial/2d/images/

BestJewSinceJC 700 Posting Maven

This can be accomplished the same way you would "connect" two classes that are not GUI classes. You have to make sure your GUI class implements ActionListener though. ActionListener has one method, actionPerformed(ActionEvent e), so implement that method and put the code to get the search term from the text field using the getText method, then use the search class in your actionPerformed method.

BestJewSinceJC 700 Posting Maven

You have to put the i++ after the if statement. If you put it before the if statement, you will be one ahead. (For example, if the first element is the one you want, you will have returned the index 1, but you want to return the index 0)

int i = 0;
          for(x : data)
          {
	        if(x==target)
	            return i;
                    i++;
           }

Also, you have to specify the type of x. int, Integer, String, whatever.

BestJewSinceJC 700 Posting Maven

Your explanation doesn't make sense to me. And not everyone here knows UML. Although I have seen UML before, I don't know what you're trying to do.

BestJewSinceJC 700 Posting Maven

You can't use a return statement unless you are in a method, so I'm assuming that is the case here.

And to figure out the index,
1. Keep a counter and increment it each time you don't find the element you're looking for. Then return the counter

I can't think of any other ways to figure out the index that don't defeat the purpose when using a for each loop as of now.

BestJewSinceJC 700 Posting Maven

Using setVisible(true) on the JFrame should be making it appear.

However, I noticed that you're removing a Container, the JScrollPane, from the JFrame after setting it to invisible. Why not just remove it, then call the validate method? http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html#validate()

edit: I'm going to run your code and see if I can figure out what's going on. Nevermind, I'd need to know where some of your classes came from.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

...
...

This might be helpful. I think the main idea Ezzaral is talking about is the format you save your files in - as in, how the information is stored in each file.

BestJewSinceJC 700 Posting Maven

For your add (should be called insert?) method:

1. You have to first figure out what index they want to add the item at. If the index they want to add the item at is the empty, then just insert it and you're done.
2. If the index is not empty, and your array is large enough to add an item, then you have to move every element after that index down. You have to start at the last filled index and move it down first. Then move the one next to it into it's place, and do this until you can add the item you want to add. You have to start at the very end so that you don't overwrite any items.
3. If you do not have an array large enough, you have to create a new array with enough space, then copy the elements over in the correct spots.

I hope that explanation helps. If you need an example, I can try to give you one, but it's hard to draw it.

BestJewSinceJC 700 Posting Maven

You basically want to set it up so that everything gets repainted, not just your rectangles. So put your drawRectangle statements in the method where all the other graphics are drawn (paint I believe), and use an "if" statement and a boolean to determine if you actually want to draw the rectangle. I think they should be put in the paint method. I'm not completely positive about all of this advice, so wait for someone to verify it. In the meantime, Here is an example from the Java Sun Tutorials http://java.sun.com/docs/books/tutorial/2d/geometry/examples/ShapesDemo2D.java

BestJewSinceJC 700 Posting Maven

This is off topic. . But that appears to be a UML class diagram. . but for some reason it seems like the composition diamond on the Course_Student should be on the other side of the arrow?

BestJewSinceJC 700 Posting Maven

Are you using netbeans or another IDE? And if so, what? In netbeans you can accomplish this by right clicking the Form (on the listing, underneath where it says the project name->package name->form) then clicking save as template, I believe.

BestJewSinceJC 700 Posting Maven

An example. . . (You can google Java WAV and a lot more will come up if this one doesn't work for you)
http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

This is probably a difficult read but it seems to have a lot of relevant and useful information.
http://java.sun.com/docs/books/tutorial/sound/sampled-overview.html

Ezzaral commented: Helpful links and a magnanimous spirit. +19
BestJewSinceJC 700 Posting Maven

The program asks the user to enter a number one time, and then asks to enter the amount of the item five times, totals each item, and then show the overall total.

What is the purpose of the first number the system asks the user to enter? Is it to verify that the user wants to continue the program? Either way, for the second part, where the system asks the user to enter the amount of the item five times, you should use a for loop.

for (int i = 0; i < 5; i++){
//Ask user for the amount of the item
//add this amount to the total amount
}

P.S.

If you click "Reply w/ Quote" on my post, you will see how to use code tags for java syntax. Although it is appreciated that you at least used them period :)

BestJewSinceJC 700 Posting Maven

What do you need help with? Ask specific questions to get help, don't just post your project description and your code. Just let me know what part of the code you're having trouble with and what you aren't understanding and I will be glad to help. If you have any compiler errors, post them. And post your code in code tags next time, see the stickies for how to do this.

BestJewSinceJC 700 Posting Maven

I'm glad you solved whatever issue you were having, and no, I do not work for Daniweb. I'm also not sure how you could interpret my attitude as arrogant or unpleasant, and I'm somewhat confused. But I'll leave this be.

BestJewSinceJC 700 Posting Maven

edit

BestJewSinceJC 700 Posting Maven

Hey, I am not telling you to write the code for me. WTF, I am just asking for help. Its not working thats why I asking you to write your example in the same context. Anyway, don't reply to this thread if you don't want to help.

You are just explaining with complicated words, I've already told you that I am new to Java.

If you don't know something, don't try to act smart, OK !! I am losing my time trying your stupid complicated examples.

You are just a dumb and thanks a lot for your complicated help ... hope someone else will help me and learn to be polite.

It sounded to me like you were asking me to write the code for you. If this wasn't the case, there is no need for you to get upset about it, just clarify what you meant. And obviously, I wanted to help because I responded to this thread. I already know how to do the task you're attempting, and have done the same thing in the past, so it isn't a question of me "not knowing it". And your personal attacks on me are very offensive and uncalled for.

Also, if you don't know what terms such as "parameter" etc mean, you could have asked for clarification or looked them up. If you aren't willing to put in effort to learn, then don't expect your code to work. And if you don't understand the simple terms I used, …

BestJewSinceJC 700 Posting Maven

Comments on circledriver class:
You don't need two Scanner Objects. You can use the same Scanner Object for reading in different types of data. Also, the standard is to capitalize the class name as far as I know, so it should be called CircleDriver.

Comments on DB class

if (select.equalsIgnoreCase("circle") )
        {
            circle c = new circle();
            if (select.equalsIgnoreCase("circle"))
            {
                c.setSelection(select);
                c.setRadius(Radius);
            
            p = c;
            }
      
        }

The second if statement is unnecessary since you already know, at that point, that it will return true.


As far as your area

public void setArea(double Area)
{
Radius=super.getRadius();
Area=3.14*Radius*Radius;
}

Why super.getRadius() and not just getRadius()? They should be calling the same method anyway. Since this class extends the other one, it inherited the getRadius method, and the Radius instance variable.

BestJewSinceJC 700 Posting Maven

I thought what Stephen said was pretty clear. We aren't going to write your code for you. If you don't know how to write an actionPerformed method, then check out this link on how to write action listeners

BestJewSinceJC 700 Posting Maven

We do not write code for people. We help people learn to write their own code.

BestJewSinceJC 700 Posting Maven

I have a way I think. Say I needed to take x to the 456th power. using while loops, I could just continuously square the number until I got to x to the 4096th power, and then multiply that by x to the 256th power, which I'd get using recursion), and multiply that by x to the 128th power, and that by x to the 64th power, that by x to the 16th power, and that by x to the 4th power and finally, that by x to the 1st power.

So I'd have x^4096 * x^256 * x^128 * x^64 * x^16 * x^4 * x^1
which equals X^(4096+256+128+64+16+4+1)
which equals x^4565

It'd be tricky I think, and might need some recursion, but this should work, right?

Can anyone think of a faster algorithm?

This may work, but I do not think it has an advantage over the multiply method. You're essentially doing the exact same operations either way, just making them appear different on paper. I'm pretty sure the computer is doing the same thing.

BestJewSinceJC 700 Posting Maven

To drag rectangles around that you have created, lets say when you click and drag, all you would need to do is get the current position of the mouse and keep calling repaint, drawing the rectangle in the new position. So basically, you'd just need to keep calling repaint, assuming that inside the paint method, you called the drawRect method, passing it the current mouse coordinates.

Note: There are some issues with paintComponent, paint, and repaint. . I'm not sure the reasoning behind having all three, and which ones call what, so be aware that they all exist and that if you're going to play around with it, you should learn those issues.

BestJewSinceJC 700 Posting Maven

I'm not going to write the code for you. You already did what I'm talking about once, with the theClient instance. Just do it again.

BestJewSinceJC 700 Posting Maven

You can share Objects among classes through a constructor (or other method) of one class that accepts a parameter of the other class. See the example I already posted.

BestJewSinceJC 700 Posting Maven

It seems like you've done what I suggested. Assuming Class 1 is named Client, and Class 1 has a method called sendData, then you should be fine. But if you want Class 3 to communicate with Class 1 (or Class 2) you will have to share the objects among those classes as well.

BestJewSinceJC 700 Posting Maven

http://www.google.com/search?q=creating+executable+jar+file&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

What do you want us to do? If you really can't accomplish this task using the command line, use an IDE like Eclipse that will do it for you.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
Read through that tutorial, it should be helpful to you. Courtesy of a post by peter budo that I found by searching daniweb.

BestJewSinceJC 700 Posting Maven

If you have an object, you can use methods in that Object's class. So if you used the setup I had above, you could access any instance variables of "whatever" from the Second class.

BestJewSinceJC 700 Posting Maven
public class First{

First thing = new First();
public static void main(String[] args){
Second second = new Second(thing);
}

}

public class Second{

First whatever = null;

public Second(First param){
whatever = param;
}
}

You basically just pass one Object of the first class's type as a parameter to a method in the other class.

BestJewSinceJC 700 Posting Maven

The only way to use that instance is to have a variable of that class's type in the other class. So if you have two classes, class one and class two, and you want to use class one from inside class two, class two has to have a variable of type class one.

BestJewSinceJC 700 Posting Maven

You might find Eclipse's debugger useful and easy to learn. Although I've personally (In Java) been able to find errors quite quickly with simple print statements.

BestJewSinceJC 700 Posting Maven

I do, but I'm not going to post it. If you want to learn how to use Tables, view the Java Sun tutorial on JTable. http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

BestJewSinceJC 700 Posting Maven

You're using netbeans? It doesn't matter what you're using though, Java works the same regardless. If you're in one class, and you make an instance of that class, a different class will not know it exists. In order for this to happen, you would have to have something like the following:

public class whatever{

whatever oneThing= new whatever();
}

public class anything{
whatever wv = null;
public anything(whatever stuff){
wv = stuff;
}
}

I hope you see what I'm trying to get at here.

BestJewSinceJC 700 Posting Maven

Yes, it makes sense. This code is available on the web, it doesn't make 100% sense to me right now (haven't looked that hard really), but it does what you're looking for. It seems to just use the drawLine method to continuously draw lines where the mouse is positioned and give the appearance of a dragged rectangle. Wouldn't normally give you code but I believe you that this isn't an assignment so...

// Rubber.java    basic rubber band  
//                Draw rectangle. left mouse down=first point      
//                Drag to second point. left mouse up=final point 


import java.awt.*;
import java.awt.event.*;

public class Rubber extends Frame
{
  int winWidth = 500;
  int winHeight = 500;
  boolean tracking = false; // left button down, sense motion 
  int startX = 0;
  int startY = 0;
  int currentX = 0;
  int currentY = 0;
  
  Rubber()
  {
    setTitle("Rubber");
    setSize(winWidth,winHeight);
    setBackground(Color.white);
    setForeground(Color.black);
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });
    setVisible(true);
    this.addMouseListener (new mousePressHandler());
    this.addMouseListener (new mouseReleaseHandler());
    this.addMouseMotionListener (new mouseMotionHandler());
  }
  
  void mouseMotion(int x, int y)
  {
    if(tracking)
    {
      currentX = x;
      currentY = y;
    } 
    requestFocus();
    repaint();
  }

  void startMotion(int x, int y)
  {
    tracking = true;
    startX = x;
    startY = y;
    currentX = x+4;
    currentY = y+4; // nonzero size, may choose to ignore later 
    requestFocus();
    repaint();
  }

  void stopMotion(int x, int y)
  {
      tracking = false; // no more rubber_rect 

      // save final figure data for 'display' to draw 
    currentX = x;
    currentY = y;
    requestFocus();
    repaint();
  }

  class mousePressHandler extends …
BestJewSinceJC 700 Posting Maven

what are you having trouble with? I'd like to help, but what you're trying to do is foreign to me. I'd like to assume it is the same as any other Java code, but that may or may not be the case.

BestJewSinceJC 700 Posting Maven

Increase the size of the text fields?

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextField.html

Notice the method "setColumns" that takes an int or Integer argument.

BestJewSinceJC 700 Posting Maven

What are you having trouble with. . having the numbers in forwards/normal order, or with multiplying the numbers, or both? The Math.pow method is useful, but I'm not sure how large your numbers are. You should check out the BigInteger class. It has multiplication and exponents for large numbers. http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html

BestJewSinceJC 700 Posting Maven

I believe the String class has a toCharArray method that would help you. Then every 3 indexes are what you are looking for. Otherwise, use a for loop to add every substring of 3 chars to an array.

BestJewSinceJC 700 Posting Maven

Ah, sorry, I only glanced over the assignment and missed that requirement. You could change your if() criteria to check that the box is big enough to hold the item. If it's big enough, check against the one you have already found to see if the current box is smaller (or if foundBox is null). If it is, set 'foundBox' to the current box. When you're done, 'foundBox' should contain the smallest box that will accommodate the dimensions you specified.

@ OP: If your post prior to this one is referring to what Ezzaral said, the if statement pseudocode would probably look like this...

if(Box Height >= Item Height && Box Width >= Item Width){
//Do what ezzaral suggested in here.
}

Of course, you will have to replace the statements I made with equivalent code statements. And out of curiosity, why are you using a 2D box instead of a 3D box?

BestJewSinceJC 700 Posting Maven

You should consider the fact that the pathname of the file matters, not only the file name.

BestJewSinceJC 700 Posting Maven

thanks very much but can you give me a suggestion for the regular add and minus methods i tried to do

this.add2(bad)

but the program didnt compile
thank you so much for your help

I looked at your add2 method and your add method. They both look like they will compile. What error are you getting? Post the entire error & your code again in code tags

BestJewSinceJC 700 Posting Maven

Follow Ezzaral's suggestions, unless you're only trying to get input from the user, in which case you can read in Integers, Strings, etc with Scanner. (Google java scanner)

BestJewSinceJC 700 Posting Maven

My internets freaked out. Triple post - sorry guys.

BestJewSinceJC 700 Posting Maven

My interwebs freaked out. What do I do? (Triple post, sorry)

BestJewSinceJC 700 Posting Maven

The code tags are just the following:

for the first one and the last one is exactly what gets pasted in there when you click the button. So all you're adding is the =Java.

And you didn't set your previous thread to solved. While I don't mind helping you, these are both things that you should be doing out of courtesy.

On to the programming, I can come back and give you some more help tomorrow. For now, you might want to consider that "cents" can be much greater than 200. It looks like your code in the add method will not work if cents is > 200. (Lets say you had 0 dollars, 200 cents. Your code would convert that to 1 dollar, 100 cents - but it should convert it to 2 dollars.)[CODE=Java] for the first one and the last one is exactly what gets pasted in there when you click the button. So all you're adding is the =Java.

And you didn't set your previous thread to solved. While I don't mind helping you, these are both things that you should be doing out of courtesy.

On to the programming, I can come back and give you some more help tomorrow. For now, you might want to consider that "cents" can be much greater than 200. It looks like your code in the add method will not work if cents is > 200. (Lets say you had 0 dollars, 200 cents. Your …

BestJewSinceJC 700 Posting Maven

Post your code?

I've used port numbers before, I can't speak for the similarity of my assignment to yours, but I believe I used values that darkagn suggested with no problems. I pretty much just used the Java Sun tutorials on Sockets when I used ports. But post it and I'd be glad to take a look and if I can't help, someone will :) .

BestJewSinceJC 700 Posting Maven

The loop to populate should be a for loop, to add to what the person above me said.

edit: I will check back on this tomorrow if I remember, I'm too tired right now. Sorry!