Re: Action/Change Listeners
Create a jar file with NetBeans that includes your source code, then change the file extension from jar to zip and then you can post it. Better still, create and post an SSCCE.
Re: Action/Change Listeners
How do you complile a program to a jarfile using netbeans including the source code... I've never had to do that before :/
Re: Action/Change Listeners
The help files are quite useful (as always). Search on "building a jar file" or something similar. You will need to read section in this tutorial on how to include the .java files in the jar as well.
Re: Action/Change Listeners
Because I use winRar to compress my files , It wont let me compress it to a normal Zip file... I will be able to e-mail if anyone would kindly take a look at it?
Re: Action/Change Listeners
If you can create a jar file, you've already got a zip file since jar uses zip as its underlying structure. Again, create a jar that contains the source files (again, the NetBeans help files will tell you all you need to know, and as always if you get stuck on a step, ask), and then simply change the jar's extension to zip and upload it here.
1 Attachment(s)
Re: Action/Change Listeners
Ok I think I've uploaded it there... The Jar file is in the dist folder in the main folder... that has all the classes and that involved there
Re: Action/Change Listeners
Some suggestions:
- Give MyControlPanel a public method setShapes(Shapes s) and use it to change the value of its Shapes field (which should not be capitalized -- shapes).
- That field should most definitely *not* be static.
- MyControlPanel's paintComponent method currently is hard coded to draw an oval. Give it an some if and if/else blocks and have it use the value of the shapes variable to decide what to draw. First though have it test that shapes is not null and if it is, draw nothing.
- paintComponent is currently hard-coded to draw an oval of 75 by 75 size. Change this so that it uses the value from the JSlider to decide how big a shape to draw.
- After inside of setShapes(...) call repaint() after setting the shapes field.
- Same for the slider. In its listener call repaint() as its last method.
- Make Shapes an abstract class since it needs to be extended to be useful.
- Get rid of MyFrame's static Shapes variable. There are no need for static variables and certainly no need for MyFrame to even have a Shapes variable.
- In your menu's ActionListener code, call setShapes on your MyControlPanel variable.
Re: Action/Change Listeners
Im confused, I'm proper beginner... right I've started creating the method such as "public void setShapes(Shapes s)" and what not I dont understand what you mean by use it to change the value of its shapes field?
Re: Action/Change Listeners
Quote:
Originally Posted by
Conor-Magee
Im confused, I'm proper beginner... right I've started creating the method such as "public void setShapes(Shapes s)" and what not I dont understand what you mean by use it to change the value of its shapes field?
Have you ever had a class that had "setter" and "getter" methods (also known as accessor and mutator methods)? That's all I'm asking. That you again, make the shapes variable non-static, and then give it a setter method, called setShapes(...) that sets the variable. The method will be just slightly different from other setter methods in that it will have two lines not one: 1) set the instance's shapes variable with the one passed into the method via its parameter (as is typical of most all setter methods), and 2) call repaint(). That's it.
For example here's a class with an int variable, foo, and a setFoo and getFoo setter and getter method pair:
Code:
public Class Example {
private int foo;
// setter method
public void setFoo(int foo) {
this.foo = foo;
}
// getter method
public int getFoo() {
return foo;
}
}
Re: Action/Change Listeners
No sorry, Ive never heard of the terms setter and getter methods :/
So at the top I will have: "Shapes shape;"
And I will have the method:
setShapes(Shapes s)
{
Shapes s = shape;
repaint();
}
Re: Action/Change Listeners
Quote:
Originally Posted by
Conor-Magee
No sorry, Ive never heard of the terms setter and getter methods :/
So at the top I will have: "Shapes shape;"
And I will have the method:
setShapes(Shapes s)
{
Shapes s = shape;
repaint();
}
Close but not quite. In your method above you are declaring a Shapes variable, s, inside of the method, and per the rules of scoping, this variable only exists within this method. Once the flow of code leaves this method, s disappears. You must use a setter method to set a class field, a Shapes variable that has been declared in the class, and again that isn't static.
Re: Action/Change Listeners
Code:
public Class MyControlPanel {
private Shapes Shape;
// Set the Shape.
public void setShape(Shapes shape) {
this.shape = shape;
}
// Get the shape.
public void getShape() {
return shape;
}
}
Do I need to use a Getter method in my own code? And more importantly, does that code above look as if im moving in the right direction, I wanna try and get this functioning as I need by tonight or tomorrow morning.
Re: Action/Change Listeners
What I want working firstly though is when I move my slider the numbers of the text boxes go up based on the length and area equations... The most progress I've got is:
Code:
public class MyChangeAction implements ChangeListener
{ @Override
public void stateChanged(ChangeEvent e)
{
int currentValue = slider.getValue();
String val = Integer.toString(currentValue);
area.setText(val);
blength.setText(val);
}
}
This actually works, but the completely wrong way I need it to... When I move the slider the value of both boxes go from 0-100 with the slider... I can see how this happens... Ive tried implementing the following code to bring my equations into it... By cross-referencing my code I uploaded maybe you can help point out where I've went wrong, I think its something silly thats preventing it from working...
Code:
public class MyChangeAction implements ChangeListener
{ @Override
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
if (source.getValueIsAdjusting()) return;
int currentValue = slider.getValue();
MyControlPanel.Shape=MyFrame.Shape;
area.setText(Double.toString(Shape.Area(currentValue)));
blength.setText(Double.toString(Shape.Area(currentValue)));
}
}
Thats the code that doesn't work when entered... Its calling from the shapes class... :S
Re: Action/Change Listeners
Quote:
Originally Posted by
Conor-Magee
Code:
public Class MyControlPanel {
private Shapes Shape;
// Set the Shape.
public void setShape(Shapes shape) {
this.shape = shape;
}
// Get the shape.
public void getShape() {
return shape;
}
}
Do I need to use a Getter method in my own code? And more importantly, does that code above look as if im moving in the right direction, I wanna try and get this functioning as I need by tonight or tomorrow morning.
This won't compile since you call your variable Shape in the class and this.shape in the method. Both variables need to start lower-case.
Re: Action/Change Listeners
Quote:
Originally Posted by
Conor-Magee
What I want working firstly though is when I move my slider the numbers of the text boxes go up based on the length and area equations... The most progress I've got is:
Code:
public class MyChangeAction implements ChangeListener
{ @Override
public void stateChanged(ChangeEvent e)
{
int currentValue = slider.getValue();
String val = Integer.toString(currentValue);
area.setText(val);
blength.setText(val);
}
}
This actually works, but the completely wrong way I need it to... When I move the slider the value of both boxes go from 0-100 with the slider... I can see how this happens... Ive tried implementing the following code to bring my equations into it... By cross-referencing my code I uploaded maybe you can help point out where I've went wrong, I think its something silly thats preventing it from working...
Code:
public class MyChangeAction implements ChangeListener
{ @Override
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
if (source.getValueIsAdjusting()) return;
int currentValue = slider.getValue();
MyControlPanel.Shape=MyFrame.Shape;
area.setText(Double.toString(Shape.Area(currentValue)));
blength.setText(Double.toString(Shape.Area(currentValue)));
}
}
Thats the code that doesn't work when entered... Its calling from the shapes class... :S
Will the slider's ChangeListener by inside of the MyControlPanel class or will it be a separate class?
Either way, again -- do not use a static Shape variable variable. If this listener is inside of MyContorlPanel, then what you want the slider to do is call repaint on the MyControlPanel instance that's it. Then in paintComponent get the value from the JSlider and use this to decide how large the drawing should be.
Re: Action/Change Listeners
I dont understand what you mean, dunno if were on the same page here...
How would I get the value of my Boundary Length and Area text boxes to display the Length and Area instead of the same value the slider is sitting at...
For example take my 'Circle' class:
Code:
public class Circle extends Shapes
{
public double Area(int L)
{
return L*L*Math.PI;
}
public double Boundary(int L)
{
return 2*L*Math.PI;
}
}
I need the variable 'L' in this code to be assigned the value of the slider, so the equation is calculated and put into the BLength and Area text boxes respectively... You know where Im coming from?
Re: Action/Change Listeners
I would probably find it helpful if you had the time to provide example code rather than a description... Im more of a visual learner, like how you would code a slider to redraw a shape based on the 2 text box values.
Re: Action/Change Listeners
OK, I'll show you this one bit of code, but all else you're on your own:
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderEg extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private JSlider slider = new JSlider(0, 100, 50);
public SliderEg() {
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
int value = slider.getValue();
// do whatever you need with slider's value here
// but also call repaint!
repaint();
}
});
slider.setOpaque(false);
setLayout(new BorderLayout());
add(slider, BorderLayout.SOUTH);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// extract the slider's value and use it however
// you see fit to set the length of your graphic
// here it's scaled such that when slider hits 100
// the oval fills the JPanel
int length = (getWidth() * slider.getValue()) / 100;
g.drawOval(0, 0, length, length);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
SliderEg mainPanel = new SliderEg();
JFrame frame = new JFrame("Slider Eg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Re: Action/Change Listeners
Thanks for your help but this is way over my head :/ I give up
My heads gonna explode