Also, da ich im moment Pong programmieren möchte brauche ich natürlich einen Ball und wollte den wie folgt anzeigen lassen
Doch leider bekomme ich dann ein Problem wenn ich es in mein bisheriges JFrame integrieren möchte, das auch noch JButtons "beherbergt". Der komplette Code:
Java:
public void showBall (Graphics g)
{
int x_pos = 30; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
g.setColor (Color.red); //Farbe des Balles
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
Doch leider bekomme ich dann ein Problem wenn ich es in mein bisheriges JFrame integrieren möchte, das auch noch JButtons "beherbergt". Der komplette Code:
Java:
package javaapplication2;
import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import javax.swing.JPanel;
public class Ball extends JPanel implements KeyListener{
JFrame frame = new JFrame("Fliegender Ball");
JButton b1 = new JButton();
JButton b2 = new JButton();
public Ball() throws IOException
{
showJFrame();
}
public void addComponentsToPane(Container pane) throws IOException {
System.out.println("Player 1: Type your name in please(max. 16 signs)");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String Spieler1 = br.readLine();
System.out.println("Player 2: Type your name in please(max. 16 signs)");
String Spieler2 = br.readLine();
pane.setLayout(null);
Insets insets = pane.getInsets();
b1.setText(Spieler1);
pane.add(b1);
Dimension size = b1.getPreferredSize();
b1.setBounds(350 , 20,
150, 20);
b2.setText(Spieler2);
pane.add(b2);
size = b2.getPreferredSize();
b2.setBounds(350 ,511,
150, 20);
}
public void showBall (Graphics g){ //Der Problemfall
int x_pos = 30; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
g.setColor (Color.red); //Farbe des Balles
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
private void showJFrame() throws IOException {
JFrame frame = new JFrame("Flying Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
showBall(frame.getGraphics()); //Die Zeile die den Problemfall aufruft
Insets insets = frame.getInsets();
frame.setSize(800 + insets.left + insets.right,
578 + insets.top + insets.bottom);
frame.setVisible(true);
frame.requestFocus();
frame.addKeyListener(this);
frame.getContentPane().setBackground(Color.BLACK);
}
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
@Override
public void keyTyped(KeyEvent e) {
if (e. getKeyChar()== 'a') {
b1.getLocation().getX();
int a = (int) b1.getLocation().getX();
int b = a-10;
if (a -10 < 0) {}
else {
b1.setLocation(b, 20);
b1.setVisible(true);
}
}
if (e. getKeyChar()== '4') {
b2.getLocation().getX();
int a = (int) b2.getLocation().getX();
int b = a-10;
if (a -10 < 0) {}
else {
b2.setLocation(b, 511);
b2.setVisible(true);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e. getKeyChar()== 'd') {
b1.getLocation().getX();
int a = (int) b1.getLocation().getX();
int b = a+10;
if (a +30 > 660) {}
else {
b1.setLocation(b, 20);
b1.setVisible(true);
}
}
if (e. getKeyChar()== '6') {
b2.getLocation().getX();
int a = (int) b2.getLocation().getX();
int b = a+10;
if (a +30 > 660) {}
else {
b2.setLocation(b, 511);
b2.setVisible(true);
}
}
}
@Override //muss (leider) drin beleiben
public void keyReleased(KeyEvent e) {
}
}
Zuletzt bearbeitet: