I am getting a cannot find symbol- method occupant(int) at the bottom of the code (line 150) probably missing something simple but cannot fix it. only need help with this part the program is far from incomplete.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Accommodation extends JFrame
implements ActionListener
{
/** Window layout constants (these need to be "static" to be referred to in main) */
private static final int panelWidth = 700;
private static final int panelHeight = 300;
/** The apartment block data structure */
private final int apartments = 10; // Total number of apartments (numbered from 1 for the user, but from 0 in the array!)
private String[] occupant; // Occupant information: one string per apartment
private int selectedApartment; // To identify the currently selected apartment
/** Configuration constants for the display */
private final int aptHeight = 40; // Dimensions for individual apartment rectangles
private final int aptWidth = 50;
/** A useful colour */
private final Color emptyColor = Color.green;
/** Graphical interface components */
private JButton next; // Dummy selection button
/** The drawing panel. */
private JPanel panel;
/**
* The main program launcher for the Accommodation application.
*
* @param args The command line arguments (ignored here).
*/
public static void main( String[] args )
{
Accommodation frame = new Accommodation();
frame.setSize( panelWidth+40, panelHeight+200 );
frame.setUpAccommodation();
frame.setUpGUI();
frame.setVisible( true );
frame.setTitle("Accomodation");
frame.setLocationRelativeTo(null);
} // End of main
/** Organizes overall set up of the apartment block details at launch time. */
private void setUpAccommodation()
{
// Create the occupant array and fill in first two apartments.
// The other elements need to contain "Empty"
occupant = new String[apartments];
occupant[0] = "Luke Skywalker"; // Occupants in apartments 1 and 2
occupant[1] = "The Hulk";
occupant[2] = "Empty";
occupant[3] = "Empty";
occupant[4] = "Empty";
occupant[5] = "Empty";
occupant[6] = "Iron Man";
occupant[7] = "Empty";
occupant[8] = "Empty";
occupant[9] = "Empty";
selectedApartment = 0; // Apartment 1 initially selected
} // End of setUpAccommodation
/** Organizes overall set up of the GUI at launch time. */
private void setUpGUI()
{
// Now set up the initial GUI: window, button, graphics panel and background colours.
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container window = getContentPane();
window.setLayout( new FlowLayout() );
window.setBackground( Color.gray );
// Set up the next button
next = new JButton("Next");
window.add(next);
next.addActionListener(this);
// Set up the drawing panel
panel = new JPanel()
{
// paintComponent is called automatically when a screen refresh is needed
public void paintComponent(Graphics g)
{
// g is a cleared panel area
super.paintComponent(g); // Paint the panel's background
paintScreen(g); // Then the required graphics
}
};
panel.setPreferredSize( new Dimension( panelWidth, panelHeight ) );
panel.setBackground( Color.white );
window.add( panel );
} // End of setUpGUI
/**
* Handle button clicks
*
* @param e Information about the button click
*/
public void actionPerformed( ActionEvent e )
{
// In this template there is only a Next button.
// In the second stage (up to 69 marks) it should causes the display to be updated
// to show the details of the next occupant of the apartment block.
// Here is an indication of what it should do:
// Adjust the selected apartment indicator
selectedApartment++;
// And refresh the display
repaint();
} // End of actionPerformed
/**
* Display the apartment block, and the details of the occupant of the selected apartment.
*
* @param g The Graphics area to be drawn on, already cleared.
*/
private void paintScreen( Graphics g )
{
// Now draw the display
displayStatus(g);
displayBlock(g);
} // End of paintScreen
/** Display information about the currently selected apartment */
private void displayStatus(Graphics g)
{
// Just information about apartment 1 in this template
g.setColor(Color.black);
g.drawString(occupant[0], 190, 70);
} // End of displayStatus
/** Draw the apartments themselves: boundary, empty/occupied colouring, number */
private void displayBlock(Graphics g)
{
for(int i = 0; i < 10; i++)
{
if (occupant(i).equals("Empty"))
{
g.setColor(Color.green);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight); //Sets the rectangle red to show the room is occupied
}
else
{
g.setColor(Color.red);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight); //Sets the rectangle green to show the room is free
}
g.setColor(Color.black);
g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
// Draws the numbers in the Corresponding boxes
g.setColor(Color.black);
g.drawString(Integer.toString(i+1), 150+(i*50)+15, 120+25);
}
//Drawing the key allowing the user to see what the colours refer to
g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
g.drawString("Key", 20,120);
g.drawString("Occupied",60,150);
g.drawString("Empty", 60, 170);
// Drawing the boxes to show what the colours mean in the key
g.setColor(Color.red);
g.fillRect(20, 130, 25, 25);
g.setColor(Color.black);
g.drawRect(20, 130, 25, 25);
g.setColor(Color.green);
g.fillRect(20,150,25,25);
g.setColor(Color.black);
g.drawRect(20,150,25,25);
} // End of displayBlock
} // End of Accommodation
Aucun commentaire:
Enregistrer un commentaire