Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Idk what is wrong

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Idk what is wrong

    Hi folks,

    I'm a beginner to programming and for school i had to make an app that would have a ball bouncing horizontal between the frame.
    there must be a button at the bottom to switch from direction.

    now i've made a litle script, but ofcourse it doesn't work.
    could someone help me out?

    Frame:
     package Oefening4_4;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import test.AutoBalKnoppenBalk;
     
    public class Frame extends JFrame {
     
    	public Frame()
    	{
    		JFrame venster = new JFrame();
    		venster.setSize(600,400);
    		venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		venster.setTitle("Oefening 4.4");
    		venster.setLocation(100,100);
    		venster.setVisible(true);
     
    		 BalPaneel ballenPaneel = new BalPaneel();
    		 add( ballenPaneel, BorderLayout.CENTER );
     
    		 KnoppenPaneel Knoppen = new KnoppenPaneel( ballenPaneel );
    		 add( Knoppen, BorderLayout.SOUTH );
     
    	}
     
    	public static void main(String[] args) 
    	{
    		new Frame();
    	}
    }

    BalPaneel:
     package Oefening4_4;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
     
    public class BalPaneel extends JPanel implements ActionListener {
     
    	/**
    	 * Delay = delay for the timer
    	 * 
    	 * veranderRichting = change direction
    	 * 
    	 * verplaatsX = move horizontal
    	 */
     
    	public Bal Bal1;
    	public final int Delay = 30;
     
    	public BalPaneel() {
     
    		Bal1 = new Bal(200, 100, Color.orange, 4);
    		Timer autoKlik = new Timer(Delay, this);
    		autoKlik.start();
    	}
     
    	public void veranderRichting()
    	{
    		Bal1.veranderRichting();
        }
     
    	public void verplaatsX() {
    		Bal1.verplaatsX();
    	}
     
    	public void paintComponent( Graphics g )
    	{
    	    super.paintComponent( g );
    	    Bal1.teken( g );
    	}
     
     
    	public void actionPerformed( ActionEvent e )
    	{
    	    Bal1.verplaatsX();
    	    repaint();
    	}
    }

    KnoppenPaneel:
     package Oefening4_4;
     
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    /**
     * wissel = button to change from direction
     * @author Rick
     *
     */
     
     
    public class KnoppenPaneel extends JPanel implements ActionListener {
     
    	private JButton wissel;
    	BalPaneel ballenPaneel;
     
    	public KnoppenPaneel( BalPaneel ballenPaneel) {
     
    		wissel = new JButton("Wissel van richting");
    		wissel.addActionListener(this);
    		add(wissel);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == wissel) {
    			ballenPaneel.veranderRichting();
    		}
    		else {
    			System.out.println("Dit is knoppenpaneel else");
    		}
    	}
     
    }

    Bal:
     package Oefening4_4;
     
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Bal {
    		int x = 200;
    		int y = 200;
    		int grootte = 150;
    		private Color kleur;
    		private int snelheid;
     
    		public Bal(int x, int verPos, Color kleur, int snelheid) {
    			this.x = x;
    			this.y = verPos;
    			this.grootte = grootte;
    			this.kleur = kleur;
    			this.snelheid = snelheid;	
    		}
     
    		public void verplaatsX() {
    			x += snelheid;
    			if (x <= 0) {
    				veranderRichting();
    			}else {
    				x += snelheid;
    			}
    		}
     
    		 public void veranderRichting()
    		  {
    		    snelheid = - snelheid;
    		    if (x >= 600 - grootte) {
    		    	verplaatsX();
    		    }else {
    		    veranderRichting();	
    		    }
    		  }
     
     
    	  public void teken( Graphics g )
    	  {
    	    g.setColor( Color.orange );
    	    g.fillOval( x, y, grootte, grootte);
     
    	    g.setColor( Color.BLACK );
    	    g.drawOval( x, y, grootte, grootte );
     
    	    g.drawOval( x + grootte / 4, y, grootte / 2, grootte ); 
    	  }
    }

    all i see = the frame. no ball and no button

    (it's in dutch with some english explaination)

    srry for my bad english
    Last edited by Devourz; October 28th, 2011 at 06:47 AM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Idk what is wrong

    Do not use hosts to upload your code. Post it here using
     [highlight=java] *** CODE *** [/highlight]

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:00 PM.

Similar Threads

  1. Not sure what is wrong
    By dremn2004 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 18th, 2011, 11:49 AM
  2. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 08:47 AM
  3. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM