Solved

Java Swing/AWT (2 Simple questions)

Posted on 2006-06-15
3
202 Views
Last Modified: 2013-11-23
Okay, I have these two source files:

public class SwingApp2
{
      public static void main(String[] args)
      {
            SwingApp2Form Form = new SwingApp2Form("Test Project",280,400);
      }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import javax.swing.*;

public class SwingApp2Form extends JFrame
{
      SwingApp2Form(String Title,int Width,int Height)
      {
            super(Title);
            setSize(Width,Height);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container App = getContentPane(); //Get content pane
            JPanel DrawPanel = new JPanel()
            {
                  public void paintComponent(Graphics Draw)
                  {
                        super.paintComponent(Draw);
                        Draw.setColor(Color.GREEN);
                        Draw.drawOval(0,0,getWidth()-1,getHeight()-1);
                  }
            };
            App.add(DrawPanel,BorderLayout.CENTER);
            setVisible(true);
      }
}

My first question is, why am I using two files for this?  From what I read it says I should use a separate class file
for this, I don't understand why since there is very little I can actually change from the main class..  I don't see in
which way this class is reusable, and if not reusable I don't understand the point of separating it.

My second question is, how do I fill the oval so that it is no longer just an outline?
0
Comment
Question by:List244
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 16916257
yyou need to use a different source file for each public class.
You could do it in one source file (by making your form not public) but then the form class would not be able to be used by other classes.

import java.awt.*;
import javax.swing.*;

public class SwingApp2
{
     public static void main(String[] args)
     {
          SwingApp2Form Form = new SwingApp2Form("Test Project",280,400);
     }
}


class SwingApp2Form extends JFrame
{
     SwingApp2Form(String Title,int Width,int Height)
     {
          super(Title);
          setSize(Width,Height);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container App = getContentPane(); //Get content pane
          JPanel DrawPanel = new JPanel()
          {
               public void paintComponent(Graphics Draw)
               {
                    super.paintComponent(Draw);
                    Draw.setColor(Color.GREEN);
                    Draw.drawOval(0,0,getWidth()-1,getHeight()-1);
               }
          };
          App.add(DrawPanel,BorderLayout.CENTER);
          setVisible(true);
     }
}

to fill the oval change:

                   Draw.drawOval(0,0,getWidth()-1,getHeight()-1);

to

                   Draw.fillOval(0,0,getWidth()-1,getHeight()-1);
0
 
LVL 8

Author Comment

by:List244
ID: 16916294
Objects, thank you for the fillOval, however, I am still slightly confused on the first part of the question.
I know how I can put it into one application, I can do that just fine.  I am more wondering why it is suggested
that you use multiple classes.  Where is the advantage in this?  As you can see above, I am able to change the
title,width, and height of my application.  I could also implement some things such as color-schemes.  But outside
of this, if I were to complete this into say a calculator.  Where would the need for reuse be?
0
 
LVL 92

Expert Comment

by:objects
ID: 16916374
In your particular case (as its small) theres not a lot of advantages.
But as your applications get larger the opportunity for reuse increase, and also keeping all the code in one file becomes unwieldy.
You'd also have to recompile the entire app whenver you made any change.

to put in one class you could change it to this:

import java.awt.*;
import javax.swing.*;

public class SwingApp2Form extends JFrame
{
     public static void main(String[] args)
     {
          SwingApp2Form Form = new SwingApp2Form("Test Project",280,400);
     }

     SwingApp2Form(String Title,int Width,int Height)
     {
          super(Title);
          setSize(Width,Height);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container App = getContentPane(); //Get content pane
          JPanel DrawPanel = new JPanel()
          {
               public void paintComponent(Graphics Draw)
               {
                    super.paintComponent(Draw);
                    Draw.setColor(Color.GREEN);
                    Draw.drawOval(0,0,getWidth()-1,getHeight()-1);
               }
          };
          App.add(DrawPanel,BorderLayout.CENTER);
          setVisible(true);
     }
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 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