Program is not doing what I want it to
Hey, I made an extremely basic program (for you guys), but it took me a while because I am learning. I try to make the words cross, as in an x, but instead, it just displays the code part of the first class.I want it to cross as in an X. Why isn't it doing so? Here is the code, and thank you!
Code:
import javax.swing.*;
import java.awt.*;
class Loop extends JFrame{
int x = 30;
int y = 50;
Loop() {
setTitle("Loop");
setSize(1000,1000);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g){
while(y<1000 && x<1000){
g.drawString("This is a loop.", x, y);
y = y +15;
x= x + 15;
}
}
public static void main(String[] args) {
new Loop();
}
}
class Pic extends JFrame{
int z = 950;
int v = 950;
public void paint(Graphics g){
while(v> 1 && z >1){
g.drawString("This is a loop.", v, z);
z = z - 15;
v= v - 15;
}
}
}
Thank you very much in advanced.
Re: Program is not doing what I want it to
Format your code correctly so it is easy to read. Proper indenting.
Next, do not override paint(). You should be overriding paintComponent().
Indeed you should not be extending JFrame in the first place.
Extend JPanel, then add that to an ordinary JFrame.
Re: Program is not doing what I want it to
Quote:
Originally Posted by
Tolls
Format your code correctly so it is easy to read. Proper indenting.
Next, do not override paint(). You should be overriding paintComponent().
Indeed you should not be extending JFrame in the first place.
Extend JPanel, then add that to an ordinary JFrame.
Hi Tolls. I'm confused as to how to do this. Would I use extend JPanel and then how do i go about making a panel?
Re: Program is not doing what I want it to
Hi Tolls. I did what you requested in the code.
Here it is:
Code:
import javax.swing.*;
import java.awt.*;
class Loop extends JFrame {
int x = 30;
int y = 50;
Loop() {
setTitle("Loop");
setSize(1000,1000);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
while(y<1000 && x<1000) {
g.drawString("This is a loop.", x, y);
y = y +15;
x= x + 15;
}
}
public static void main(String[] args) {
new Loop();
}
}
class Pic extends JPanel{
int z = 950;
int v = 950;
public void paintComponent(Graphics g){
while(v > 1 && z > 1){
g.drawString("This is a loop.", v, z);
z = z - 15;
v= v - 15;
}
}
}
Re: Program is not doing what I want it to
You need to format your code properly.
That is really hard to read and follow the flow without proper indentation.
You aren't using the Pic class anywhere.
Re: Program is not doing what I want it to
Quote:
Originally Posted by
MW130
Hi Tolls. I did what you requested in the code.
Here it is: [ snip ]
According to your own post in the Lobby section:
Quote:
Originally Posted by
MW130
I understand methods, constructors, swing, and all the basics... What do you guys think about this? Thanks for all opinions.
I must ask: who or what created the code in this thread; your hamster?
kind regards,
Jos
Re: Program is not doing what I want it to
Quote:
Originally Posted by
JosAH
According to your own post in the Lobby section:
I must ask: who or what created the code in this thread; your hamster?
kind regards,
Jos
No. I don't have a hamster, and even if I did, I doubt it would be able to learn Java. On a serious note, can you please tell me what to do correctly, how would I set up a panel? Please don't link me to Oracle's panel tutorial, because they are not well-explained. Thanks.
Re: Program is not doing what I want it to
Quote:
Originally Posted by
MW130
No. I don't have a hamster, and even if I did, I doubt it would be able to learn Java. On a serious note, can you please tell me what to do correctly, how would I set up a panel? Please don't link me to Oracle's panel tutorial, because they are not well-explained. Thanks.
Those tutorials are how most of us learned Swing and Java, and the more you use them, the easier they become to use. We've linked you to them for a reason as they explain why you should not draw on JFrames. In fact, I thought that we had this discussion long ago.
Re: Program is not doing what I want it to
Quote:
Originally Posted by
Fubarable
Those tutorials are how most of us learned Swing and Java, and the more you use them, the easier they become to use. We've linked you to them for a reason as they explain why you should not draw on JFrames. In fact, I thought that we had this discussion long ago.
Hi Furable. I know :P. I was just saying that, I don't quite understand the java tutorial on using panels :( ... I understand how to make the panels, but not where I should declare them, and how to set their size. I don't really get what they are in general... Is there another tutorial (other than the oracle one?) Thank you
Re: Program is not doing what I want it to
Hi,
I don't know what you want do it exactly.
But you only extends Jframe that is not enough you also call Jpanel for call the paint method.
I did some changes in your code. I have given that code below.. So Have you look on that..
***********/ Code / ****************
[ crappy, unformatted, spoonfeeding attempting code delete -mod]
Thanks,
Re: Program is not doing what I want it to
Swing consists of containers and things you put in a container.
A JFrame is a container that holds a JPanel (actually slightly more than that, but we'll just stick to the basics) and controls the display of the application as a whole...it's the window the app appears in. A JPanel is a container that holds your other widgets (including other JPanels) and is used to structure the layout of those widgets. Various subclasses of JPanel provide additional functionality (eg JScrollPane), but are still essential panels at heart.
That's it in a nutshell.
So you create a JFrame (don't create your own subclass of it) then either add a JPanel to it, or use the default one. Then add widgets to the JPanel.
In your case you probably just want to add an instance of your Pic JPanel subclass to it.
Re: Program is not doing what I want it to
Quote:
Originally Posted by
tamilarasi
Hi,
I don't know what you want do it exactly.
But you only extends Jframe that is not enough you also call Jpanel for call the paint method.
I did some changes in your code. I have given that code below.. So Have you look on that..
Please do not provide solutions like that. We don't spoonfeed here.
In addition that is poor practice. You do not override the paint() method. That's what paintComponent() is for.