Solved

Java Swing/AWT (2 Simple questions)

Posted on 2006-06-15
3
197 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

758 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now