• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making Line in java

 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a line in java by invoking a object of this class from another class.
i will substitute parameter of line with the b;

However,The line doesn't appear.





 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're paint method is never called because it is not part of a component. You should create a class that extends (J)Panel and override the paint method.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply.

The problem with the above code was that I wasnt Inheriting Frame in MyA
..





When i call MyA through B. It runs but no frame appears. When i directly start MyA, it works properly.

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually your line is being drawn. From 0,0, to 0, 200. That is because instance variables are initialized before the constructor runs. And because you haven't assigned a value to b it has its default value of 0.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how to make sure that constructor initializes the values and line is drawn according to it.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initialize the Line in the constructor.
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i define it in constructor its not visible outside.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strangely, Netbeans was showing some error when i was doing that earlier bt not now. But still no frame appears when i run it through MyB.

Frame frame = new MyA(50) - > this constructor overrides over the constructor invoked from MyB
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not really surprising. Just compare what you're doing in MyA.main() and MyB.main()
 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of MyA in MyB invoked, which again calls another contructor of MyA .

The reason i have to initialize frame with MyA;s constructor is to invoke paint method. I cannot use MyA(//without parameter ) . So i wrote that MyA( 50). Now if i replace 50 with "b" - > public element. It says you cannot refer to non static context from a static context so what should i do
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Wadhwani wrote:The constructor of MyA in MyB invoked, which again calls another contructor of MyA .

In your posts there is only 1 constructor.

Gaurav Wadhwani wrote:The reason i have to initialize frame with MyA;s constructor is to invoke paint method.

You're not invoking paint(). The super class does that for you (in this case).

Gaurav Wadhwani wrote:I cannot use MyA(//without parameter ) . So i wrote that MyA( 50). Now if i replace 50 with "b" - > public element. It says you cannot refer to non static context from a static context so what should i do
Sounds like you're trying to access an instance variable without an instance reference. This is not possible.

Just look again at the differences between MyA.main() and MyB.Main(). If you still can't figure it out, post the code that causes the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!