Reputation: 69
Hey im trying to make a weather applet but i dont understand j panels every single time the one overlays the other heres what i want it to look like in the end
After i get this part of code i will be making an if else satement that draws an image dependent on "Season"
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class Test21 extends JApplet
{
JPanel jPanel1, jPanel2;
JButton Enter , Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
/** Initializes the applet Test02 */
public void init ()
{
jPanel2 = new DrawingPanel ();
getContentPane ().setLayout (null);
//);
getContentPane ().add (jPanel2);
jPanel2.setBounds (20, 20, 320, 240);
jPanel1 = new DrawingPanel();
getContentPane ().setLayout (null);
jPanel1.setBackground (new java.awt.Color (255, 255, 255));
//);
getContentPane ().add (jPanel1);
jPanel1.setBounds (20, 20, 320, 240);
getContentPane() .setLayout (new FlowLayout ());
Enter =new JButton ("Enter");
Exit =new JButton ("exit");
city = new JLabel ("What city?");
location = new JTextField (20); //location entry field
Seasons = new JComboBox ();
Seasons.addItem ("Summer");
Seasons.addItem ("Fall");
Seasons.addItem ("Winter");
Seasons.addItem ("Spring");
time = new JRadioButton ("check if night?");
getContentPane().add (city);
getContentPane().add (location);
getContentPane().add (Seasons);
getContentPane().add (time);
}
public void paintComponent (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine (10, 70, 70, 10);
}
private class DrawingPanel extends javax.swing.JPanel
{
public void paint (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
drawBars (g);
}
}
public void drawBars (Graphics g)
{
//Precipitation Bar
g.setColor (Color.black);
g.drawRect (40, 170, 100, 20); //outline of bar
g.setColor (Color.blue);
g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
//Temparature Bar
g.setColor (Color.red);
g.fillRect (170 + 4, 50, 14, 100); //Covers in
g.setColor (Color.black);
g.drawRect (170, 50, 20, 100); //outline of bar
g.setColor (Color.white);
g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}
}
this is a big mark in my class please dont give me the why teachers shouldn't teach CS or any of that it dosn't help me i use Ready java while programming also so if you could try to keep same packets i'm very inexperienced and lost but i need this done by monday and have been working on it for a long time
ok so im trying a new file with border layout andill create the classes bellow and try to call them i guess?
public class Final
{
JPanel jPanel1, jPanel2;
JButton Enter, Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
public void init ()
{
JPanel contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.add (locationGui, BorderLayout.NORTH);
contentPane.add (temp, BorderLayout.EAST);
contentPane.add (Percip (g), BorderLayout.SOUTH);
contentPane.add (image, BorderLayout.CENTER);
}
public void drawPercip (Graphics g)
{
//Precipitation Bar
g.setColor (Color.black);
g.drawRect (40, 170, 100, 20); //outline of bar
g.setColor (Color.blue);
g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}
}
This is now the code im working with i got the location gui parts in but when i trydoing tlabel = new DrawingPanel(); it says it cant be done so how would i bring that graphic in? and im pretty sure ill need to be using actionlistener so ill have to add it in with implements a bit later
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class Final2 extends JFrame
{
//Panels
JPanel locationGui = new JPanel ();
JPanel temp = new JPanel ();
JPanel percip = new JPanel ();
JPanel image = new JPanel ();
//Location gui components
JButton Enter, Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
//bar # genertor
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
public Final2 ()
{
init ();
}
public void init ()
{
Font font = new Font ("impact", Font.PLAIN, 20);
//________________________________________________new panel____________________
locationGui.setBackground (Color.RED);
JLabel guiLabel = new JLabel ("");
guiLabel.setFont (font);
Enter = new JButton ("Enter");
Exit = new JButton ("exit");
city = new JLabel ("What city?");
location = new JTextField (20); //location entry field
Seasons = new JComboBox ();
Seasons.addItem ("Summer");
Seasons.addItem ("Fall");
Seasons.addItem ("Winter");
Seasons.addItem ("Spring");
time = new JRadioButton ("check if night?");
locationGui.add (city);
locationGui.add (location);
locationGui.add (Seasons);
locationGui.add (time);
locationGui.add (guiLabel);
//________________________________________________new panel____________________
temp.setBackground (Color.BLUE);
temp.setLayout (new GridBagLayout ());
JLabel tempLabel = new JLabel ("Temp");
tempLabel.setFont (font);
temp.add (tempLabel);
//________________________________________________new panel____________________
percip.setBackground (Color.GREEN);
JLabel pLabel = new JLabel ("Percip");
pLabel.setFont (font);
percip.add (pLabel);
//________________________________________________new panel____________________
image.setBackground (Color.ORANGE);
image.setLayout (new GridBagLayout ());
JLabel imageLabel = new JLabel ("Image");
imageLabel.setFont (font);
image.add (imageLabel);
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.add (locationGui, BorderLayout.NORTH);
contentPane.add (temp, BorderLayout.EAST);
contentPane.add (percip, BorderLayout.SOUTH);
contentPane.add (image, BorderLayout.CENTER);
setContentPane (contentPane);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setSize (400, 400);
setLocationRelativeTo (null);
setVisible (true);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
new Final2 ();
}
}
);
}
private class DrawingPanel extends javax.swing.JPanel
{
public void paint (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
drawPercip (g);
}
}
public void drawPercip (Graphics g)
{
//Precipitation Bar
g.setColor (Color.black);
g.drawRect (40, 170, 100, 20); //outline of bar
g.setColor (Color.blue);
g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}
public void drawTemp (Graphics g)
{
//Temparature Bar
g.setColor (Color.red);
g.fillRect (170 + 4, 50, 14, 100); //Covers in
g.setColor (Color.black);
g.drawRect (170, 50, 20, 100); //outline of bar
g.setColor (Color.white);
g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}
}
Upvotes: 0
Views: 111
Reputation: 209092
JApplet
has no paintComponent
method, so you're not actually overriding anything. As good practice, you should use the @Override
annotation to make cure you are making a valid override. JApplet
does have a paint
method though.paint
in a JPanel
. Instead override paintComponent
and call super.paintComponent
UPDATE for your new post
Container
. You can make it = a JPanel
See the example below to see how BorderLayout
would work for your Final
class.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Final extends JFrame{
JPanel locationGui = new JPanel();
JPanel temp = new JPanel();
JPanel percip= new JPanel();
JPanel image = new JPanel();
public Final() {
init();
}
public void init() {
Font font = new Font("impact", Font.PLAIN, 20);
locationGui.setBackground(Color.RED);
JLabel guiLabel = new JLabel("Location Gui");
guiLabel.setFont(font);
locationGui.add(guiLabel);
temp.setBackground(Color.BLUE);
temp.setLayout(new GridBagLayout());
JLabel tempLabel = new JLabel("Temp");
tempLabel.setFont(font);
temp.add(tempLabel);
percip.setBackground(Color.GREEN);
JLabel gLabel = new JLabel("Percip");
gLabel.setFont(font);
percip.add(gLabel);
image.setBackground(Color.ORANGE);
image.setLayout(new GridBagLayout());
JLabel imageLabel = new JLabel("Image");
imageLabel.setFont(font);
image.add(imageLabel);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(locationGui, BorderLayout.NORTH);
contentPane.add(temp, BorderLayout.EAST);
contentPane.add(percip, BorderLayout.SOUTH);
contentPane.add(image, BorderLayout.CENTER);
setContentPane(contentPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new Final();
}
});
}
}
temp = new DrawingPanel()
, it works, but there are two problems
BorderLayout
of the frame. To fix that, override the getPreferredSize
of the DrawingPanel
fillRect
doesn't show because it's the same color as the background.Here are the fixes to your code.
private class DrawingPanel extends javax.swing.JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawPercip(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
}
public void drawPercip(Graphics g) {
g.setColor(Color.black);
g.drawRect(40, 170, 100, 20);
g.setColor(Color.blue);
g.fillRect(40 + 1, 170 + 4, P, 14);
}
Upvotes: 3
Reputation: 42174
The image you posted can be achieved using a BorderLayout. Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
JPanel contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(locationGui, BorderLayout.NORTH);
contentPane.add(temp, BorderLayout.EAST);
contentPane.add(percip, BorderLayout.SOUTH);
contentPane.add(image, BorderLayout.CENTER);
Upvotes: 1