使用Java将多个图像添加到GPanel

时间:2016-06-07 00:06:04

标签: java arrays graphics2d

我正在学校用java构建二十一点游戏,我似乎无法弄清楚如何将前四张卡添加到GPanel。卡片阵列被洗牌,数组中的字符串与图像的文件名匹配。我可以在数组中加载第一张卡而不是其他三张卡。任何帮助都会很棒。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <fragment
        android:id="@+id/fragment_red"
        android:name="fragmentexample.example.com.myapplication.Fragment1"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_weight="1"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            >
            <fragment
                android:id="@+id/fragment_blue"
                android:name="fragmentexample.example.com.myapplication.Fragment2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />

            <fragment
                android:id="@+id/fragment_yellow"
                android:name="fragmentexample.example.com.myapplication.Fragment5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                />

        </LinearLayout>

        <fragment
            android:id="@+id/fragment_green"
            android:name="fragmentexample.example.com.myapplication.Fragment3"
            android:layout_height="0dp"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            />

    </LinearLayout>

    <fragment
        android:id="@+id/fragment_puple"
        android:name="fragmentexample.example.com.myapplication.Fragment4"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_weight="1"
        />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的代码中有3个主要问题。

第一个问题是由[currentPosition - i]引起的,因为结果总是等于0,因此数组索引0处的图像/卡将是唯一将被绘制的图像/卡。

第二个问题是你只绘制一个图像,因为g2.drawImage仅在for循环之后调用,而是应该在for循环中,以便绘制所有4个图像/卡。

第三个问题是你总是在同一个地方绘制你的图像,所以即使你在绘制所有4个图像的地方,你也只会看到最后一个,因为它涵盖了以前的图像。

试试这个,它应该打印所有4个图像(假设你有一个435x112面板):

public void paintComponent(Graphics g) {
    //System.out.println(gameInProgress);

    if(gameInProgress == true) {
        super.paintComponent(g);

        //Add a couple new variables
        int x = 115;

        Graphics2D g2 = (Graphics2D) g;
        Image image = null;
        int i;
        for (i = 0; i < 4; i++) {
            image = Toolkit.getDefaultToolkit().getImage("images/"+deck[i]+".png");
            //New location of g2.drawImage
            //The X positions should change each time
            g2.drawImage(image, x, 5, 80, 107, this);
            //Change the X location of the next image
            x = x + 80;
        }
        //Moved g2.drawImage from here to above the bracket
        g2.finalize();
    }
}
相关问题