-1

I'm currently working on a java program that has a JCombo box where users select a shape and that shape is generated randomly multiple times. I'm using the command line to run it and using notepad. I'm on the latest JDK version. How can I modify my code so it compiles on the newer version..?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;

public class Hw1b extends JFrame
{
    public Hw1b()
    {
        super("Hw1b");
        final ComboPanel comboPanel = new ComboPanel();
        String[] shapeItems = {
            "Square", "Oval", "Rectangle", "Circle"
            //"Circle", "Square", "Oval", "Rectangle"
        };

        JComboBox shapeBox = new JComboBox(shapeItems);
        shapeBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                if(e.getStateChange() == ItemEvent.SELECTED)
                {
                    String item = (String)e.getItem();
                    if(item.equals("Square"))
                        comboPanel.makeSquares();
                    if(item.equals("Oval"))
                        comboPanel.makeOvals();
                    if(item.equals("Rectangle"))
                        comboPanel.makeRectangles();
                    if(item.equals("Circle"))
                        comboPanel.makeCircles();
                    /*  
                    if(item.equals("Circle"))
                        comboPanel.makeSquares();
                    if(item.equals("Square"))
                        comboPanel.makeOvals();
                    if(item.equals("Oval"))
                        comboPanel.makeRectangles();
                    if(item.equals("Rectangle"))
                        comboPanel.makeCircles();
*/                      
                }
            }
        });

        //position and set size of Jpanel 
        JPanel southPanel = new JPanel();
        southPanel.add(shapeBox);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(comboPanel, "Center");
        getContentPane().add(southPanel, "South");
        setSize(600,400);
        setLocation(200,200);
        setVisible(true);
    }

    private class ComboPanel extends JPanel
    {
        int w, h;
        Random seed;
        static final int
            OVAL = 0,
            RECT = 1;
        int shapeType = -1;

        public ComboPanel()
        {
            seed = new Random();
            setBackground(Color.white);
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int width = getWidth();
            int height = getHeight();
            int x, y;
            Shape s = null;
            for(int i = 0; i < 20; i++)
            {
                x = seed.nextInt(width - w);
                y = seed.nextInt(height - h);
                switch(shapeType)
                {
                    case OVAL:
                        s = new Ellipse2D.Double(x, y, w, h);
                        break;
                    case RECT:
                        s = new Rectangle2D.Double(x, y, w, h);
                }
                if(shapeType > -1)
                    g2.draw(s);
            }
        }

        public void makeSquares()
        {
            shapeType = RECT;
            w = 50;
            h = 50;
            repaint();
        }

        public void makeOvals()
        {
            shapeType = OVAL;
            w = 80;
            h = 60;
            repaint();
        }

        public void makeRectangles()
        {
            shapeType = RECT;
            w = 80;
            h = 40;
            repaint();
        }

        public void makeCircles()
        {
            shapeType = OVAL;
            w = 75;
            h = 75;
            repaint();
        }
    }

    public static void main(String[] args)
    {
        new Hw1b();
    }
}
Holger
  • 285,553
  • 42
  • 434
  • 765
  • 1
    What is the error? What version is the old version? – James Wierzba Sep 25 '15 at 18:20
  • Strongly suggest you use an actual IDE... Eclipse, NetBeans, IDEA, JDeveloper ... anything would be better than notepad. Even vim would give you syntax highlighting! – dcsohl Sep 25 '15 at 18:22
  • 1
    “Unchecked or unsafe operations” is only a *warning*. Your code should still work. The reason is that since Java 7, certain Swing classes, like [`JComboBox`](http://docs.oracle.com/javase/7/docs/api/?javax/swing/JComboBox.html) are generic and like to have type parameters. So you may change `JComboBox shapeBox = new JComboBox(shapeItems);` to `JComboBox shapeBox = new JComboBox<>(shapeItems);` to declare that it will contain `String`s only… – Holger Sep 25 '15 at 18:25
  • The error is the -Xlint – SkywardSword Sep 25 '15 at 19:37
  • Okay I ran it using Xlint:unchecked and now it's shows 'code' unchecked call to JComboBox(E[]) as a memer of the raw type JComboBox – SkywardSword Sep 25 '15 at 19:48

1 Answers1

0

The JComboBox is a "raw" type, which means you need to provide the actual type it is going to use. The message you are concerned with is not an error, it rather tries to draw your attention to this line:

JComboBox shapeBox = new JComboBox(shapeItems);

In order to make your code type-safe you would need to explicitly say that your JComboBox is going to work with Strings:

JComboBox<String> shapeBox = new JComboBox<>(shapeItems);

I would recommend to go through this post in case you wish to know more about raw types and generics.

Community
  • 1
  • 1
Sva.Mu
  • 1,141
  • 1
  • 14
  • 24
  • OK, it seems it took me too long to finish my answer - meanwhile it was elaborated very well directly in the answer's comments :-) – Sva.Mu Sep 25 '15 at 19:58
  • This website is full of douchebags....How is this question off topic? Thank you for the answer, it helped with my issue since I can now see the errors – SkywardSword Sep 25 '15 at 21:25