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