Results 1 to 10 of 10
Like Tree2Likes
  • 2 Post By jim829

Thread: Swing Frames

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

    Default Swing Frames

    In Swing, I am making a simple pong game. I have the ball add 5 x coords and 7 y per second. However, I notice that the movement is very laggy. Is there any fix for this? Also, the two pong rackets are controlled by one (w up and s down) and two (up arrow up and down arrow down). For the arrow key player two control, if you hold down the keys it moves continuously. However, this down not happen for w and s. It has to be pressed repeatedly. How can I make w and s continuously move when pressed? This is under the KeyPressed method of interface KeyListener

    Thanks!

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Swing Frames

    Can you please post your code
    With the right know how, anything is possible

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

    Default Re: Swing Frames

    Quote Originally Posted by Daryn View Post
    Can you please post your code
    Sorry, here it is:

    MyListener class:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class MyListener implements ActionListener {
    private Pong pong;
    	
    	public MyListener(Pong pong){
    		this.pong = pong;
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
         pong.MoveBall();		
    	}
    
    }
    Main Class:

    Java Code:
    import javax.swing.*;
    public class Main {
    
    	public Main(){
    		JFrame frame = new JFrame("Ping Pong");
    		frame.setSize(500,500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Pong pong = new Pong();
    		frame.add(pong);
    		MyListener listener = new MyListener(pong);
    		Timer timer = new Timer(1000/30,listener);
    		frame.addKeyListener(pong.k);
    		timer.start();
    		frame.repaint();
    		frame.setVisible(true);
    		
    	}
    	public static void main(String[] args) {
        new Main();
    	}
    
    }
    Pong Class (Graphics)
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.*;
    public class Pong extends JComponent{
    int pongX=200;
    int pongY=200;
    int pongXspeed = 5;
    int pongYspeed = 7;
    int pongDiameter = 60;
    int SliderOneX = 5;
    int SliderOneY = 0;
    int SliderTwoX = 465;
    int SliderTwoY = 0;
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.fillOval(pongX, pongY, pongDiameter, pongDiameter);
    		g.setColor(Color.blue);
    		g.fillRect(SliderOneX, SliderOneY, 30, 120);
    		g.fillRect(SliderTwoX, SliderTwoY,30, 120);
    	}
    public void MoveBall(){
    	pongX += pongXspeed;
    	if(pongX<=SliderOneX+30 && pongY>=SliderOneY+120 && pongY<=SliderOneY || pongX+65>=SliderTwoX && pongY<=SliderTwoY && pongY>=SliderTwoY  ){
    		pongXspeed *= -1;
    	}
    	pongY += pongYspeed;
    	if(pongY > getHeight()-pongDiameter || pongY < 0){
    		pongYspeed *= -1;
    	}
    	if(pongX>500 || pongX<-30){
    		pongX = 200;
    		pongY = 200;
    	}
    	repaint();
    }
    KeyListener k = new KeyListener(){
    @Override
    public void keyTyped(KeyEvent e) {
    	// TODO Auto-generated method stub
    	
    }
    @Override
    public void keyPressed(KeyEvent e) {
    	int k = e.getKeyCode();
    	if (k == KeyEvent.VK_UP) {
    		SliderTwoY -= 15;
    	}
    	if (k == KeyEvent.VK_DOWN){
    		SliderTwoY += 15;
    	}
    	if (k == KeyEvent.VK_W){
    		SliderOneY -=15;
    	}
    	if (k == KeyEvent.VK_S){
    		SliderOneY +=15;
    	}
    	repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {
    	// TODO Auto-generated method stub
    }
    };
    }
    Another problem I am having is how to get the ball to move the opposite direction when it hits the slider. This is done in the MoveBall() method declared in the pong class. However, I am not sure how to approach it. I know that to reverse the ball you do speed *-1, but I do not know which coordinates to put for it to hit and move backwards. This is kinda more mathematical, but hopefully you know.

    Thanks Daryn

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Swing Frames

    Im awsome with the whole mathametical aspect of programing. So what you can do to things for bouncing the balls off of the paddle. One would be to send it from where it came and change both x and y coordinates the other would be to just flip the x. Play around with both of them and see what you come up with
    With the right know how, anything is possible

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: Swing Frames

    A key has two time values associated with it: a delay value; after an initial key press, you have to wait 'delay' time units before the key starts to repeat; a 'repeat' value; after the key starts 'repeating' it does so after 'repeat' time units. See for yourself: press, say, the 'x' key on the command line and notice those two time values. If you only move the ball when such events happen, you'll notice sluggish behaviour. Keep the ball moving until an event hits that tells your program to do otherwise. A Timer can help here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Swing Frames

    Thanks JosAH, that helped. Do you know how I could make W and S continuously move instead of having to press them repeatedly?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: Swing Frames

    Quote Originally Posted by MW130 View Post
    Thanks JosAH, that helped. Do you know how I could make W and S continuously move instead of having to press them repeatedly?
    Yup, do the moving in another thread: define a small state machine with three states, not moving, moving up or moving down; define how the state changes if a 'w' or 's' key is pressed. If no key is pressed, the state doesn't change.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,896
    Rep Power
    5

    Default Re: Swing Frames

    Start moving when the key press event is fired, stop moving when the key release event is fired. That indeed involves maintaining a state as Jos explains.

    And this is only an easy example. Imagine when you want to create a game where you also want to react to a button being pressed for a certain amount of time, or is double tapped. Fun fun :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Swing Frames

    You may also want to consider using inner classes for your event handlers. They are very useful when there are many methods but you only need one or two as you can extend the appropriate adapter class. And the enclosing class and inner class have access to each others state, private or otherwise.

    Regards,
    Jim
    gimbal2 and MW130 like this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Swing Frames

    From what I am reading on this thread, you are bothered that when you hit a key, you move, then it stops for some time and starts back up, correct? To fix this without multi threading (as stated above), use booleans, in the KeyPressed method, when you press a key, set a boolean for that key to true, in the keyRealeased method, when that key is released set the boolean to false. This will ensure your movements are based off of the key being pressed, not just the event firing of the key.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

Similar Threads

  1. connecting two frames
    By pardhu in forum AWT / Swing
    Replies: 2
    Last Post: 10-28-2011, 06:50 PM
  2. frames
    By giorgi in forum AWT / Swing
    Replies: 2
    Last Post: 05-04-2011, 08:45 PM
  3. travelling between frames
    By Stephen Douglas in forum New To Java
    Replies: 24
    Last Post: 07-04-2010, 03:51 PM
  4. jsp frames
    By vasug in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2010, 05:00 PM
  5. Help regarding Frames
    By ramesh.8189 in forum AWT / Swing
    Replies: 14
    Last Post: 02-15-2009, 08:12 AM

Posting Permissions

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