blutiger Anfänger braucht Hilfe ^^

Status
Nicht offen für weitere Antworten.

jeken

Neues Mitglied
hi ihr da draussen 🙂
bin wirklich ein blutiger anfänger und stehe hier vor einem kleinen Problem.
Ich hab mich an Swing versucht und habe es auch so weit geschafft das ich
blaue Bälle animiert über den Bildschirm wandern lasse 😉. Mein Problem is
folgendes. Ich hab die ganzen Konstruktoren mit den Buttons und den Comboboxen
und dem JSlider in einer Klasse ApplicationWindow und die methode die dann malt in einer anderen
Klasse Bouncingball. Ich würde gerne von ComboBoxen und dem Slider den Index bzw.
das Value an Bouncingball übergeben um die Geschwindigkeit der Bälle
und so zeugs zu steuern. ich muss also irgendwie die BOxen und den Slider aus
Bouncingball heraus auslesen oder sehe ich das falsch ? ich hab nur keine ahnung wie ^^
wäre dankbar wenn ihr mir helfen könntet 🙂

hier noch die beiden relevanten auszüge:

Code:
class BouncingBall{
    
    private int m=20;
    private int m2=ApplicationWindow.mass2*10;
    private int m3=35;
    private int m4=15;
    
    private int radius = m*3;
    private int radius2 = m2*3;
    private int radius3= m3*3;
    private int radius4= m4*3;
    
    
    private int x = 100;
    private int x2 = 400;
    private int x3 = x2+radius2+radius3;
    private int x4=  x3+radius3+radius4;
    
    private int y =  250;

    private int vx=ApplicationWindow.val;
    private int vx2=0;
    private int vx3=0;
    private int vx4=0;
  
    private Color color = new Color(0,0,255);
    private int val;    
   
    
    public void move() {
        
        x += vx;
        x2 +=vx2;
        x3 +=vx3;
        x4 +=vx4;
        //if (x <= radius) { x = radius; vx = -vx;}
        if (x >= x2-radius-radius2 ){
        
        vx2=vx; 
        
        
        vx3=vx+1; 
        
        vx4=vx+2;
        
        vx=-vx+1;}
        
        
        //if (x2 >= 949-radius2){vx2=-vx2;}
        y=250;
        
    }
    
    public void paint(Graphics g) {
        g.setColor(color);
        g.fillOval(x-radius, y-radius, radius+radius, radius+radius);
        g.fillOval(x2-radius2, y-radius2, radius2+radius2, radius2+radius2);
        g.fillOval(x3-radius3, y-radius3, radius3+radius3, radius3+radius3);
        g.fillOval(x4-radius4, y-radius4, radius4+radius4, radius4+radius4);
        
    }
}



class ApplicationWindow extends JFrame {
    public static int val;
    public static int mass2;
    
    protected AnimationWindow animationWindow;
    
    public ApplicationWindow() {
        super("KlickKlack");
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);}
        });
    
        JToolBar toolBar = new JToolBar();
        addButtons(toolBar);
        
        animationWindow = new AnimationWindow();
        JScrollPane scrollPane = new JScrollPane(animationWindow);
        
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        contentPane.setPreferredSize(new Dimension(950, 530));
        contentPane.add(toolBar, BorderLayout.NORTH);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        setContentPane(contentPane);
    }
    
    public void addButtons(JToolBar toolBar) {
            String[] masses = { "keine Masse", "1", "2", "3",
    "4", "5", "6",
    "7", "8" };
    
        JButton button = null;
        button = new JButton("Run");
        button.setToolTipText("Animation starten");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                animationWindow.setMode(true);
            }
        });
        toolBar.add(button);
       
        button = new JButton("Stop");
        button.setToolTipText("Animation stoppen");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                animationWindow.setMode(false);}
        });
        toolBar.add(button);
        
        button = new JButton("Quit");
        button.setToolTipText("Programm beenden");
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             System.exit(0);}
        });
        
        toolBar.add(button);
        

         JSlider speed = new JSlider(1,6,1);
        speed.setPaintTicks(true);
        speed.setMajorTickSpacing(1);
        Hashtable labelTable = new Hashtable();
        labelTable.put( new Integer( 1 ), new JLabel("Slow") );
        labelTable.put( new Integer( 6 ), new JLabel("Fast") );
        speed.setLabelTable( labelTable );
        speed.setPaintLabels(true);
        val=speed.getValue();
        toolBar.add(speed); 
        
        JComboBox masschoices1 = new JComboBox(masses);
        masschoices1.setSelectedIndex(0);
        masschoices1.setBounds(30, 115, 100, 25);
        masschoices.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                                   }
            });
        toolBar.add(masschoices1);
        

        
        
    }
    

    
}
 
Hi, du musst in BouncingBall das Interface ChangeListener implementieren.
Danach kannst du den BouncingBall beim JSlider als Listener registrieren.

Code:
class BouncingBall implements ChangeListener  {  

     // Wird wenn registriert immer aufgerufen, wenn sich der Wert ändert.
     public void stateChanged(ChangeEvent e)  {
         JSlider slider = (JSlider) e.getSource();  // geht nur, wenn du nur beim JSlider registriert bist.
         int val = slider.getValue();
     }

In deiner anderen Klasse machst du dann soetwas

Code:
      BouncingBall b = new BouncingBall ();
      slider.addChangeListener(b);
 
muchas gracias 🙂
hat funktioniert .
sollten wir uns mal treffen .. ich schulde dir ein bier 😉

mfg
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben