Hey Leute,
bei meinem Code (unten) finde ich den Fehler nicht. Die Klasse Object, von der später im Spiel alle Planeten, Sonnen und was weiß ich was noch kommt, erben, erbt von einem JPanel. Ich setzte im Konstruktor ja Größe, Position und Sichtbarkeit. Warum aber, wenn ich es zu meinem JFrame hinzufügen will, geht es nicht? Wenn ich es jedoch als ContentPane mache wird es ohne Probleme angezeigt.
Mein Frame:
Mein "Object":
Schon mal Danke im Voraus,
Daviddiviad
bei meinem Code (unten) finde ich den Fehler nicht. Die Klasse Object, von der später im Spiel alle Planeten, Sonnen und was weiß ich was noch kommt, erben, erbt von einem JPanel. Ich setzte im Konstruktor ja Größe, Position und Sichtbarkeit. Warum aber, wenn ich es zu meinem JFrame hinzufügen will, geht es nicht? Wenn ich es jedoch als ContentPane mache wird es ohne Probleme angezeigt.
Mein Frame:
Java:
package SC.MainFrame;
import java.awt.Color;
import javax.swing.JFrame;
import SC.Objects.Object;
public class Startup {
public static void main(String[] args) {
Startup start = new Startup();
}
public Startup () {
Object objectTest = new Object(0, 0, 200, 200);
objectTest.setBackground(Color.BLACK);
JFrame frame = new JFrame("Test");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.add(objectTest);
frame.setVisible(true);
}
}
Mein "Object":
Java:
package SC.Objects;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Object extends JPanel {
public static final long serialVersionUID = 1;
int x;
int y;
int width;
int height;
String id = "";
BufferedImage texture;
boolean debug = false;
public Object (int x, int y, int width, int height) {
setSize(width, height);
setLocation(x, y);
setVisible(true);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void setTexture (String path) {
try {
this.texture = ImageIO.read(getClass().getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getTexture () {
return texture;
}
public Point getPosition () {
return new Point(x, y);
}
public void setPosition (int x, int y) {
this.x = x;
this.y = y;
}
public Dimension getSize () {
return new Dimension(width, height);
}
public void setSize (int width, int height) {
this.width = width;
this.height = height;
}
public boolean isDebugEnabled () {
return debug;
}
public void setDebugEnabled (boolean debugEnabled) {
this.debug = debugEnabled;
}
public void setID (String id) {
this.id = id;
}
public String getID () {
return id;
}
public void render() {
repaint();
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
if(debug) {
g.setColor(Color.RED);
g.drawString(id, 0, 20);
}
}
}
Schon mal Danke im Voraus,
Daviddiviad