Results 1 to 20 of 22
Thread: Action/Change Listeners
- 11-18-2011, 11:12 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
Action/Change Listeners
Alright guys I have a horrible problem... Each week in University we're building towards creating a program going through each section one at a time... Im at the final hurdle and I am completely stuck...
We need to create a program with 3 JMenuItems: Square, Circle and Triangle... When you click on each of these it should create the shapes on the main panel... Theres also a slider and 2 text boxes (Area and Boundary Length) when I move the slider it should calculate the area and blength and also adjust the size of the drawn shape... Ive got the slider working so that the text boxes move from 0-100 but not with the equations...
Ive commented out some code Ive tried.. If someone could take a look at it, and if their able to supply code that will work.. Id like to see how action and change listeners are used as well as using it to change the size of specified shapes...
Thanks
-Conor
(Wouldnt let me attach the Netbeans package to this thread, If you post your email I could send you it and you could possibly get back to me via email or here?)
-
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.
- 11-19-2011, 12:22 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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.
- 11-19-2011, 06:33 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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.
- 11-19-2011, 06:46 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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.
- 11-19-2011, 07:34 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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
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:
Java Code:public Class Example { private int foo; // setter method public void setFoo(int foo) { this.foo = foo; } // getter method public int getFoo() { return foo; } }
- 11-19-2011, 08:14 PM #11
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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
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.
- 11-19-2011, 08:22 PM #13
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
Re: Action/Change Listeners
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.Java 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; } }
- 11-19-2011, 08:59 PM #14
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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:
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...Java 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); } }
Thats the code that doesn't work when entered... Its calling from the shapes class... :SJava 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))); } }
-
Re: Action/Change Listeners
-
Re: Action/Change Listeners
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.
- 11-19-2011, 09:41 PM #17
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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:
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?Java 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; } }
- 11-19-2011, 09:54 PM #18
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
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:
Java 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(); } }); } }
- 11-19-2011, 10:00 PM #20
Member
- Join Date
- Nov 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
action listeners
By Ike in forum New To JavaReplies: 13Last Post: 11-07-2011, 10:31 AM -
Adding Action Listeners
By mamoonrizwan in forum Advanced JavaReplies: 2Last Post: 08-07-2011, 06:25 PM -
Applets issue with Action Listeners
By aadem in forum New To JavaReplies: 2Last Post: 03-19-2011, 06:17 PM -
Creating a GUI events with action listeners
By sidd0123 in forum AWT / SwingReplies: 8Last Post: 04-02-2010, 11:32 PM -
Action Event and Listeners
By lost1 in forum New To JavaReplies: 3Last Post: 11-14-2007, 04:26 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks