Results 1 to 3 of 3
  1. #1
    reddy is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default drawing the cross section of a pool using user input

    Hey all,

    Just starting to learn Java, and I've hit a roadblock on a should be simple exercise in my book:

    "Write a program which draws a cross-section of a swimming pool and which calculates and displays the volume of the pool given its dimensions. The width of the pool is fixed at 5 meters and the length is fixed at 20 meters.
    The user of this program controls the depth of the shallow and deep end of the pool by using input dialog boxes. Each time the the user enters the depth of the shallow or deep end of the pool your program should draw the current shape of the pool. Create two new methods: one to draw the swimming pool, and one to calculate the volume."

    so far I have

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class PoolXSection extends JFrame
    implements ActionListener {


    private JButton button;
    private JPanel panel;

    public static void main(String[] args) {
    PoolXSection frame = new PoolXSection();
    frame.setSize(350, 350);
    frame.createGUI();
    frame.setVisible(true);
    }


    private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout() );


    panel = new JPanel();
    panel.setPreferredSize(new Dimension(350,275));
    panel.setBackground(Color.white);
    window.add(panel);

    button = new JButton("Press me");
    window.add(button);
    button.addActionListener(this);
    }


    public void actionPerformed(ActionEvent event) {


    int shallow;
    int deep;
    int length = 20;
    int width = 5;
    double volume;
    String deepString;
    String shallowString;

    deepString = JOptionPane.showInputDialog(
    "Enter deep end depth");
    deep = Integer.parseInt(deepString);

    shallowString = JOptionPane.showInputDialog(
    "Enter shallow end depth");
    shallow = Integer.parseInt(shallowString);



    volume = length * width * (shallow + deep)*0.5;
    JOptionPane.showMessageDialog(null, "The volume of the pool is " + volumeCalc
    + "\n" + "shallow end: " + shallow
    + "\n" + "deep end: " + deep);

    }

    }

    and I know the two methods should look like this:

    private double calculateVolume(int length, int width, int deep, int shallow)

    private void drawPool(Graphics g, int x, int y, int deep, int shallow, int length)

    I have gotten as far as calculating the volume of the pool, but I'm completely lost as to how to use integer values from the public void actionPerformed(ActionEvent event) method in another method to draw the graphic.

    Any help would be appreciated!


    Thanks
    reddy

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    Quote Originally Posted by reddy View Post
    I have gotten as far as calculating the volume of the pool, but I'm completely lost as to how to use integer values from the public void actionPerformed(ActionEvent event) method in another method to draw the graphic.
    Any variables that represent the 'state' of an object should be instance fields, so that they may be accessed by any method.

    Custom painting is performed in a painting method override. The method to override for Swing components is always, unless you have a very good reason to do otherwise, paintComponent(...)
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    And if you really want to get help with your code, edit your post to include the code tags. [code]CODE[/code] will display as
    Java Code:
    CODE
    db

Similar Threads

  1. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  2. Replies: 0
    Last Post: 04-20-2010, 08:28 AM
  3. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  4. Getting User Input to Drawing Shapes
    By mlfatty in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2009, 06:36 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •