[英]Graphics not displaying (JAVA, GRAPHICS)
I am building a pathway: a red cross in a white circle, and 3 green squares.我正在建造一条路径:一个白色圆圈中的红十字和 3 个绿色方块。
I have built the items but it does not show up.我已经构建了这些项目,但它没有显示出来。 I have tried many things but I cannot figure our what I am missing... The graphics g is not showing up.
我已经尝试了很多东西,但我无法弄清楚我错过了什么……图形 g 没有出现。
I tried to add getContentPane() and super.paint(g).我尝试添加 getContentPane() 和 super.paint(g)。 I shall maybe add: .add(g) but I am not sure where...
我可能会添加: .add(g) 但我不确定在哪里......
Many thanks in advance for your help (and explanations so that I understand and avoid the mistake next time).非常感谢您的帮助(和解释,以便我下次理解并避免错误)。
public class Robot0 extends JFrame implements ActionListener{
public Robot0(String nom, int larg, int haut){
JFrame fen = new JFrame(); // creer une fenetre, nommee fen, de la classe JFrame
fen.setTitle(nom); // definir un titre de fenetre
fen.setSize(larg, haut); // definir la taille de fenetre
fen.setLocationRelativeTo(null); // creer fen au centre de l’ecran
fen.setResizable(false); //prevent redimensionning
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Femer la fenetre (termine l’execution) quand on clique sur la croix rouge de fen
fen.setVisible(true); // rendre visible la fenetre fen
}
Timer tm = new Timer(10,this);
private int posX = 0; // (posX,posY) une position dans le panneau `a cr´eer
private int posY = 0;
private int velX = 1;
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
public void paint(Graphics g){
super.paint(g);
//Draw the pathway in a 800x600 pixels panel made up of 5 segments (using draw.Polyline)
int xt[] = {50, 50, 250, 250, 350, 350};
int yt[] = {50, 150, 150, 50, 50, 150};
g.drawPolyline(xt, yt, 6);
//On the pathway, draw 3 squares (the 3 rooms)
//J'ai place les carres tel que son centre soit sur la ligne de trajet
g.setColor(Color.GREEN);
g.drawRect(35, 135, 30,30);
g.drawRect(235, 35, 30,30);
g.drawRect(335, 135, 30,30);
//J'ai place le robot tel que son centre soit sur la ligne de trajet
g.setColor(Color.WHITE);
g.fillOval(40+posX,40+posY,20,20);
g.setColor(Color.RED);
g.drawLine(45+posX,50+posY,55+posX,50+posY);
g.drawLine(50+posX,45+posY,50+posX,55+posY);
tm.start();
}
//ACTIONS TO BUILD //
// Build the Panel
public static void main(String[] args){
Robot0 r = new Robot0("Robot0", 800, 600);
r.getContentPane().setBackground(Color.BLUE);
}
}
Robot0
extends JFrame
, so is itself a JFrame
. Robot0
扩展JFrame
,因此它本身就是JFrame
。 You therefore don't need to, and probably shouldn't, create a new JFrame
in the constructor or Robot0
.因此,您不需要也可能不应该在构造函数或
JFrame
中创建新的Robot0
。
Replace代替
public Robot0(String nom, int larg, int haut){
JFrame fen = new JFrame(); // creer une fenetre, nommee fen, de la classe JFrame
fen.setTitle(nom); // definir un titre de fenetre
fen.setSize(larg, haut); // definir la taille de fenetre
fen.setLocationRelativeTo(null); // creer fen au centre de l’ecran
fen.setResizable(false); //prevent redimensionning
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Femer la fenetre (termine l’execution) quand on clique sur la croix rouge de fen
fen.setVisible(true); // rendre visible la fenetre fen
}
with和
public Robot0(String nom, int larg, int haut){
setTitle(nom); // definir un titre de fenetre
setSize(larg, haut); // definir la taille de fenetre
setLocationRelativeTo(null); // creer fen au centre de l’ecran
setResizable(false); //prevent redimensionning
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Femer la fenetre (termine l’execution) quand on clique sur la croix rouge de fen
setVisible(true); // rendre visible la fenetre fen
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.