Hi me and my class are getting this problem with tim.start(); and stop. we get start to work with a button but cant stop and we can stop with the keyboard but not with the buttons bad explaination i know but have an look plz :P

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author lematdel1
 */
public class Main 
{
    public static class Tennis extends JFrame implements ActionListener
    {
        private Spelplan plan=new Spelplan();
        private JLabel poäng1=new JLabel("0",JLabel.CENTER);
        private JLabel poäng2=new JLabel("0",JLabel.CENTER);
        private JPanel pan = new JPanel();
        private JButton b1 = new JButton("Start");
        private JButton b2 = new JButton("Paus");
        private JButton b3 = new JButton("stopp");
        public Tennis()
        {
            setTitle("Tennis");
            plan.setPreferredSize(new Dimension (350,250));
            plan.setBackground(Color.white);
            poäng1.setFont(new Font("SansSerif", Font.BOLD, 24));
            poäng2.setFont(new Font("SansSerif", Font.BOLD, 24));
            plan.setLayout(new FlowLayout());
            //pan.setLayout(new FlowLayout());
            pan.add(b1);
            pan.add(b2);
            pan.add(b3);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            Container c = getContentPane();
            c.add(plan, BorderLayout.CENTER);
            c.add(poäng1, BorderLayout.WEST);
            c.add(poäng2, BorderLayout.EAST);
            c.add(pan, BorderLayout.SOUTH);
            pack();
            
            plan.init(poäng1, poäng2);
            //plan.startaSpel();
            
            setVisible(true);
            //setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public void actionPerformed(ActionEvent e)
        {
            plan.requestFocus();
            if(e.getSource()==b1)
                plan.startaSpel();
            if(e.getSource()==b3)
                plan.stoppaSpel();
        }
        
    }
    public static void main(String[]args) throws InterruptedException
    {
        Tennis t = new Tennis();
    }
}
class Spelplan extends JPanel implements ActionListener
{
    Image bilden = Toolkit.getDefaultToolkit().getImage("H:\\boll.gif");
    private Timer tim = new Timer (10, this);
    private JLabel poäng1;          //för att visa poäng
    private JLabel poäng2;          //för att visa poäng
    private int p1;                 //aktuella poäng
    private int p2;                 //aktuella poäng
    private int xMax, yMax;         //högsta x- och y-Koordinat
    private int r, x0, y0;          //bollens radie
    private int xSteg, ySteg;       //bollens steglängd
    private int v, v0=1;            //bollens hastighet
    
    private int rVä, rL1, rSteg1;      //Rackets överkant, längd och steglängd
    private int rHö, rL2, rSteg2;
    
    public void init(JLabel l1, JLabel l2)
    {
        poäng1=l1;
        poäng2=l2;
        xMax=getSize().width-1;
        yMax=getSize().height-1;
        r=yMax/20; 
        rL1=3*r;                             //beräkna rackets längd
        rSteg1=r;//beräkna bollens radie
        rL2=3*r;                             //beräkna rackets längd
        rSteg2=r;
        addKeyListener(kl);
        addComponentListener(cl);           //ändringar av planens storlek
        nollställ();
    }
    private void nollställ()
    {
        p1=0;
        p2=0;
        poäng1.setText("0");
        poäng2.setText("0");
        xSteg = ySteg = v = v0 = 1;         //utgångshastighet
        x0 = r+1;                           //sätt bollen i vänsterkanten
        y0 = yMax/2;                        //i mitten av kortsidan
    }
    public void startaSpel()
    {
        tim.start();
    }
    public void stoppaSpel()
    {
        tim.stop();
    }
    public void actionPerformed(ActionEvent e)
    {
        requestFocus();                     //Fix för tangentavlyssnare
        //x0=x0+1;
        //y0=y0+1;
        if(x0-r<=0)                         //är bollen i vänsterkanten?
        {
            if(y0<rVä || y0 > rVä+rL1)       //miss
            {
                Toolkit.getDefaultToolkit().beep();
                p2++;
                poäng2.setText(""+String.valueOf(p2)+"");
                v=v0; 
            }
            else
            {
                v++;  
            }
            xSteg=v;                        //flytta åt höger nästa gång
        }
        else if(x0+r>=xMax)
        {
            if(y0<rHö-1 || y0 > rHö+rL2)      //miss
            {
                Toolkit.getDefaultToolkit().beep();
                v=v0;
                p1++;
                poäng1.setText(""+String.valueOf(p1)+"");
            }
            else
            {
                v++;  
            } 
            xSteg=-v;
        }
        if(y0-r<=0 || y0+r>=yMax)           //i över eller underkanten?
            ySteg=-ySteg;
        x0+=xSteg;
        y0+=ySteg;
        if(x0<r)
            x0=r;
        else if (x0>xMax-r)
            x0=xMax-r+1;
        if(y0<r)
            y0=r;
        else if (y0>yMax-r+1)
            y0=yMax-r+1;
        repaint();
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(bilden, x0-r, y0-r, this);
        g.setColor(Color.black);
        g.fillRect(0,rVä,2,rL1);                //rita racket 
        g.fillRect(xMax-1,rHö,2,rL2);
    }
    KeyListener kl = new KeyAdapter()
    {
        public void keyPressed(KeyEvent e)
        {
            if(e.getKeyCode() == KeyEvent.VK_S)
                tim.start();
            if(e.getKeyCode() == KeyEvent.VK_K)
                rHö=Math.max(0, rHö-rSteg2);
            else if(e.getKeyCode()==KeyEvent.VK_M)
                rHö=Math.min(yMax-rL2, rHö+rSteg2);
            if(e.getKeyCode() == KeyEvent.VK_A)
                rVä=Math.max(0, rVä-rSteg1);
            else if(e.getKeyCode()==KeyEvent.VK_Z)
                rVä=Math.min(yMax-rL1, rVä+rSteg1);
            if(e.getKeyCode() == KeyEvent.VK_UP)
                y0=y0-5;
            if(e.getKeyCode() == KeyEvent.VK_DOWN)
                y0=y0+5;
            if(e.getKeyCode() == KeyEvent.VK_LEFT)
                x0=x0-5;
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
                tim.stop();
            repaint();
        }
    };
    ComponentListener cl=new ComponentAdapter()
    {
        public void componentResized(ComponentEvent e)
        {
            xMax=e.getComponent().getSize().width-1;
            yMax=e.getComponent().getSize().height-1;
            e.getComponent().requestFocus();
            repaint();
        }
    };
}

Hi me and my class are getting this problem with tim.start(); and stop. we get start to work with a button but cant stop and we can stop with the keyboard but not with the buttons bad explaination i know but have an look plz :P

Yes, I think a better explanation is needed. I ran it and I can't figure out what it's SUPPOSED to do, so I don't know what the problem is. I guess "S" is for "start" and the right arrow key is "stop"?

well yes its suposed to start the program and stop it but it wont work. But did u run the code and it worked for you?

well yes its suposed to start the program and stop it but it wont work. But did u run the code and it worked for you?

I don't know if it worked or not because I don't know what the program is supposed to do when it's starting and stopping. I see numbers changing sometimes, but I don't know when and how they are supposed to change or anything like that. I certainly don't see a tennis ball going back and forth. What's the program supposed to look like when it's started and stopped?

ahh sorry see the code where i specify the picture of the ball? u dont have that pic... but any 30x30 pixel pic will do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.