0

I'm trying to develop a simple game. The games is about the shapes. Shapes will move and we'll catch by mouse. I have already created a oval and given size of oval graphic. But I cannot move this shape repeatedly. I think I need to use timer class. I have been trying 2 hours myself but I didnt do yet.

The code;

    import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;


import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;



public class myshapestry extends JFrame implements ActionListener  {

JFrame frame=new JFrame("Deneme");
Container l ;
private static int ballX=150;
private static  int ballY=150;
myshapestry() {
     l=this.getContentPane();
     l.setLayout(null);
     MyPanel panel=new MyPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
        frame.setSize(getPreferredSize());``

 }
 public Dimension getPreferredSize() {
       return new  Dimension(500,600);
   }
   public static void main (String args[]){
       myshapestry tr=new myshapestry();
       tr.setTitle("Game of Shapes");


   } 

   private static class MyPanel extends JPanel {
       protected void paintComponent(Graphics g){
           super.paintComponent(g);
           g.fillOval(ballX, ballY,50 , 70);

    }

       public void actionPerformed(ActionEvent e){
          ballX = ballX + 5;
          ballY = ballY + 10;
          repaint();
      }

       }

   }

I was trying these code in the myshapestry code block;

Timer timer=new Timer(100,myshapestry);
t.start();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    *"But I cannot move this shape repeatedly. I think I need to use timer class."* Yes. From a glance at your code, it appears that it needs to establish a Swing `Timer` using the instance of `panel`. Tips: 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jun 20 '15 at 13:44
  • `l.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 20 '15 at 13:46
  • Yeah I will follow your tips Thank you so much – Muhammed Yalçın Kuru Jun 20 '15 at 14:12

1 Answers1

3

Add something like this

javax.swing.Timer timer=new javax.swing.Timer(100, panel) ;
timer.start();

Each 100msec the timer invokes actionPerformed() method of your MyPanel class

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Okey man. It is working right now. Thank you so much for your trips. And Can you explain me why I have to use javax.swing? As I wrote I was trying Timer t= new Timer(100,panel); t.start(); but I got wrong why? – Muhammed Yalçın Kuru Jun 20 '15 at 14:15
  • Now also I got it. I was importing wrong package. I have to import javax.swing.Timer; Instead of java.util.Timer; – Muhammed Yalçın Kuru Jun 20 '15 at 14:23