For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2004 > handeling events









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author handeling events
Triff

2004-12-29, 8:58 pm

I'm quite bad at explaining my problems.....but here goes!

package harmonics;
import java.awt.BorderLayout;
import javax.swing.*;

public class MainProgram
{
public static JPanel jpanNorth, jpanSouth;

public static void main(String args[])
{
JFrame jfrmMain = new JFrame("Harmonics Program");
jpanNorth = new JPanel();
jpanSouth = new JPanel();

Producer sp = new Producer(jpanSouth);

jfrmMain.getContentPane().add(jpanNorth, BorderLayout.NORTH);
jfrmMain.getContentPane().add(jpanSouth, BorderLayout.SOUTH);
jfrmMain.setSize(500,500);
jfrmMain. setDefaultCloseOperation(WindowConstants
.EXIT_ON_CLOSE);
jfrmMain.setVisible(true);
}
}



package harmonics;

import javax.swing.JPanel;
import javax.swing.JSlider;

public class Producer
{
public static JSlider sliders[];

public Producer(JPanel pan)
{
sliders = new JSlider[10];

SliderChanger sliderMoved = new SliderChanger();

for(int i = 0; i < sliders.length; i++)
{
sliders[i] = new JSlider(1,0,100,10);
sliders[i].addChangeListener(sliderMoved);
pan.add(sliders[i]);
};
}
public static int[] getSliderValues()
{
int sliderValues[];
sliderValues = new int[10];
for(int i = 0; i < sliders.length; i++)
{
sliderValues[i] = sliders[i].getValue();
};
return sliderValues;
}
}

package harmonics;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderChanger implements ChangeListener
{
public Points p;
public void stateChanged(ChangeEvent e)
{

int sliderValues[] = null;
for(int i= 0; i < 10;i++)
{
sliderValues = new int[10];
sliderValues = Producer.getSliderValues();
};
p = new Points(sliderValues);
p.printSliderValues();
}
}


package harmonics;

public class Points
{
private int SliderValues[];
private double ScaledSliderValues[];
private double AllHarmonicValues[][];
private double NewWaveValues[];
public Points(int vals[])
{
SliderValues = new int[10];
System.arraycopy(vals, 0, SliderValues, 0, 10);
}
public void printSliderValues()
{
for(int i= 0; i < 10;i++)
{
System.out.print(SliderValues[i] + " ");
};
System.out.print("\n");
}
}






Right so there is the code, and here is my problem. As you can see I can
handle the slider event, but I want to have another class called consumer
which recieves a points object and does stuff with it, how would I do that?
Cheers for any help
Triff



ByteCoder

2004-12-29, 8:58 pm

Triff wrote:
> I'm quite bad at explaining my problems.....but here goes!
>

[snip code]
> Right so there is the code, and here is my problem. As you can see I can
> handle the slider event, but I want to have another class called consumer
> which recieves a points object and does stuff with it, how would I do that?
> Cheers for any help
> Triff


You could just do this:
new Consumer(point);

Make sure the Consumer has a constructor which accepts a point object.

--
-------------
- ByteCoder - ...I see stupid people
-------------
Curiosity *Skilled* the cat
Fahd Shariff

2004-12-29, 8:58 pm

You could either create a new consumer instance every time.

Consumer c = new Consumer() ;
c.doStuff(points) ;

or maybe just have a global consumer:

consumer.doStuff(points) ;

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com