d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Repeat/return A Value. > Detail Inside. 2 Second Problem/fix
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 14 2013 02:34pm
Edit: I'm a moron.. :wallbash: lol

I had the x, y values declared at 300, 300 for starting points when my frame was only 300x300 wide. So I realized what I did and redeclared the starting values (points) to x=1, y=1 or (1, 1, 300, 300) so that way the filled square would start at (1, 1) and continue from there. Derp! So now each time a new value is declared it rewrites over the old drawing and makes it seem as if a new template was used!

SUCESS!!!

The corrected code is at the bottom of the page if you want to see where I screwed up. Took me all day to realize that dumb little mistake! DOH!





Ignore this: lol

So I have this problem with a program I am writing. It using a slider bar GUI to change the depths of a swimming pool. 1-3 feet for the shallow end and 4-10 feet for the deep end. If it draws one value, say 1' shallow x 5' deep, it works fine. The problem is I have to close out the program and re-run it if I want to draw another value because it will overlap. I.E. I will have multiple polygons going throughout my drawing. What code can I write to make it erase the lines or draw over everything with a blank background. Say white.

I have tried fillRect but it won't draw another rectangle everytime a new value is declared. How can I accomplish this?

Code
public void actionPerformed(ActionEvent event) {
    Graphics paper = panel.getGraphics();
     paper.setColor(Color.white);
    paper.fillRect (300, 300, 300, 300);
       panel.setBackground(Color.white);


Here is the entire program. The problem area is around where the paper.drawLine coding is.

Code
/**
* @(#)Pool.java
*
*
* @Anderson, Michael.
* @version 1.00 2013/2/13
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
public class Pool extends JFrame implements
   ActionListener, ChangeListener {
   
   private JButton button;
   private JPanel panel;
   private Random random;
   private JSlider sliderDeep, sliderShallow;
   private JTextField volumeField, shallEnd, depEnd;
   private JLabel shallowLabel, deepLabel, volumeLabel;
   private int width = 50, length = 100;
   //Width and length of the pool.
   private int deepEnd = 4, shallowEnd = 1;
   private int x = 50, y = 50;
   //Where the x and y points starts. Upper, left edge of the pool.
   //private int s1 = 300, s2 = 300;
   
   public static void main (String[] args) {
       Pool frame = new Pool();
       frame.setSize(1200, 500);
       frame.createGUI();
       frame.setVisible(true);
   }

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

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

     
       depEnd = new JTextField(10);
       window.add(depEnd);
       
       shallowLabel = new JLabel("Shallow End:  ");
       window.add(shallowLabel);
       
     
       sliderShallow = new JSlider(JSlider.HORIZONTAL, 1, 3, 1);
       //Shallow end is from 1 to 3 feet deep, starting at 1 foot.
       window.add(sliderShallow);
       sliderShallow.addChangeListener(this);

       deepLabel = new JLabel("Deep End:  ");
       window.add(deepLabel);
       
       shallEnd = new JTextField(10);
       window.add(shallEnd);
       
       
       sliderDeep = new JSlider(JSlider.HORIZONTAL, 4, 10, 4);
       //Deep end is from 4 to 10 feet deep, starting at 4 foot.
       window.add(sliderDeep);
       sliderDeep.addChangeListener(this);
       
       button = new JButton("Draw Pool");
       window.add(button);
       button.addActionListener(this);
       
        volumeLabel = new JLabel("Volume of pool pixels.:  ");
       window.add(volumeLabel);
       
       volumeField = new JTextField(10);
       window.add(volumeField);
               
   }
  /*
private void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
paper.fillRect (300, 300, s1, s2);
       panel.setBackground(Color.white);
}
*/

    public void actionPerformed(ActionEvent event) {
    Graphics paper = panel.getGraphics();
     paper.setColor(Color.white);
    paper.fillRect (300, 300, 300, 300);
       panel.setBackground(Color.white);
    paper.setColor( Color.blue);
       paper.drawLine (x, (y + (shallowEnd * 10)), x + 100, y + (deepEnd * 10));
       //Slope of pool. (50, (50 + (value of shallowEnd * 10)), 50 + 100, 50 + (value of deepEnd * 10)); Returns point A (50,60) and point B (150,90).
       //Yields the slop. The slop of the pool will vary from m=1/10 to m=9/10
       paper.drawLine (x, y, x + 100, y);
       //Horizon of pool. (50, 50, 50 + 100, 50); Point A (50,50) and point B (150, 50). Yields the horizontal line of the pool.
       paper.drawLine (x, y, x, (y + (shallowEnd * 10)));
       //Draws a vertical line for the left side of the pool.
       paper.drawLine (x + 100, y, x + 100, (y + (deepEnd * 10)));
       //Draws a vertical line for the right side of the pool.
     
   }

   public void stateChanged(ChangeEvent e) {
   
        deepEnd = sliderDeep.getValue();
        shallowEnd = sliderShallow.getValue();
       
       double avg = (double)(((double)deepEnd + (double)shallowEnd)/2);
       //Formula given by Mr. Farkas. (Decimal deepEnd + decimal shallowEnd)divided by 2 will yield the average.
       
       double v =  avg  * width * length;
       //Average from the above double (decimal) multiplied by width and length.
     
         
       volumeField.setText(Double.toString(v));
       //Using the variable v to be called by the string. v = Avg * W * L
       
       
     
       shallEnd.setText(Integer.toString(deepEnd));
       //Puts shallow end into correct textfield
       depEnd.setText(Integer.toString(shallowEnd));
       //Puts deep end into correct textfield
       
       //Note all stuff in actionPerformed method could be copied and pasted int stateChanged
       // - leaveing an empty actionPerformed method
       //Left these notes as is.
   }
}


The corrected code!


Was originally written like this:

Code
public void actionPerformed(ActionEvent event) {
    Graphics paper = panel.getGraphics();
     paper.setColor(Color.white);
    paper.fillRect (300, 300, 300, 300);
       panel.setBackground(Color.white);
    paper.setColor( Color.blue);
       paper.drawLine (x, (y + (shallowEnd * 10)), x + 100, y + (deepEnd * 10));
       //Slope of pool. (50, (50 + (value of shallowEnd * 10)), 50 + 100, 50 + (value of deepEnd * 10)); Returns point A (50,60) and point B (150,90).
       //Yields the slop. The slop of the pool will vary from m=1/10 to m=9/10
       paper.drawLine (x, y, x + 100, y);
       //Horizon of pool. (50, 50, 50 + 100, 50); Point A (50,50) and point B (150, 50). Yields the horizontal line of the pool.
       paper.drawLine (x, y, x, (y + (shallowEnd * 10)));
       //Draws a vertical line for the left side of the pool.
       paper.drawLine (x + 100, y, x + 100, (y + (deepEnd * 10)));
       //Draws a vertical line for the right side of the pool.      
   }


Should be written as this: paper.fillRect (1, 1, 300, 300);

Code
public void actionPerformed(ActionEvent event) {
    Graphics paper = panel.getGraphics();
     paper.setColor(Color.white);
    paper.fillRect (1, 1, 300, 300);
       panel.setBackground(Color.white);
    paper.setColor( Color.blue);
       paper.drawLine (x, (y + (shallowEnd * 10)), x + 100, y + (deepEnd * 10));
       //Slope of pool. (50, (50 + (value of shallowEnd * 10)), 50 + 100, 50 + (value of deepEnd * 10)); Returns point A (50,60) and point B (150,90).
       //Yields the slop. The slop of the pool will vary from m=1/10 to m=9/10
       paper.drawLine (x, y, x + 100, y);
       //Horizon of pool. (50, 50, 50 + 100, 50); Point A (50,50) and point B (150, 50). Yields the horizontal line of the pool.
       paper.drawLine (x, y, x, (y + (shallowEnd * 10)));
       //Draws a vertical line for the left side of the pool.
       paper.drawLine (x + 100, y, x + 100, (y + (deepEnd * 10)));
       //Draws a vertical line for the right side of the pool.
   }


This post was edited by NinjaSushi2 on Feb 14 2013 02:44pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll