^^ Isn't that exactly what I just said? Lol
BestJewSinceJC 700 Posting Maven
^^ Isn't that exactly what I just said? Lol
This is just a guess, but I'm somewhat certain that if you don't explicitly call the constructor, the compiler will put in a super() call for you, which calls the default constructor (the constructor with no parameters). Since your Base class does not have a constructor w/ no parameters, that is why you are getting the error. Either add a constructor with no parameters, or in the class where you get the error, add a call to an existing constructor (like Base(int i) constructor)
This is supposed to draw a line through the JButtons when square9 is clicked. It doesn't work but there are no errors and I don't see what the problem is. Ezzaral gave me the code to draw the line earlier. Obviously, the winning squares aren't always diagonal, so I will eventually have to adapt the code to draw the line across the correct squares. My code looks different than the code he gave me; however, I can't figure out any problems with it. The main difference is that he made an inner class for his action listener while I'm implementing ActionListener in my main class, but I don't see why this would be a problem.
public class TicTacToeGUI extends JFrame implements ActionListener{
JPanel panel;
JButton square1 = new JButton("");
JButton square2 = new JButton("");
JButton square3 = new JButton("");
JButton square4 = new JButton("");
JButton square5 = new JButton("");
JButton square6 = new JButton("");
JButton square7 = new JButton("");
JButton square8 = new JButton("");
JButton square9 = new JButton("");
/**
* @param args
*/
public class myGlassPane extends JComponent{
protected void paintComponent(Graphics g){
int x0 = square1.getX()+square1.getWidth()/2;
int y0 = square1.getY()+square1.getHeight()/2;
int x1 = square9.getX()+square9.getWidth()/2;
int y1 = square9.getY()+ square9.getHeight()/2;
g.setColor(Color.RED);
g.drawLine(x0, y0, x1, y1);
}
}
public void actionPerformed(ActionEvent e){
JButton temp = (JButton)e.getSource();
if (temp == square9){
getGlassPane().setVisible(getGlassPane().isVisible() );
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() …
Thanks a lot man. Appreciated.
Also, as a side note, if anybody wants to offer their advice on this also. . How can the visible text that shows up inside a JButton be sized to span the entire button?
I have a TicTacToe board and I need to draw a line through the X's or O's when someone wins. I know how to use the paint method, repaint, etc (as far as how they're called & the basics). I'm just not sure how to go about actually drawing the line. I assume its by repeatedly calling repaint, but I'm unsure about the rest of the logic to actually get the line to run through the squares of the board. One way (I guess) would be to repeatedly give the method coordinates, and "dot" each coordinate until it formed a line. But I think that would be overkill . . & it would be pretty hard to get the coordinates that run through the middle of the JButtons anyway.
One error:
You tried to create a 'Circle' object, but you do not have a class called Circle. Read my example below.
public class Rectangle{ <------After this line, rectangle is a class type.... which means you are allowed to create Objects of the type Rectangle. Saying Rectangle blah = new Rectangle(); creates a new Rectangle called blah. You can create as many Rectangles as you want, as long as the name isn't already taken.
int width; //This creates a variable named width
int height; //This creates a variable named height
public Rectangle(int h, int w){
width = w;
height = h;
//Calling this method creates a Rectangle object with width = w and height = h.
}
public static void main(String[] args){
Rectangle r = new Rectangle(1, 3); //this creates a rectangle called r
Rectangle r = new Rectangle(2, 4); // this is NOT allowed, you already have a rectangle called r.
Rectangle g = new Rectangle(1,1); // this IS allowed
Rectangle h = new Rectangle(1,1); // ALSO allowed!
}
}
Second off, this line of code is a huge error:
Circle myCircle = Circle(myCircle.r);
Why? You do not have a class type called Circle! Earlier, you had the code Circle Circle = new Circle(). I told you to change it to myCircle because of the naming conflict. I did not mean change the class to myCircle (which is what you did), I meant change it like this: Circle myCircle = new Circle(). Also, there is …
Oh ok, I misunderstood you then. Thats fine. Just post up your code after you're finished editing it if you still have any problems.
thanks for the replys! i have changed all of my variables to "myCircle" instead but it still get stuck on that very same line, also i have created a "getradius" public integer. which hasnt helped much.. :/
The idea is that every time you do the following:
Circle whateverName = new Circle();
You are creating a new Object. So you should have a different name every time you create a different Circle. For example,
//Two good Circles
Circle myCircle = new Circle();
Circle myOtherCircle = new Circle();
//Wrong
Circle myCircle = new Circle();
Circle myCircle = new Circle();
First, decide what properties a Bank Account has. For example, a Rectangle has the following properties:
height, width, area
Once you decide what properties a Bank Account has, create variables in your Bank Account class to represent those properties. Then, create methods to do certain things with those properties. Does this sound too vague? Probably. Why? Because nobody can 'get you started' on Java or any programming language for that matter. You have to get yourself started by picking up a book or tutorial on Java, then come to us with specific questions about things you don't understand.
public class Rectangle{
private int height;
private int width;
public static void main(String[] args){
Rectangle rect = new Rectangle(1, 5);
System.out.println(rect.getWidth()); // prints 5
rect.setWidth(3);
System.out.println(rect.getWidth()); //prints 3
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void setWidth(int wdth){
this.width = wdth;
}
//Calling this method creates a new Rectangle object,
//which will have its height and width set to the h and w
//you pass in.
public Rectangle(int h, int w){
height = h;
width = w;
}
/**Note, this method has the SAME method header as the method below
*and the compiler will NOT let you declare the same method twice.
*However, this method does the exact same thing as the one above.
*The reason? Because when you want to create an object, you
*use a constructor (like this one). Where I have this.height,
*saying 'this' means I am referring to the height variable of
*the object I am creating, NOT the height that got passed in (the one in parens).
*/
public Rectangle(int height, int width){
this.height = height;
this.width = width;
}
}
I didn't even notice that, good catch Ezzaral. Also, AceAtch, take a look at the comments I put for your constructor.
public Circle(int x, int y,int r){
r = radiusInt; //This makes r have the value that was in radiusInt
x = radiusInt *2; //this makes x have 2 * the value that was in radiusInt
y= radiusInt * 2; //this makes y have 2 * the value that was in radiusInt
//You probably meant to do this:
radiusInt = r;
//The other code you have doesn't make any sense, since 'Circle' doesn't have any
//variables other than radiusInt
}
I don't think you're understanding the basic idea of how things work. I'll write something real quick to try to help you out. Then, you can try to apply the ideas I give you to your own code.
What they suggested above isn't your problem (well, it is a problem, but its not your error). Your problem is that you can't name your variable with the class name. Circle Circle is invalid because Circle is the class name. The word after the first Circle should be your variable name, which cannot be the name of a class or a reserved word (such as int). For example, you could use
Circle myCircle = new Circle();
However, you should also consider the fact that your code is confusing. You have a variable named radiusInt and you also have a method named radiusInt(). This won't confuse the compiler, since the compiler knows methods are called with parentheses, but it will confuse people. Method names should be descriptive, for example, a method that returns your radiusInt should be called getRadiusInt, like the guy above me said.
Like I said, t is a MEMORY LOCATION. You never changed the code where you said
t = t + 1;
That line of code looks at the memory location that is stored in t, and adds '1' to that memory location. You are getting a seg fault since this is not allowed, since the new memory location that you tried to assign to t is not allowed for your program. What you wanted to do was add '1' to the value that is at t, you didn't want to add 1 to t's memory location. To do so, dereference the pointer like this:
*t = whatever;
the '*' operator goes to the memory location that is stored in t and allows you to change its contents.
I am just trying to explore c and i was wonder what is reason behind ... segmentation fault..
but second function is workiing .. can someone explain me reasonvoid myfunc1(char *t) { t = t+1; t = 'l'; //it gives segmentation fault ? why }
}
You declared your function as taking a pointer to a character. t refers to the memory location where this character is stored. However, to access that memory location, you must use different syntax.
*t= 'l'; should work, if I remember correctly. Try it.
Also, think about the problem as this. If you declare it as char * t,
The contents of t is a memory address, lets say 0xEEFAA (whatever it is, not of concern)
what you WANTED to do is access that memory location and change its contents to 'l'. You can do that by saying *t = 'l';
A seg fault occurs because you are attempting to access a memory location that is not part of the memory space that you are supposed to be accessing. In other words, you have an error in your program that is causing it to end up in an unallowed memory location. You should think of this as a good thing, because it indicates that you have an error. If you didn't get a seg fault, you'd still have an error, but you wouldn't know about it (yet).
I think you could use a 3D KD tree for that. Well, actually, not really, since a KD tree works by analyzing the first key, then second, then third, etc, depending on what level you're on. But I don't see why you can't put all the data into one array, then simply search through the data according to whatever characteristic you're looking for. And Ezzaral, my professors always use Global to refer to what would be the same as a 'static' member of a class in Java. But if you're saying an instance of a class doesn't contain Global variables (I mean, that Global variables aren't class instances), I agree.
This forum should have some kind of a hall of shame, where these types of posts get the attention they deserve.
I'll help you out, Ezzaral. I voted for the option at the top. I'm still not sure what ugent means, though. *also shrugs*
@ Vernon: I got the impression that he isn't creating a new class, he was just giving an example of what he thought the concepts were, and he's going to use those concepts in the class he currently has. No?
Identify the portion of the code that isn't working and explain what it is supposed to do and I will gladly try to help you. Also, if we're supposed to identify your problem based on that 100 lines of input & output and that small segment of code, how are we going to do so if you didn't post the code for your functions that were called in that segment of code?
Lol, this kid literally posted his entire assignment and expects someone to do it for him. Wow.
Yes, the above example will work, as long as you declare x properly. It should say public static int x = 37;
its not a binary search tree.....just a simple binary search
plz help :(
I already did help. I only posted that code because I thought it was relevant and I had already written it for personal use. So I'm not going to write the code for you. However, here's how you would do this in pseudocode:
while your value is not found, do the following:
1. Search the root. If the root contains the value you want, return that node and quit.
2. Search the left subtree.
3. Search the right subtree.
Since every "left subtree" has a root, and every "right subtree" has a root, the above algorithm will work. Go make a method, write the code that will follow that algorithm, and then re-run your program. Hint: the method makes recursive calls to itself in order to search the left subtree and the right subtree.
public class anExample{
private Object obj;
private static Object obj2;
public Object obj3;
public static Object obj4;
public static void main(String[] args){
}
//other methods
}
From a method inside the class with an instance of anExample:
You can refer to (use) obj and obj3.
From a static method inside the class without an instance of anExample:
You can use obj2 and obj4. Being static means the class has one copy of them that is shared by the entire class.
From a method inside a different class, with an instance of anExample:
You can refer to obj3
From a method inside a different class, without an instance of anExample:
you can refer to obj4
So the answer to your question would be the following, although I didn't look at your code. Either use a public or protected variable in your main driver class (the one thats run when you first start the program), use a private instance variable and provide accessors and mutators, or you could use a static variable. In either case, if you wanted easy access to the variable in both of your classes, consider using constructors that take the parameter as an argument.
Use code tags. Do so by clicking go advanced then clicking the icon at the top. It'll look like this: (And by the way, if you are using a Binary Search Tree, my code may be helpful to you, because at first glance, your code seems to be very wrong. But if you're using a BTree then nevermind). Also, the code I posted below only works for ints, so if you wanted to implement it for other types, you'd have to use the equals method, compareTo, and possibly others.
public static void insert( Integer x )
{
root = insert( x, root );
}
private static Node insert( Integer x, Node t )
{
if( t == null )
return new Node( x, null, null );
int compareResult = x.compareTo( t.data );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
private static Node find(Node toFind, Node root){
if (root == null || root.data == toFind.data)
return root;
if (toFind.data < root.data)
return find(toFind, root.left);
else if (toFind.data > root.data)
return find(toFind, root.right);
return root;
}
Are you asking how to add something to an ArrayList? If so, and your ArrayList is called myList, use myList.add(item); where item is what you want to add to the ArrayList.
http://java.sun.com/docs/books/tutorial/networking/sockets/
Sockets in java^, if you get stuck on these, I can help you
http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
^ Using sockets to communicate between different programming languages. Do not bother trying to learn the info contained in this link until you understand how to use sockets in Java, which are much easier. Also, I'm only providing you w/ a reference, if you get stuck, I can't help you, since I don't know the information myself. Good luck
edit again... disregard
http://www.daniweb.com/forums/thread154611-3.html
^^ No, I was talking about marking that thread as solved, which is your previous thread. Its a matter of courtesy to mark a thread as solved, especially when someone spends a lot of their time helping you, as I have.
Anyway, the way that you make a rectangle with no area is easy. Area = length * width, so a rectangle with either length = 0 or width = 0 will have 0 area. So set one of the sides of the rectangle to 0. Again, I direct you to the Javadoc:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html
That doesn't make sense because the Javadoc says that it is an integer. 8.64 is not an integer.
Yeah, thanks. Here's the code I was working on, in case anyone is interested. It finds the max value that can fit in float and double. If no one is interested, whatever, wasn't the purpose of this thread anyway.
#include <stdio.h>
#include <math.h>
double bintodec(char* array);
int main(){
char maxfloat[] = "1111111111111111\0";
char maxDouble[] = "11111111111111111111111111111111\0";
printf("Max value float can hold: %lf, max double can hold: %lf", bintodec(m\
axfloat), bintodec(maxDouble));
return 0;
}
double bintodec(char* array){
double result = 0.0;
double power = 0.0;
int i;
int temp;
double temp2;
for (i = 0; array[i] != '\0'; i++){
temp = 1;
temp2 = temp * pow(2.0, power);
printf("the int = %d, power = %f and result for bin digit is %lf\n", temp\
,power,temp2);
result += temp2;
power = power + 1;
}
return result;
}
que?
I thought the math library was there by default. Like stdio.h.
edit: yeah, it is, I just needed to use the -lm flag
http://www.daniweb.com/forums/thread154611-3.html
I spent 2-3 hours helping you, the least you can do is mark the thread as 'solved'. I also gave you a suggestion on how to do this in the other thread, although I don't think my suggestion was a good way to do it. But if you make the rectangle have no area, then when it is repainted, it will display as invisible.
Its basically telling me it doesn't know wth the pow() function is. But at the top of the file, I have #include<math.h> so shouldn't that work? And right after I have the #include, I declared the function definition of bintodec, so that isn't the problem either.
int bintodec(char* array){
int result = 0;
double power = 0.0;
int i;
int temp;
double temp2;
for (i = 0; array[i] != '\0'; i++){
temp = (int)array[i];
temp2 = temp * pow(2.0, power);
result += (int)temp2;
power = power + 1;
}
return result;
I saw someone, I think it was s.o.s, recommend the use of BigDecimal to someone else. So out of curiosity, and having heard of BigDecimal before, I looked at the Javadoc to see how large the values it could hold were and how it held them. From the doc:
"Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale). "
What does an unscaled value refer to? One that hasn't been correctly sized, and therefore doesn't represent anything in its present state? It seems like whats above could be referring to, lets say we had unscaled value = 123456789 and scale = 5. Then the BigDecimal would treat the scaled version as 123456789.00000? And if scale = -5 it'd be 1234.56789? Is that correct? And how does BigDecimal store numbers?
Suggestions:
1. Where your LeatherBlueDot constructor (the one with 6 arguments) calls super, the arguments you have in super don't exist. item, name, balls, prices, and subs are not defined in the LeatherBlueDot class. You probably meant to say super( itemNumber, itemName, ballCount, ballPrice, subTotal );
2. You have the line SoftballInv3 softBall1 = new SoftballInv3(); but you don't have a no argument constructor in your SoftBallInv3 class. You have LeatherBlueDot softBall2 = new LeatherBlueDot(); but you don't have that constructor in the LeatherBlueDot class either.
3. softBall2.getReStock(stock); Where is stock declared?
4. softBall2.displayArrays(itemNumber, ballCount, coverType, ballPrice, reStock, itemName, subTotal); You seem to be calling this wrong (with the wrong number or types of arguments)
I'm really not sure how you would do that. You should look into the documentation for the various methods you've been using, such as the ones that you are currently using in your paint method. For example, what would happen if you set both the rectangle's length and width to 0 then called repaint? It seems like this would lead to a rectangle with 0 area, which would be painted as nothing on your screen. That probably isn't the best way to go about things, in fact - probably the worst way you could do it - but it will probably work. If you want a better solution, which I'd hope you do, then look into the methods you are using. They have documentation which tells you what happens based on the parameters.
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseListener.html
Thats the Javadoc for MouseListener. Now, you tell me, what method should your code go if you want something to happen when the user clicks the mouse?
What I was saying in my last post is that you want the Rectangle to be moved to wherever you drag your mouse on the screen. This can be done by putting the correct code in mouseDragged. You also want the Rectangle to be colored green when the user unclicks. This can be done by implementing MouseListener, then putting the correct code in the correct method (read the description of the methods in the MouseListener javadoc!). So, in summary, you have to implement both MouseMotionListener and MouseListener.
When repaint() is called, I'm pretty sure it calls paintComponent. So you would have to write the code that does the actual redrawing of the Rectangle based on its position in paintComponent. But when you want to change the Rectangle's color, you can just put the code to change the color in the method that detects when the user unclicks. Then, right after that, you'd say repaint() so that the color change takes effect.
If you need clarification of anything I just said, I will clarify. Beyond that, I won't help anymore unless you post code and ask specific questions about it.
Read the link I gave you about MouseMotionListener. The method detail for mouseDragged states that as the user drags the mouse, MouseEvents continue to occur until the user stops by unclicking. Therefore, if you want the Rectangle to move wherever the user drags it, then you would have to call the repaint() method inside your mouseDragged code. The code that you posted earlier, in mouseDragged in post #11, will be useful for knowing where the mouse currently is. If you read the method details for mouseDragged, you will also notice that it will probably not help you with the part about "I want the rectangle to be green when the user stops dragging it." To make the Rectangle green, you will need to look into other methods. Hint: look into the MouseListener interface. You can do this by googling for Java MouseListener.
A quick example of what I explained in my post above. Comments start with // and are either above the line they refer to, or next to the line they refer to.
public class UsingVariables{
public static void main(String[] args){
int aNewVariable = 0; //creates 'aNewVariable' & initializes it to 0.
//creates 'a_Different_Variable' & doesn't initialize it.
int a_Different_Variable;
System.out.println(aNewVariable); //This will print 0
//We didn't initialize the variable & can't be sure what this prints
System.out.println(a_Different_Variable);
//Not allowed. a_Different_Variable already exists!
int a_Different_Variable = 5;
//This initializes a_Different_Variable to 5.
a_Different_Variable = 5;
System.out.println(a_Different_Variable); //This will print 5.
}
}
Chaster is correct, you must define your variables before you use them. A variable in computer science is the same as it is in normal algebra. For example, x and y are commonly used variables in algebra. Similarly, in Java programming, you can define your own variables. To define a variable of type double named Age of Interest, do the following.
double age_Of_Interest;
Please note the following things about the declaration above:
Actually, you DO need to erase the part where it says "MouseListener", but not for the reason that you think. You were supposed to implement MouseMotionListener. So where it says MouseListener, change it MouseMotionListener. Then, go to the Java API for that class. (Link: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseMotionListener.html). Go to the method summary and make sure that you have each of the methods in your code. Currently, you only have the mouseDragged method in your code, so you will have to add the mouseMoved method to your code.
Also, since you don't yet know how implementing classes works, GO READ THE JAVA TUTORIALS!
Interfaces:
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
No, that will not solve your problem. The problem is that there are methods that you need to implement which you didn't implement. Specifically, you need to implement the mouseExited method. To do that, all you need to do is put the mouseExited method in your code.
That error is because when you implement a class (which you did by saying DragRectangle implements MouseListener) you have to have all of the methods from the class.
Example: lets say these are the methods from MouseListener:
int mouseStuff();
Object moreMouseStuff();
Object otherMouseStuff();
That would mean that any time you write a class that 'implements MouseListener', your class would have to include the methods mouseStuff, moreMouseStuff, and otherMouseStuff. Specifically, the compiler is complaining because you need to have a method in your class that is declared as follows:
public void mouseExited(MouseEvent e){
}
(Side note: what is the point of the code in your mouseDragged method that says if (drag==false)? It seems to me that you never initialized the variable, dragged. Also, it seems like the variable is useless anyway, since if mouseDragged gets called, its guaranteed that the mouse was dragged.)
Note: to the kid who is having trouble, you can ignore everything I say after this line... pay attention to what I said above. Everything after this point is directed at Ezzaral & others.
Documentation for the class can be found here:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseListener.html
If you read the documentation, it says that mouseDragged is NOT a method supported by the class, so I am confused why he implemented it. It says to use MouseMotionListener. Am I looking at the wrong documentation somehow?
edit: I looked at the documentation for MouseMotionListener & back at Ezzeral's code, …
http://www.csee.umbc.edu/courses/undergraduate/341/fall08/Lectures/Trees/BST.pdf
Read those lecture notes. They literally have the remove method in them. So assuming the rest of your code is correct, look at the remove method in those notes and compared it to your delete method. Once you figure out whats different, try to figure out why your code is wrong. If you can't, point out the sections of your code where its different from the correct remove() method and ask us for some help from there. Good luck
Wait until someone verifies this advice, but back in the day, I did a doubly linked list program. I made the delete function somewhat like you did, and my program didn't work correctly. I think it should be declared as
char delete ( ListNodePtr **sPtr, ListNodePtr **tailPtr, char value )
Anyone... verify or contradict that suggestion?
(Don't take this advice until someone experienced elaborates)
I've never used MouseListener, but your mouseDragged code is wrong. "e" refers to a MouseEvent object, NOT your rectangle. Lets say you had an Object that represented your rectangle, here's what your code should look similar to.
//Note this shouldn't be of type object, I'm just showing that it is an object
Object myRectangle;
public void mouseDragged(MouseEvent e){
Object source= e.getSource();
if (source == myRectangle){
myRectangle.setColor(whatever.GREEN);
//call some repaint method here
}
}