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;
}
}