1

I'm trying to make a Weather applet for school but I'm having a problem calling a class to a main class. In the end I will want 3 permanent components (Location, Temperature, Precipitation) then in image box I want to do an if statement that pics the appropriate image from the data in components.

Layout idea

Main Class code

// The "Weather" class.
import java.applet.*;
import javax.swing.*;
import java.awt.*;

public class Weather extends Applet
{
//TempBar Intergers
    //int x= 
    //int y=
    //int H=    //H = Heat

    //PercipBar Intergers
    //int x2=
    //int y2=
    //int P =   //P = pericipitation

    public void init ()
    {
       GridLayout umm = new GridLayout(0,2);
       PercipBar percip = new PercipBar();
       getContentPane.addItem (percip());
    } 

    public void paint (Graphics g)
    {
    } 
} 

PercipBar Code

import java.awt.*;
import java.applet.*;

public class PercipBar extends Applet 
{
     int x2 =2;
     int y2 =2;

     int P =80;//P = percipitation will be declared in main file

    public void paint (Graphics g)
    {
        g.setColor (Color.black);
        g.drawRect (x2, y2, 100, 20);//outline of bar
        g.setColor (Color.blue);
        g.fillRect (x2+1, y2+4, P, 14 ); //indicator bar (+4 puts space beetween outline bar)
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"make a Weather applet for school"* 1) Please refer the teacher to [Why CS teachers should *stop* teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Same deal with AWT vs. Swing. See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. A `JLabel` can display an image easily, while an AWT based `Label` supports only text. – Andrew Thompson Jan 16 '14 at 04:07

1 Answers1

0

That GUI seems well suited to being contained in a BorderLayout.

  • location gui would be put in position BorderLayout.PAGE_START
  • image would be put in BorderLayout.CENTER
  • temp would be put in BorderLayout.LINE_END
  • percip (which is spelled precip) would be put in position BorderLayout.PAGE_END

Note that the precipitation bar should net be another applet! It should just be a Component.

In order to be assigned space in the BorderLayout, a custom painted component will need to return a sensible preferred size.

Preferably, rather than custom paint such things, just use a Label to display the text. That way, we don't need to override either the paint method or getPreferredSize. The GUI will takes its hints from the natural size of the label.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433