Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java图形问题_Java_Swing_User Interface - Fatal编程技术网

Java图形问题

Java图形问题,java,swing,user-interface,Java,Swing,User Interface,我是一名java初学者,在我的第一个项目中,我开始构建一个垄断游戏 我正在使用图形方法在SWING中构建GUI 出现了两个我找不到答案的问题 第一个问题是,我似乎无法为我的JPanel设置背景色,我以前在同一个项目的另一个JPanel中以相同的方式进行了设置 第二个问题是,我在尝试添加图像时遇到了一个NullPointerException。我通过try/catch成功地纠正了这个错误,但图形似乎无法绘制。同样,我在以前的JPanel中使用了相同的方法加载和添加图像,并且成功了 我应该提到,目前

我是一名java初学者,在我的第一个项目中,我开始构建一个垄断游戏

我正在使用图形方法在SWING中构建GUI

出现了两个我找不到答案的问题

第一个问题是,我似乎无法为我的JPanel设置背景色,我以前在同一个项目的另一个JPanel中以相同的方式进行了设置

第二个问题是,我在尝试添加图像时遇到了一个
NullPointerException
。我通过
try/catch
成功地纠正了这个错误,但图形似乎无法绘制。同样,我在以前的JPanel中使用了相同的方法加载和添加图像,并且成功了

我应该提到,目前我的JFrame包含3个元素,每个元素都在单独的类中,并通过BorderLayout()添加

这是产生问题的类的代码:

    public class MonopolyBoard extends JPanel{


    Image atlantic;
    MonopolyBoard() {
        this.setBorder(new EtchedBorder());

        this.setBackground(new Color( (80), (180), (210) )); //this code dosent work

        //this throws exception without try catch
        try{
        ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/Atlantic Ave.jpg"));
         atlantic = a.getImage();
       }
       catch(NullPointerException e){}
       }

    public void paint(Graphics g){

          }
          Graphics2D g2 = (Graphics2D) g; 
         //this code should draw the image but it dosent
          g2.drawImage(atlantic, 100, 100, null);
          g.drawImage(atlantic, 100, 100, this);

    };
}

我对您的代码感到非常困惑,但我认为问题在于您的面板没有绘制!,你的绘画方法应该是

@Override
public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g; 
    //this code should draw the image but it dosent
     g2.drawImage(atlantic, 100, 100, null);
     g.drawImage(atlantic, 100, 100, this);
}

除非在catch块中打印stacktrace,否则您不会知道。如果构造函数
newImageIcon()
,没有抛出异常,而是返回一个null对象,那么下一行
a.getImage()
,肯定会导致NPE,因为您无法对null对象调用方法

而不是这个

 //this throws exception without try catch         
 try
 {           
     ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
     atlantic = a.getImage();        
 }        
 catch(NullPointerException e){}   
试试这个

// the next line may be wrapped incorrectly due to MarkDown
ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
if (a == null)
{
    System.out.println("Can not find AtlanticAve.jpg");
    return;
}
     atlantic = a.getImage();        
线路

 // the next line may be wrapped incorrectly due to MarkDown
 ImageIcon a = new ImageIcon(this.getClass().getResource("../Card/AtlanticAve.jpg"));
基本上,您需要从查看如果ImageIcon返回空对象,会导致构造函数的原因开始。这会让你走上正轨。这可能是由于getResource()调用失败造成的。找到答案的一个简单方法是将上面的行分成几个部分,并给它们自己的结果变量。这是混乱和无效的,但这就是故障排除有时进行的方式

// using _var_ because I'm too lazy to look up the return types of the methods
var x1 = this.getClass().getResource("../Card/AtlanticAve.jpg");
if (x1 == null)
{
   System.out.println("Can't find my resource");
}

您得到的图片

设置背景色没有任何效果,因为您覆盖了paint()。 绘画负责绘制背景

您需要在绘制方法中绘制背景:

public void paint(Graphics g){
    // paint the background
    g.fill(); // not sure about the parameters
    // paint your image
    g.drawImage(...);
}

不要在那里使用
try/catch
。张贴堆栈跟踪。“Swing程序应该重写
paintComponent()
,而不是重写
paint()
”——“这会在不使用try-catch的情况下引发异常”——这是不正确的。如果块确实抛出异常,则不管try/catch如何,它都会抛出异常。try/catch块所做的是隐藏该异常。这反过来又使得调试变得困难而不可能。您需要理解为什么抛出异常,而不是将它隐藏在地毯下面。考虑从一个简单的开始。OK,我不知道为什么抛出异常。我以前通过构造器在JPAND中显示图像,而这个DIDENTAN使用Eclipse?如果你不应该,它应该阻止你有这么多的问题:)玩得开心!我使用Netbeans,这是我的错,它没有绘制图像,没有在正确的包中,这就是为什么我现在得到异常。我唯一的问题是JPanel isent设置的背景,因为它应该是我能看到的唯一原因是我发布的答案,在你的绘制方法中没有调用任何东西。上面的答案也应该是你以后可能需要的一个有用的提示。如果面板等更新其图形,请在父容器上使用invalidate()。这将通过调用paintComponent方法重新绘制所有子项我已经设法解决了图像问题似乎我没有将图像放入正确的包中现在我唯一剩下的问题是为什么不设置背景?