Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

*PLEASE HELP!!!*

 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone who can help, please help me out. when i click "circle", it still draws a square. I can't find my problem. please help!!
this is what i'm supposed to have.
http://mason.gmu.edu/~rvenna/FinalProject.html
this is what i have.
http://mason.gmu.edu/~bbahr/it108project.html
this is my code.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class it108project extends Applet implements ActionListener
{
private Button rectButton,circButton,moveButton;
private TextField xcordField,ycordField,lenField,widField,diaField;
int xcord,ycord,len,wid,dia;
int flag;

public void init()
{
setLayout(new BorderLayout());
Panel buttonPanel=new Panel();
Button rectButton=new Button("Rectangle");
rectButton.addActionListener(this);
buttonPanel.add(rectButton);
Button circButton=new Button("Circle");
circButton.addActionListener(this);
buttonPanel.add(circButton);
Button moveButton=new Button("Move");
moveButton.addActionListener(this);
buttonPanel.add(moveButton);
add("North",buttonPanel);

Panel textPanel=new Panel();

Label xcordLabel=new Label("X coordinate: ");
textPanel.add(xcordLabel);
xcordField=new TextField(10);
textPanel.add(xcordField);

Label ycordLabel=new Label("Y coordinate: ");
textPanel.add(ycordLabel);
ycordField=new TextField(10);
textPanel.add(ycordField);

Label lenLabel=new Label("length: ");
textPanel.add(lenLabel);
lenField=new TextField(10);
textPanel.add(lenField);

Label widLabel=new Label("width: ");
textPanel.add(widLabel);
widField=new TextField(10);
textPanel.add(widField);

Label diaLabel=new Label("diameter: ");
textPanel.add(diaLabel);
diaField=new TextField(10);
textPanel.add(diaField);
add(textPanel,"South");


}
public void paint(Graphics g)
{
if(flag==1)
g.setColor(Color.red);
g.fillRect(xcord,ycord,len,wid);
if(flag==2)
g.setColor(Color.blue);
g.fillOval(xcord,ycord,dia,dia);
if(xcord>500)
g.drawString("All Values Must Be Less Than "+
"500",50,50);
if(ycord>500)
g.drawString("All Values Must Be Less Than "+
"500",50,50);
if(len>500)
g.drawString("All Values Must Be Less Than "+
"500",50,50);
if(wid>500)
g.drawString("All Values Must Be Less Than "+
"500",50,50);
if(dia>500)
g.drawString("All Values Must Be Less Than "+
"500",50,50);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==rectButton)
{
flag=1;
}
if (e.getSource()==circButton)
{
flag=2;
}
xcord=Integer.parseInt(xcordField.getText());
ycord=Integer.parseInt(ycordField.getText());
len=Integer.parseInt(lenField.getText());
wid=Integer.parseInt(widField.getText());
int dia=Integer.parseInt(diaField.getText());

if (e.getSource()==moveButton)
{
repaint();
}
repaint();

}
}
PLEASE HELP ME!!!
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need braces for one, for the lines below (like this):
if(flag==1)
{
g.setColor(Color.red);
g.fillRect(xcord,ycord,len,wid);
}
if(flag==2)
{
g.setColor(Color.blue);
g.fillOval(xcord,ycord,dia,dia);
}
hope this helps...
Maneesh
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this had no effect. any other suggestions??
PLEASE!!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've made a classic mistake. You've got member variables "rectButton" and "circButton" which are never initialized, and local variables in "init()" that go by the same name. The code in init() is referring to these locals, while the code in actionPerformed() refers to the members, which are always null. You'll see a big change in your program's behavior if you change the two lines in init like
Button rectButton=new Button("Rectangle");
to instead be like
rectButton=new Button("Rectangle");
so that they're assigning to the member variables, rather than declaring new variables altogether.
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this also had no effect. I took "Button" out of the init method and it behaves the exact same. any other suggestions??
PLEASE!!!
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same issue with int dia in the actionperformed method. removed int since you are assigning a new local variable instead of the global variable. I tested this and it works.. you also need the braces i mentioned earlier and all the other fixes that Ernest Friedman-Hill mentioned too.
hope this helps...
Maneesh
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to make it easier to understand, here is what is should say:
dia=Integer.parseInt(diaField.getText()); // the int has been removed
thanks,
Maneesh
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i must be missing something, becuase i have done all these steps and there is no change in my program.
this is my current code:
http://mason.gmu.edu/~bbahr/it108project.java
everything looks right to me.....???
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just ran your code myself and it seems to be running fine too... compilation issue? runtime issue?
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles fine. i just cant get it to draw a circle and it is not even reading the setColor(Color.red) because i get a black square no matter what i click. does it do this for you??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested it and it works too.
Note that the x and y coordinates are relative to the entire component, not just the center region, so you have to type in a y coordinate of at least 100 or you won't see anything. To fix this, you might use a separate component as the paintable in the middle -- i.e., a subclass of JPanel containing the paint() method.
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did it actually draw a blue circle for you?? i can't get it to draw anything but a black square.......
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds to me like the compiled version that is referenced on your html page is an old copy. Try deleting that class and recompile the class file. I tested it and it works exactly as the code states. It draws a red rectangle when rectangle is clicked and a blue circle when the circle button is clicked.
Thanks,
Maneesh
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i removed the .class file and recomplied it. for some reason, this will not work on my machine. my html file reads:
<applet code="it108project.class" height=400 width=900></applet>
click the link to my project in my original post. does it work when you click that link??
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it seems to work fine.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me, anyway, the 900 pixel applet width isn't enough for all the text fields to show up, so there's no way to type into them (the peers aren't created if you can't see them on the screen). You could make the applet area larger, or you could set the layout manager for the bottom panel to new GridLayout(2, 5) to put the components into two rows.
Is that the problem you're having?
 
Brendan Bahr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well then i've come to the conclusion that it must be my browser. if you guys can click the link and it works, well then i should be good to go. Let me go check it on another computer in the office. Thank you everyone for being more than helpful and sharing your time. thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic