Link to home
Start Free TrialLog in
Avatar of List244
List244

asked on

Java Swing/AWT (2 Simple questions)

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?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of List244
List244

ASKER

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?
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);
     }
}