[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Swing/AWT (2 Simple questions)

Posted on 2006-06-15
3
Medium Priority
?
214 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 2000 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 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