Hallo allerseits,
in einem Demo-Programm (mit Netbeans erstellt und dem zugehörigen grafischen Editor) lasse ich zu Testzwecken immer wieder 2 selbstgebastelte Autos (also keine Autos aus Grafikdateien) sich von von links nach rechts bewegen.
Das einfache Demo-Programm funktioniert. Nur gibt es ein kleines Problem:
Den Hintergrund habe ich im Visual-Editor (VE) in SpielJFrame rot und in SpielJPanel grün gemacht.
Warum erscheint der Hintergrund aber grau ??
Wo ist mein Fehler ?
Habe schon alles Mögliche probiert, hat aber nichts geholfen.
mfg
Ernst
[Java]
package pack30;
public class SpielJFrame extends javax.swing.JFrame {
private SpielJPanel spielJPanel;
/** Creates new form SpielJFrame */
public SpielJFrame() {
initComponents();
spielJPanel = new SpielJPanel();
jPanel1.add(spielJPanel, "Center");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 204, 153));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setLayout(new java.awt.BorderLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(85, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpielJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
[/Java]
[Java]
package pack30;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class SpielJPanel extends javax.swing.JPanel {
private Timer t;
private Auto auto1;
private Auto auto2;
private Image bild;
/** Creates new form SpielJPanel */
public SpielJPanel() {
initComponents();
auto1 = new Auto(this, Color.RED, 0, 0, 100);
auto2 = new Auto(this, Color.BLUE, 50, 80, 100);
auto1.setSchritt(10);
auto2.setSchritt(20);
t = new Timer(20,
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
myupdate();
maleInsBild();
repaint();
}
});
t.start();
}
public void setTimer(int millis) {
t.setDelay(millis);
}
public int getTimerTime() {
return t.getDelay();
}
public void myupdate() {
System.out.println("this.getWidth()="+this.getWidth()+"this.getHeight="+this.getHeight());
auto1.berechnePos();
auto2.berechnePos();
}
public void maleInsBild(){
int sx, sy;
Graphics graphics=null;
sx = this.getSize().width;
sy = this.getSize().height;
bild = createImage(sx, sy);
graphics = bild.getGraphics();
auto1.zeichneAuto(graphics);
auto2.zeichneAuto(graphics);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bild,0,0,null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setBackground(new java.awt.Color(204, 255, 153));
setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
setLayout(new java.awt.BorderLayout());
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
[/Java]
[Java]
package pack30;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
public class Auto {
private SpielJPanel spielJPanel;
private Color farbe;
private int x;
private int y;
private int groesse;
private int schritt;
private static int strassenlaenge;
public Auto(SpielJPanel pSpielJPanel, Color pFarbe, int pOrtX, int pOrtY, int pGroesse){
spielJPanel = pSpielJPanel;
farbe = pFarbe;
x = pOrtX;
y = pOrtY;
groesse = pGroesse;
schritt = 0;
}
public void setX(int pX){
x = pX;
}
public int getX(){
return x;
}
public void setY(int pY){
y = pY;
}
public int getY(){
return y;
}
public void setFarbe(Color pFarbe){
farbe = pFarbe;
}
public Color getFarbe(){
return farbe;
}
void berechnePos(){
if(x <= spielJPanel.getWidth()){
x = x + schritt;
}
else{
x=0;
}
}
public Point getPos() {
return new Point(x, y);
}
void setSchritt(int pSchritt){
schritt = pSchritt;
}
void zeichneAuto(Graphics g){
// Linker oberer Eckpunkt des Wagens
int x1 = x;
int y1 = y;
// Rechter unterer Eckpunkt des Wagens
int x2 = x1 + groesse; // Größe = Länge = x2-ortX
int y2 = y1 + groesse/2; // Höhe = y2-ortY
// Linker oberer Eckpunkt des Fensters
int x3 = x2 - groesse/4; // Fenster
int y3 = y1;
// Rechter unterer Eckpunkt des Fensters
int x4 = x3 + groesse/4; // Fenster
int y4 = y3 + groesse/4;
int r = groesse/8; // RadRadius
int x5 = x1 + 19 + r/2; // Hinterrad
int x6 = x2 - 19 - r/2; // Vorderrad
// Karosserie
g.setColor(farbe);
g.drawRect(x1, y1, x2-x1, y2-y1);
g.fillRect(x1, y1, x2-x1, y2-y1);
// Fenster
g.setColor(Color.BLACK);
g.drawRect(x3-1, y3-1, x4-x3+1, y4-y3+1);
g.setColor(Color.WHITE);
g.fillRect(x3, y3, x4-x3, y4-y3);
// Hinterrad
g.setColor(Color.BLACK);
g.fillOval(x1, y2-r, 2*r, 2*r);
// Vorderrad
g.fillOval(x2-2*r, y2-r, 2*r, 2*r);
}
}
[/Java]
in einem Demo-Programm (mit Netbeans erstellt und dem zugehörigen grafischen Editor) lasse ich zu Testzwecken immer wieder 2 selbstgebastelte Autos (also keine Autos aus Grafikdateien) sich von von links nach rechts bewegen.
Das einfache Demo-Programm funktioniert. Nur gibt es ein kleines Problem:
Den Hintergrund habe ich im Visual-Editor (VE) in SpielJFrame rot und in SpielJPanel grün gemacht.
Warum erscheint der Hintergrund aber grau ??
Wo ist mein Fehler ?
Habe schon alles Mögliche probiert, hat aber nichts geholfen.
mfg
Ernst
[Java]
package pack30;
public class SpielJFrame extends javax.swing.JFrame {
private SpielJPanel spielJPanel;
/** Creates new form SpielJFrame */
public SpielJFrame() {
initComponents();
spielJPanel = new SpielJPanel();
jPanel1.add(spielJPanel, "Center");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 204, 153));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setLayout(new java.awt.BorderLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(85, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpielJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
[/Java]
[Java]
package pack30;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class SpielJPanel extends javax.swing.JPanel {
private Timer t;
private Auto auto1;
private Auto auto2;
private Image bild;
/** Creates new form SpielJPanel */
public SpielJPanel() {
initComponents();
auto1 = new Auto(this, Color.RED, 0, 0, 100);
auto2 = new Auto(this, Color.BLUE, 50, 80, 100);
auto1.setSchritt(10);
auto2.setSchritt(20);
t = new Timer(20,
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
myupdate();
maleInsBild();
repaint();
}
});
t.start();
}
public void setTimer(int millis) {
t.setDelay(millis);
}
public int getTimerTime() {
return t.getDelay();
}
public void myupdate() {
System.out.println("this.getWidth()="+this.getWidth()+"this.getHeight="+this.getHeight());
auto1.berechnePos();
auto2.berechnePos();
}
public void maleInsBild(){
int sx, sy;
Graphics graphics=null;
sx = this.getSize().width;
sy = this.getSize().height;
bild = createImage(sx, sy);
graphics = bild.getGraphics();
auto1.zeichneAuto(graphics);
auto2.zeichneAuto(graphics);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bild,0,0,null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setBackground(new java.awt.Color(204, 255, 153));
setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
setLayout(new java.awt.BorderLayout());
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
[/Java]
[Java]
package pack30;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
public class Auto {
private SpielJPanel spielJPanel;
private Color farbe;
private int x;
private int y;
private int groesse;
private int schritt;
private static int strassenlaenge;
public Auto(SpielJPanel pSpielJPanel, Color pFarbe, int pOrtX, int pOrtY, int pGroesse){
spielJPanel = pSpielJPanel;
farbe = pFarbe;
x = pOrtX;
y = pOrtY;
groesse = pGroesse;
schritt = 0;
}
public void setX(int pX){
x = pX;
}
public int getX(){
return x;
}
public void setY(int pY){
y = pY;
}
public int getY(){
return y;
}
public void setFarbe(Color pFarbe){
farbe = pFarbe;
}
public Color getFarbe(){
return farbe;
}
void berechnePos(){
if(x <= spielJPanel.getWidth()){
x = x + schritt;
}
else{
x=0;
}
}
public Point getPos() {
return new Point(x, y);
}
void setSchritt(int pSchritt){
schritt = pSchritt;
}
void zeichneAuto(Graphics g){
// Linker oberer Eckpunkt des Wagens
int x1 = x;
int y1 = y;
// Rechter unterer Eckpunkt des Wagens
int x2 = x1 + groesse; // Größe = Länge = x2-ortX
int y2 = y1 + groesse/2; // Höhe = y2-ortY
// Linker oberer Eckpunkt des Fensters
int x3 = x2 - groesse/4; // Fenster
int y3 = y1;
// Rechter unterer Eckpunkt des Fensters
int x4 = x3 + groesse/4; // Fenster
int y4 = y3 + groesse/4;
int r = groesse/8; // RadRadius
int x5 = x1 + 19 + r/2; // Hinterrad
int x6 = x2 - 19 - r/2; // Vorderrad
// Karosserie
g.setColor(farbe);
g.drawRect(x1, y1, x2-x1, y2-y1);
g.fillRect(x1, y1, x2-x1, y2-y1);
// Fenster
g.setColor(Color.BLACK);
g.drawRect(x3-1, y3-1, x4-x3+1, y4-y3+1);
g.setColor(Color.WHITE);
g.fillRect(x3, y3, x4-x3, y4-y3);
// Hinterrad
g.setColor(Color.BLACK);
g.fillOval(x1, y2-r, 2*r, 2*r);
// Vorderrad
g.fillOval(x2-2*r, y2-r, 2*r, 2*r);
}
}
[/Java]