Toon posts:

[java]hoe moet ik hier een main voor maken

Pagina: 1
Acties:
  • 52 views sinds 30-01-2008

Verwijderd

Topicstarter
hoe krijg ik dit draaiend ... ik weet niet hoe main in elkaar steekt

...

public static void main(String[] args){ .. wat moet ik hiertussen zetten ??? .. }

...

mijn code...

/*
* Ball.java
*
* Created on 16 juli 2006, 12:36
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author rs
*/
import java.awt.event.*;
import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;

public class Ball extends JPanel implements ActionListener{
private int delay = 10;

//creat timer with delay X ms
protected Timer timer = new Timer(delay,this);

private int x = 0; // current ball position
private int y = 0;
private int radius = 15; //ball raduis
private int dx = 2; // increment on ball's x- & Y-coordinates
private int dy = 2;

public Ball(){
timer.start();
}

/** handle the action event */
public void actionPerformed(ActionEvent e){
repaint();
}
//paint ball
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
// Check bounderies
if (x < radius)
dx = Math.abs(dx);

if (x > getWidth() - radius)
dx = -Math.abs(dx);

if (y < radius)
dy = Math.abs(dy);

if (y > getWidth() - radius)
dy = -Math.abs(dy);
// adjust ball position
x += dx;
y += dy;
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);

}

public void suspend(){
timer.stop(); // suspend timer
}

public void resume(){
timer.start();
}

public void setDelay(int delay){
this.delay = delay;
timer.setDelay(delay);
}
}


-----------------------------

/*
* BallControle.java
*
* Created on 16 juli 2006, 13:06
*
*/

/**
*
* @author rs
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class BallControl extends JPanel implements ActionListener, AdjustmentListener{
private Ball ball = new Ball();
private JButton jbtStop = new JButton("Stop");
private JButton jbtStart = new JButton("Start");
private JScrollBar jsbDelay = new JScrollBar();

/** Creates a new instance of BallControle */
public BallControl() {
//group buttons in a panel
JPanel panel = new JPanel();
panel.add(jbtStop);
panel.add(jbtStart);

// add ball and numbers to panel
ball.setBorder(new javax.swing.border.LineBorder(Color.red));
jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
ball.setDelay(jsbDelay.getMaximum());
setLayout(new BorderLayout());
add(jsbDelay, BorderLayout.NORTH);
add(ball, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);

// register listeners
jbtStop.addActionListener(this);
jbtStart.addActionListener(this);
jsbDelay.addAdjustmentListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == jbtStop)
ball.suspend();
else if(e.getSource() == jbtStart)
ball.resume();
}

public void adjestmetValueChanged(AdjustmentEvent e){
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
}

public void adjustmentValueChanged(AdjustmentEvent e) {
}
}


----------------------------------------

/*
* BounceBallApp.java
*
* Created on 16 juli 2006, 14:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author rs
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class BounceBallApp extends JApplet {

/** Creates a new instance of BounceBallApp */
public BounceBallApp() {
getContentPane().add(new BallControl());
}
}


-------------------


kan de code nu eindelijk compilen & maar nu ???

  • Jaap-Jan
  • Registratie: Februari 2001
  • Laatst online: 15-02 21:59
new BounceBallApp() in je main zetten. Ik neem aan dat dat de plek is waar je je programma wilt laten beginnen.

| Last.fm | "Mr Bent liked counting. You could trust numbers, except perhaps for pi, but he was working on that in his spare time and it was bound to give in sooner or later." -Terry Pratchett


  • NMe
  • Registratie: Februari 2004
  • Laatst online: 22-01 23:51

NMe

Quia Ego Sic Dico.

Sorry hoor, maar dit wordt toch echt wel in elke beginnerstutorial en boek behandeld. Bovendien is het niet de bedoeling dat je hier maar even tientallen regels code post, en er dan vanuit gaat dat wij het even voor je afmaken; daar is GoT niet voor bedoeld. Zie ook Programming Beleid - De "quickstart".

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Dit topic is gesloten.