0

Im trying to draw some bars that have random intergers into the java applet bellow but the overrides have an error im new to java i tried to makr the two areas in code the draw method is at the very bottom the other is the 2nd panel area What im aiming for

https://i.stack.imgur.com/oRdi4.jpg

sizes x,y not coordinates

-total size = 200,250

-location gui =200,50

-image = 140,150

-precip = 100,20

-temp = 20,100

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 DrawingPanel (); //where it should be

        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 {//area i have issues with atm
    @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)
    {
        //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)
    }




}

my current Errors (4) are happening in this block of code at the @override the only message provided are "illegal token" and "unexpected symbols ignored" idk why it doesn't give more information

 private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        drawPercip(g);
    }
    @Override 
    public Dimension getPreferredSize() {
        return new Dimension(200, 400);
    }
  • If you get errors, you should post them. Note that `paintComponent` should be protected, but this shouldn't be giving problems. – Hovercraft Full Of Eels Jan 24 '14 at 02:25
  • im an amateur so idk what protected means and the errors are "illegal token"and "unexpected symbols ignored" im using Ready java as a compiler – Curtis Spence Jan 24 '14 at 02:27
  • 1) don't paraphrase your error messages. Instead post the entire message as an edit to your question. Indicate in your code where the error is occurring. – Hovercraft Full Of Eels Jan 24 '14 at 02:28
  • That isnt paraphrasing, it is the legitamate message on @override illegal token shows on the @ and rest on override – Curtis Spence Jan 24 '14 at 02:30
  • @CurtisSpence I think what Hovercraft is trying to say is, the compiler will output more than just the error, it will output the location of the error and normally provides some additional information that might be helpful in diagnosing the problem – MadProgrammer Jan 24 '14 at 02:42
  • 1
    What level of API support does "Ready" Java Compiler support. It may not support annotations... – MadProgrammer Jan 24 '14 at 03:17
  • this is the site idk what API is http://compsci.ca/holtsoft/ – Curtis Spence Jan 24 '14 at 19:32

1 Answers1

2

First I seen no applet,

Second, DrawingPanel is not a JLabel

JLabel pLabel = new DrawingPanel(); //where it should be

Change the pLable type to JComponent or JPanel or DrawingPanel depending on what you want to be able to access out of each class.

Third, you should not rely on magic numbers

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)
}

There is no guarantee that the height and width of the panel is what you think it might be. Make use of the getWidth and getHeight methods

Fourth, if you intend to have more the one DrawingPanel, you should make

int P = rand.nextInt(100) + 1; //Random Precipitation
int H = rand.nextInt(50) + 1; //Random Heat

Part of the DrawingPanel.

You should also make the time to read through Code Conventions for the Java Programming Language

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i dont quite understand what you're saying but i added more info revolving the finale layout idea and the sizes im after for each item – Curtis Spence Jan 24 '14 at 02:51
  • What part don't you understand? – MadProgrammer Jan 24 '14 at 02:54
  • 1) you said to change plabel to DrawingPanel which it already is 2) i have neverheard of "magic numbers" – Curtis Spence Jan 24 '14 at 02:56
  • @CurtisSpence 1. No, `pLabel` is defined, explicity as a `JLabel`, `JLabel pLabel = new DrawingPanel(); //where it should be`, `JLabel` and `DrawingPanel` are two different types of objects, they can not be assigned to each other. 2. There is no way you can guarantee the size of the `DrawingPanel`. The layout manager may need to give more or less room to other components within your layout, changing the size of the `DrawingPanel`, meaning when it's painted, it won't paint properly. – MadProgrammer Jan 24 '14 at 02:59
  • so JLabel pLabel = new DrawingPanel(); should acutaly be DrawingPanel plabel = new DrawingPanel(); – Curtis Spence Jan 24 '14 at 03:03
  • @CurtisSpence That would be my guess – MadProgrammer Jan 24 '14 at 03:04