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




Reply With Quote