Java paintComponent内部的if语句使其不进行绘制
我想写一个纸牌游戏。Java paintComponent内部的if语句使其不进行绘制,java,if-statement,paintcomponent,Java,If Statement,Paintcomponent,我想写一个纸牌游戏。 我想在游戏开始时调用repaint(),并绘制整个游戏组 一次只调用第一次重新绘制,但是当我添加if语句时,它 不再画画。 下面是带有if语句的代码: private void paintInitialDeck(Graphics g, Card card){ card.paintCard(g); } public void paintComponent(Graphics g) { super.paintComponent(g)
我想在游戏开始时调用
repaint()
,并绘制整个游戏组一次只调用第一次重新绘制,但是当我添加
if语句时,它
不再画画。
下面是带有if语句的代码:
private void paintInitialDeck(Graphics g, Card card){
card.paintCard(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (initialDrawing) {
Card card;
for (int i = 0; i < deck.size(); i++) {
card = deck.get(i);
card.setY((int) (50 + 0.2 * i));
card.setX((int) (250 + 0.2 * i));
paintInitialDeck(g, card);
}
initialDrawing = false;
}
}
private void paintInitialDeck(图形g、卡片){
卡片、油漆卡片(g);
}
公共组件(图形g){
超级组件(g);
if(初始图纸){
卡片;
对于(int i=0;i
它不起作用,但是如果我删除if语句和initialDrawing=false
然后它就工作了。
问题是,我需要if语句。
有人能帮我更好地理解这一点吗
注:initialDrawing首先设置为true。而且,它实际上是initialDrawing=false
这使得整个过程不必绘制。发生的事情是,使用paintComponent
方法初始化该类时,paintComponent
方法被调用一次。因此,绘制将发生一次,但之后不会再次发生,因为在初始实例化中第一次调用paintComponent
后,initiaDrawing
被设置为false。因此,在程序中调用repaint()
将不起作用,因为initialDrawing
在第一次启动绘制过程后将为false
我不确定您是否在第一次启动程序时看到绘制,但这就是为什么,它不允许您在程序启动后再次绘制
一个可能的修复方法可能是将initialDrawing
初始化为false,以便它最初不会绘制。然后,无论哪个组件调用repaint()
方法,在操作中,首先将initialdrawing
设置为true,然后调用repaint()
。禁用该组件,以便无法再次调用repaint()
。(或这种逻辑性质的东西)
这里有一个例子
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisablePaint extends JPanel {
ImagePanel panel;
JButton button;
boolean intialDraw = false;
public DisablePaint() {
button = new JButton("Paint Once");
setLayout(new BorderLayout());
panel = new ImagePanel();
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
intialDraw = true;
panel.repaint();
button.setEnabled(false);
}
});
}
private class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (intialDraw) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 200, 200);
initialDraw = false;
}
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new DisablePaint());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您可以看到,initialDraw
首先设置为false,所以它不会在程序的初始启动时绘制。然后,我在执行的操作中调用repaint()
,但首先将intialDrawing
设置为true。然后禁用按钮,不允许另一个初始绘制所发生的事情是,使用paintComponent
方法初始化该类时,paintComponent
方法被调用一次。因此,绘制将发生一次,但之后不会再次发生,因为在初始实例化中第一次调用paintComponent
后,initiaDrawing
被设置为false。因此,在程序中调用repaint()
将不起作用,因为initialDrawing
在第一次启动绘制过程后将为false
我不确定您是否在第一次启动程序时看到绘制,但这就是为什么,它不允许您在程序启动后再次绘制
一个可能的修复方法可能是将initialDrawing
初始化为false,以便它最初不会绘制。然后,无论哪个组件调用repaint()
方法,在操作中,首先将initialdrawing
设置为true,然后调用repaint()
。禁用该组件,以便无法再次调用repaint()
。(或这种逻辑性质的东西)
这里有一个例子
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisablePaint extends JPanel {
ImagePanel panel;
JButton button;
boolean intialDraw = false;
public DisablePaint() {
button = new JButton("Paint Once");
setLayout(new BorderLayout());
panel = new ImagePanel();
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
intialDraw = true;
panel.repaint();
button.setEnabled(false);
}
});
}
private class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (intialDraw) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 200, 200);
initialDraw = false;
}
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new DisablePaint());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您可以看到,initialDraw
首先设置为false,所以它不会在程序的初始启动时绘制。然后,我在执行的操作中调用repaint()
,但首先将intialDrawing
设置为true。然后禁用按钮,不允许另一个初始绘制所发生的事情是,使用paintComponent
方法初始化该类时,paintComponent
方法被调用一次。因此,绘制将发生一次,但之后不会再次发生,因为在初始实例化中第一次调用paintComponent
后,initiaDrawing
被设置为false。因此,在程序中调用repaint()
将不起作用,因为initialDrawing
在第一次启动绘制过程后将为false
我不确定您是否在第一次启动程序时看到绘制,但这就是为什么,它不允许您在程序启动后再次绘制
一个可能的修复方法可能是将initialDrawing
初始化为false,以便它最初不会绘制。然后,无论哪个组件调用repaint()
方法,在操作中,首先将initialdrawing
设置为true,然后调用repaint()
。禁用该组件,以便无法再次调用repaint()
。(或这种逻辑性质的东西)
这里有一个例子
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisablePaint extends JPanel {
ImagePanel panel;
JButton button;
boolean intialDraw = false;
public DisablePaint() {
button = new JButton("Paint Once");
setLayout(new BorderLayout());
panel = new ImagePanel();
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
intialDraw = true;
panel.repaint();
button.setEnabled(false);
}
});
}
private class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (intialDraw) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 200, 200);
initialDraw = false;
}
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new DisablePaint());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
您可以看到,initialDraw
首先设置为false,所以它不会在程序的初始启动时绘制。然后,我在执行的操作中调用repaint()
,但首先将intialDrawing
设置为true。然后禁用按钮,不允许另一个初始绘制所发生的事情是,使用paintComponent
方法初始化该类时,paintComponent
方法被调用一次。因此,绘制将发生一次,但之后不会再次发生,因为在初始实例化中第一次调用paintComponent
后,initiaDrawing
被设置为false。因此,在程序中调用repaint()
将不起作用,因为initialDrawing
将是fals