Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default My timer constructor is not defined

    In my main method I am trying to create a Timer, but when I put new Timer(1000,listener) the constructor is not defined. It is when there is nothing in it. I find this super weird because in all my other programs this does not happen. I looked at the documentation and can't see what I am doing wrong, so I turn here.


    ActionClass
    Java Code:
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class Action extends JComponent {
    private BufferedImage shooter1;
    private BufferedImage shooter2;
    private BufferedImage currentShooter = shooter1;
    int char1x = 20;
    int char1y= 470;
    int k;
    int time = 0;
    Boolean rightPressed = false;
    Boolean leftPressed = false;
    public Action(){
    try{
    shooter1 = ImageIO.read(new File("/Users/M/Downloads/char1.png"));
    shooter2 = ImageIO.read(new File("/Users/M/Downloads/char2.png"));
    } catch(Exception e){System.out.print("image not received");}
    }
    public void paintComponent(Graphics g){
    g.fillRect(50,50,50,50);
    currentShooter = shooter1;
    g.drawImage(currentShooter,char1x,char1y,null);
    g.drawString(Integer.toString(time),20,100);
    repaint();
    }
    public void right(){
    char1x+=5;
    }
    public void left(){
    char1x-=5;
    }
    public void Move(){
    if(rightPressed = true){
    right();
    }
    if(leftPressed = true){
    left();
    }
    }
    KeyListener kl = new KeyListener(){
    
    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
                  k = e.getKeyCode();
                 if(k==KeyEvent.VK_RIGHT){
                rightPressed=true;
                  repaint();
                 }
                 if(k==KeyEvent.VK_LEFT){
                     leftPressed = true;
                repaint();
                 }
                 
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
    if(k==KeyEvent.VK_RIGHT){
               	 rightPressed=false;
                 	 repaint();
                }
                if(k==KeyEvent.VK_LEFT){
                    leftPressed = false;
               	 repaint();
                }
    }
    };
    //switch the images with the time modulus
    public void imgSwitch(){
    if(time ==0){
    currentShooter = shooter1;
    }
    if(time == 1){
    currentShooter = shooter2;
    }
    if(time%2==1){
    currentShooter = shooter2;
    }
    if(time%2==0){
    currentShooter  = shooter1;
    }
    }
    
    }
    Main

    Java Code:
    import java.util.Timer;
    import javax.swing.*;
    
    
    public class Main extends Action {
      
     public Main(){
    JFrame f = new JFrame("MW Game");
       f.setSize(750,750);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Action a = new Action();
       MyListener listener = new MyListener(a);
       Timer timer = new Timer(100,listener);
       timer.start();
       f.add(a);
       f.addKeyListener(a.kl);
       f.setResizable(false);
       f.setLocationRelativeTo(null);
       a.Move();
       f.repaint();
       f.setVisible(true);
     }
    
    public static void main(String[] args) {
    new Main();
    }
    
    }
    error like 13-14

    MyListener

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    public class MyListener implements ActionListener {
    private Action a;
    
    MyListener(Action action){
    this.a = a;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    a.imgSwitch();
    }
    
    }
    Last edited by MW130; 02-13-2014 at 04:59 AM.

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: My timer constructor is not defined

    Resolved

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: My timer constructor is not defined

    Resolved
    How was it resolved?

Similar Threads

  1. Replies: 11
    Last Post: 10-29-2013, 12:37 PM
  2. What is my Timer constructor complaining about ?
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 02-19-2012, 10:11 PM
  3. Problems with Timer Constructor
    By Yids in forum AWT / Swing
    Replies: 3
    Last Post: 03-11-2011, 05:35 AM
  4. Problems with Timer Constructor
    By Yids in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 04:56 PM
  5. Accessing class defined within a constructor
    By kwaspl in forum New To Java
    Replies: 4
    Last Post: 12-21-2009, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •