Results 1 to 8 of 8
- 07-17-2010, 11:44 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
how do i call a class from main class
Hallo...I am quite new to java programming and recently I came across a problem which I can't solve almost a week.I have a small garphic project called SmileyFace and when I compile it there's is awlays some problem with it. Could anybody help me just to give me a hint or a clue where am I mistaking by writing the code...
Here's the code:
import java.awt.*;
import javax.swing.*;
public class RunSmileyFace
{
public static void main(String[] args)
{
SmileyFace myObjRef = new SmileyFace();
myObjRef.SmileyFace();
}
}
public class SmileyFace extends JFrame
{
public void SmileyFace()
{
setTitle("Smiley Face");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250,220);
setLocation(300,300);
getContentPane().setBackground(Color.yellow);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g); // call the paint method of the superclass, Jframe
g.setColor(Color.red);
g.drawOval(85,75,75,75); // the face
g.setColor(Color.blue);
g.drawOval(100,95,10,10); // the right eye
g.drawOval(135,95,10,10); // the left eye
g.drawArc(102,115,40,25,0,-180); // the mouth
g.drawString("Smiley Face", 90,175);
}
}// end of the code
I will be very grateful if some one can help me out with it, because I have tried so many different ways and the result is zilch....Thanks.
- 07-17-2010, 12:25 PM #2
Member
- Join Date
- Jul 2010
- Location
- IL
- Posts
- 7
- Rep Power
- 0
what seems to fail?
can you post the error msgs?
- 07-17-2010, 12:45 PM #3
Instead of using the JFrame as the canvas to draw on, use a JPanel that is added to the JFrame. Override the JPanel's paintComponent() method and put your drawing code there.
Search on this forum for code samples of paintComponent.
- 07-17-2010, 12:53 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Hi, gold.dudu...the error message is :
class SmileyFace is public,should be decleared in a file named SmileyFace.java
- 07-17-2010, 12:58 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Hi Norm, I would try that you have suggested me, but in the case I wrote the code like this is there anything wrong, can you tell is it the way correct of calling class SmileyFace from the main class or have done it right, or I should call the first one in some other way...
- 07-17-2010, 01:12 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,393
- Blog Entries
- 7
- Rep Power
- 17
Obviously your current code is incorrect (otherwise it would've run and you wouldn't be here). Lucky for you your code isn't cast in stone so are able to change it; write the following (public) classes:
compile it and run your Application class ...Java Code:public class SmileyPanel extends JPanel { public void paintComponent(Graphics g) { // paint a smiley face here } } public class SmileyFrame extends JFrame { // add a new SmileyPanel to the contentPane here } public class Application { // this class contains the main method // and instantiates a SmileyFrame }
kind regards,
Jos
- 07-17-2010, 01:24 PM #7
Member
- Join Date
- Jul 2010
- Location
- IL
- Posts
- 7
- Rep Power
- 0
- 07-20-2010, 05:32 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM -
How can I call method from class in other class??
By Hisham in forum New To JavaReplies: 6Last Post: 02-14-2010, 03:49 PM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks