英文:
i am trying to color the JPanel window Background.but i got a JPanel window without any color after execution.what error in code?
问题
package BrickBreaker;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Gameplay extends JPanel {
private boolean play = false;
private int totalBrick = 28;
private Timer time;
private int delay = 8;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private int playerX = 350;
public Gameplay() {
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
}
}
英文:
I am trying to colour the JPanel window Background. but I got a JPanel window without any colour after execution. What error in code?
package BrickBreaker;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Gameplay extends JPanel {
private boolean play = false;
private int totalBrick = 28;
private Timer time;
private int delay = 8;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private int playerX = 350;
public Gameplay() {
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
}
}
答案1
得分: 2
检查颜色是否需要大写:g.setColor(Color.BLACK);
如果不需要,尝试使用jPanel.setBackground(Color.BLACK);
。
英文:
Check if the Color needs to be capital: g.setColor(Color.BLACK);
if not try to use jPanel.setBackground(Color.BLACK);
答案2
得分: 0
你应该像这样做(我假设您有一个 JFrame,面板已添加到其中):
public class Gameplay extends JPanel {
private boolean play = false;
private int totalBrick = 28;
private Timer time;
private int delay = 8;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private int playerX = 350;
public Gameplay() {
setBackground(Color.black);
}
// 如果您要覆盖此方法,您需要调用父类的 paintComponent 方法。
// 否则,setBackground 方法将无效。
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g = g.create();
// g.setColor(Color.black); // 您不需要这行
// g.fillRect(1, 1, 692, 592); // 现在您不需要这行
// 但是,如果您想要绘制一个红色圆圈,您需要改变颜色
// 用于绘制红色圆圈
g.setColor(Color.red);
g.fillOval(100, 100, 50, 50);
g.dispose();
}
}
英文:
You should do it like this (I am assuming you have JFrame to which the panel is added).
public class Gameplay extends JPanel {
private boolean play = false;
private int totalBrick = 28;
private Timer time;
private int delay = 8;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private int playerX = 350;
public Gameplay() {
setBackground(Color.black);
}
// if your going to override this you need to call the parent's paintComponent.
// otherwise the setBackground would not work.
public void paintComponent(Graphics g) {
super.paintComponent(g);
g = g.create();
// g.setColor(Color.black);// you don't need this
// g.fillRect(1, 1, 692, 592); // now you don't need this
// But if you want to draw a red circle, you do need to change the color
// for that
g.setColor(Color.red);
g.fillOval(100,100,50,50);
g.dispose();
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论