Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Interface Question

Posted on 1998-05-19
7
Medium Priority
?
249 Views
Last Modified: 2013-11-23
I am trying to organize the code in one of my applets so that the initial graphics screen generation is done in a separate class from the "main" class. Here is a simple example of my problem. I am trying to get the Hammer class to draw a label on the screen - but it will not. If I put the new_label code in the "main" classTest class it works fine. I think I am missing some simple fundamental concept here.

import java.applet.Applet;

public class classTest extends Applet
{
    public void init()
    {
        Hammer h = new Hammer();
    }

}
class Hammer extends Frame
{
    Hammer()
    {  
        java.awt.Label new_label;
       
            setLayout(null);
              new_label = new java.awt.Label("New Label");
            new_label.reshape(5,5,100,18);
            add(new_label);
            }
}

Thanks
boones
0
Comment
Question by:boones
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 1000 total points
ID: 1221242
You forgot one small thing. You have to add statement

h.setVisible(true);

in the init () method after creating hammer object.


0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1221243
Did you try doing a h.setVisible(true) in the init ?
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221244
May be you need to try h.setVisible(true) in start() method of applet not in init. I am sorry for that error.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:aziz061097
ID: 1221245
The following skeleton is something you can use if you want a solution with Interfaces.

public interface ITestInterface
{
      public abstract void
              Method1 (
 
                  );
      public abstract void
            Method2(
                  );
}




public class TestFrame extends Frame implements ITestInterface
 {
//Implement your UI here


  public void Method1 (){
                       }
 

  public void Method2 (){
                       }
}



public class LogicTest {
      public LogicTest(ITestInterface callingFrame ){
      _callingFrame=callingFrame ;

      _callingFrame.Method1();

}
}
0
 

Author Comment

by:boones
ID: 1221246
Evijay,

Your answer does make the code work but not the way I expected. I will give you additional points if you can answer me this. When the code is executed it creates a separate window for my program I do not want this. I do not want a separate window for my applet I want the applet to run in the browser window. How can I tweak my code for this while maintaining the screen initialization calls in the separate class? Thanks.

Aziz, thank you for your comments, they also have been helpful.

Boones
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221247

        import java.applet.Applet;

        public class classTest extends Applet
        {
            public void init()
            {
                Hammer h = new Hammer(this);
            }

        }
        class Hammer
        {
           Applet m_app;
            Hammer(Applet app)
            {    
              m_app = app;            
   java.awt.Label new_label;
                 
       app. setLayout(null);
                new_label = new java.awt.Label("New Label");
        new_label.reshape(5,5,100,18);
        app.add(new_label);
        }
        }

this may require some modification. try it.

0
 

Author Comment

by:boones
ID: 1221248
Adjusted points to 250
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question