-
July 27th, 2010, 01:02 PM
#1
JSlider...
Can't get the JSlider to work, why ?
Code:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Boll b = new Boll ();
JFrame ramen = new JFrame ("Flying things...");
ramen.setSize(800, 640);
ramen.setLocationRelativeTo(null);
ramen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rummet rum = new Rummet ();
ramen.add(rum, "Center");
ramen.setVisible(true);
ramen.setResizable(false);
ramen.add(b.linjal (), "South");
Random farg = new Random ();
while (true)
{
int rod = farg.nextInt (256);
int gron = farg.nextInt (256);
int bla = farg.nextInt (256);
Color farger = new Color (rod, gron, bla);
b = new Boll (farger);
rum.läggTillRitaBoll(b);
Destination <Boll> dest = new Destination <Boll> (rum, b);
new Thread (dest).start ();
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
}
}
}
}
Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Boll implements FlyttaBoll, RitaBoll, ChangeListener
{
private Slider linjal = new Slider (0, 5, 1);
private int x;
private int y;
private int dx;
private int dy;
private Paint bollFarg;
private int xStorlek;
private int yStorlek;
public Boll ()
{
}
public Boll (Paint bollFarg)
{
this.bollFarg = bollFarg;
this.xStorlek = 2;
this.yStorlek = 2;
this.x = 0;
this.y = 0;
this.dx = linjal.getValue();
this.dy = linjal.getValue();
linjal.addChangeListener(this);
}
public void rita (Graphics2D g)
{
Ellipse2D cirkel = new Ellipse2D.Double (x, y, xStorlek, yStorlek);
g.setPaint (bollFarg);
g.fill (cirkel);
}
public boolean flytta ()
{
if (x < 0 || x > 780)
{
dx = -dx;
}
if (y < 0 || y > 570)
{
dy = -dy;
}
x += dx;
y += dy;
boolean kvarIRummet = true;
return kvarIRummet;
}
public void stateChanged (ChangeEvent e)
{
Object kallan = e.getSource();
Slider slide = (Slider) kallan;
dx = slide.getValue();
dy = slide.getValue();
}
public Slider linjal ()
{
return linjal;
}
}
-
July 28th, 2010, 02:39 AM
#2
Re: JSlider...
And how does it not work? As I recall, default LayoutManager for JFrame is BorderLayout and your spelling of the constants there is correct, so I suppose the slider does show. However, that said, use the constant variable instead of literal String where ever possible... such as BorderLayout.CENTER and not "Center".
What is your slider supposed to do? You have methods for moving the ball (flytta) and drawing the ball (rita), but they are not called in any of the code you have. You get informed through the change listener when the slider is adjusted, why not call the necessary methods then? (adjust the variables, and call repaint on whichever component uses those variables.)
You should repost with more accurate information, what is the slider supposed to do? How is the change information propagated into actual drawing done on the expected panel (a Rummet?), and how does the panel handle the balls now. I suspect, you change the variables, but it does not mean, that a repaint just magically knows to happen.
Lastly....
It is not a long lasting principle to combine controls (such as slider) and model objects (such as ball) together. Develop concept such as "selectedBall", this you can do, by adding a mouseListener to the panel where you draw, iterate over the Balls you have and check which ones bounds rectangle contained the mouse events point.
Notify the mainpanel of the selection change, there forward that information to whoever needs to know (such as the slider). Upon recieving the change information, slider will update to given ball instance and so forth. Keep your code close to the bone, if you need a slider to adjust the value of the ball, extend slider to an implementation that handles a ball. Personally I do not see sense in why you would want to have methods relating to painting of a different component (rita), or managing the actual moving of a ball (flytta) inside your slider. So not, its responsibility.
Last edited by Londbrok; July 28th, 2010 at 02:42 AM.
-
July 28th, 2010, 09:54 AM
#3
Re: JSlider...
I want to change the speed of the balls, nothing happens when I drag the slider...
Ok, have changed now to BorderLayout.CENTER and BorderLayout.SOUTH, thanks!
Can post everything, all my files.
I only want to change the speed of the balls, therefore I have:
public void stateChanged (ChangeEvent e)
{
Object kallan = e.getSource();
Slider slide = (Slider) kallan;
dx = slide.getValue(); // Then what?
dy = slide.getValue(); // and here ?
}
MAIN
Code:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Boll b = new Boll ();
JFrame ramen = new JFrame ("Flying things...");
ramen.setSize(800, 640);
ramen.setLocationRelativeTo(null);
ramen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rummet rum = new Rummet ();
ramen.add(rum, BorderLayout.CENTER);
ramen.setVisible(true);
ramen.setResizable(false);
ramen.add(b.linjal (), BorderLayout.SOUTH);
Random farg = new Random ();
while (true)
{
int rod = farg.nextInt (256);
int gron = farg.nextInt (256);
int bla = farg.nextInt (256);
Color farger = new Color (rod, gron, bla);
b = new Boll (farger);
rum.läggTillRitaBoll(b);
Destination <Boll> dest = new Destination <Boll> (rum, b);
new Thread (dest).start ();
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
}
}
}
}
Boll (BALL)
Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Boll implements FlyttaBoll, RitaBoll, ChangeListener
{
private Slider linjal = new Slider (0, 5, 1);
private int x;
private int y;
private int dx;
private int dy;
private Paint bollFarg;
private int xStorlek;
private int yStorlek;
public Boll ()
{
}
public Boll (Paint bollFarg)
{
this.bollFarg = bollFarg;
this.xStorlek = 2;
this.yStorlek = 2;
this.x = 0;
this.y = 0;
this.dx = linjal.getValue();
this.dy = linjal.getValue();
linjal.addChangeListener(this);
}
public void rita (Graphics2D g)
{
Ellipse2D cirkel = new Ellipse2D.Double (x, y, xStorlek, yStorlek);
g.setPaint (bollFarg);
g.fill (cirkel);
}
public boolean flytta ()
{
if (x < 0 || x > 780)
{
dx = -dx;
}
if (y < 0 || y > 570)
{
dy = -dy;
}
x += dx;
y += dy;
boolean kvarIRummet = true;
return kvarIRummet;
}
public void stateChanged (ChangeEvent e)
{
Object kallan = e.getSource();
Slider slide = (Slider) kallan;
this.dx = slide.getValue();
this.dy = slide.getValue();
}
public Slider linjal ()
{
return linjal;
}
}
Destination
Code:
class Destination <T extends RitaBoll & FlyttaBoll> implements Runnable
{
private Rummet rum;
private T boll;
public Destination (Rummet rum, T boll)
{
this.rum = rum;
this.boll = boll;
}
public void flytta ()
{
boolean ejDestination = boll.flytta ();
while (ejDestination)
{
rum.repaint();
try
{
Thread.sleep(4);
}
catch (Exception e)
{
}
ejDestination = boll.flytta ();
}
}
public void run ()
{
this.flytta ();
}
}
Slider
Code:
import java.awt.Color;
import javax.swing.*;
public class Slider extends JSlider
{
public Slider (int minstaVarde, int storstaVarde, int startVarde)
{
super (minstaVarde, storstaVarde, startVarde);
this.setBackground(Color.BLACK);
this.setMajorTickSpacing(1);
this.setPaintTicks(true);
this.setPaintLabels(true);
}
}
Rummet (The room)
Code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Rummet extends JPanel
{
private ArrayList <RitaBoll> bollar;
public Rummet ()
{
this.setBackground(Color.BLACK);
bollar = new ArrayList <RitaBoll> ();
}
@Override
public void paintComponent (Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
synchronized (this)
{
int antalBollar = bollar.size();
RitaBoll boll = null;
for (int i = 0; i < antalBollar; i++)
{
boll = bollar.get(i);
boll.rita(g);
}
}
}
public synchronized void läggTillRitaBoll (RitaBoll boll)
{
bollar.add(boll);
}
}
RitaBoll (PaintBall)
Code:
import java.awt.*;
public interface RitaBoll {
void rita (Graphics2D g);
}
FlyttaBoll (MoveBall)
Code:
public interface FlyttaBoll{
boolean flytta ();
}
-
July 28th, 2010, 11:29 AM
#4
Re: JSlider...
Why is the slider part of the Ball class, it has nothing to do with this class.
Your problem is you are adding the slider from a ball but then you are creating another ball which you are doing something with. The new ball has it's own slider which is a different slider to the one you have added to the screen and hence it's stateChanged() method is never being called.
-
July 28th, 2010, 08:31 PM
#5
Re: JSlider...
Ok, have moved it now. But I have tried everything now, and I can't solve it :-(
Really need help with this one.
-
July 29th, 2010, 05:21 AM
#6
Re: JSlider...
Where have you moved it to and have you added the correct ball as a listener?Post your current code so we can see what you have changed.
-
July 29th, 2010, 09:00 AM
#7
Re: JSlider...
New ball:
Code:
import java.awt.*;
import java.awt.geom.*;
public class Boll implements FlyttaBoll, RitaBoll
{
private int x;
private int y;
private int dx;
private int dy;
private Paint bollFarg;
private int xStorlek;
private int yStorlek;
public Boll ()
{
}
public Boll (int dxx, int dyy)
{
this.dx = dxx;
this.dy = dyy;
}
public Boll (Paint bollFarg)
{
this.bollFarg = bollFarg;
this.xStorlek = 2;
this.yStorlek = 2;
this.x = 0;
this.y = 0;
this.dx = 1;
this.dy = 1;
}
public void rita (Graphics2D g)
{
Ellipse2D cirkel = new Ellipse2D.Double (x, y, xStorlek, yStorlek);
g.setPaint (bollFarg);
g.fill (cirkel);
}
public boolean flytta ()
{
if (x < 0 || x > 780)
{
dx = -dx;
}
if (y < 0 || y > 570)
{
dy = -dy;
}
x += dx;
y += dy;
boolean kvarIRummet = true;
return kvarIRummet;
}
}
Main
Code:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Activate a = new Activate ();
Boll bollar = a.bollen();
JFrame ramen = new JFrame ("Flying things...");
ramen.setSize(800, 640);
ramen.setLocationRelativeTo(null);
ramen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rummet rum = new Rummet ();
ramen.add(rum, BorderLayout.CENTER);
ramen.add(a.slide(), BorderLayout.SOUTH);
ramen.setVisible(true);
ramen.setResizable(false);
Random farg = new Random ();
while (true)
{
int rod = farg.nextInt (256);
int gron = farg.nextInt (256);
int bla = farg.nextInt (256);
Color farger = new Color (rod, gron, bla);
bollar = new Boll (farger);
rum.läggTillRitaBoll(bollar);
Destination <Boll> dest = new Destination <Boll> (rum, bollar);
new Thread (dest).start ();
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
}
}
}
}
New class
Code:
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Activate
{
private Boll b;
private Slider linjal = new Slider (0, 5, 1);
public Activate ()
{
linjal.addChangeListener(new ChangeListener()
{
public void stateChanged (ChangeEvent e)
{
int varde = linjal.getValue();
b = new Boll (varde, varde);
}
});
}
public Slider slide ()
{
return linjal;
}
public Boll bollen ()
{
return b;
}
}
-
July 30th, 2010, 10:23 AM
#8
Re: JSlider...
Look at your logic at explain to me how that is going to work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|