Solved

Java Swing/AWT (2 Simple questions)

Posted on 2006-06-15
3
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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