FillRect()无效

时间:2016-02-10 20:09:23

标签: java jpanel paintcomponent

我试图制作一个在JFrame中绘制绿色方块的程序,但是当我尝试它时,方块是不可见的。我不知道他们是不是被吸引还是因为某些原因而不能被看见。

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    //make sure nothing happens when at main menu
    if(level >= 1)
    {
        //determine square size
        size[0] = playScreen.getWidth() / squares;
        size[1] = playScreen.getHeight() / squares;
        System.out.println("The width of a single square is " + size[0]);
        System.out.println("The height of a single square is " + size[1]);
        //draw the cubes
        g.setColor(Color.GREEN);
        if(currentView == 0)
        {
            //front view
            for(int i = 0; i < xPosTargets.size(); i++)
            {
                g.fillRect(xPosTargets.get(i)*size[0], zPosTargets.get(i)*size[1], size[0], size[1]);
            }
        }
        else if(currentView == 1)
        {
            //side view
            for(int i = 0; i < xPosTargets.size(); i++)
            {
                g.fillRect((yPosTargets.get(i))*size[0], (zPosTargets.get(i))*size[1], size[0], size[1]);
            }
        }
        else
        {
            //top view
            for(int i = 0; i < xPosTargets.size(); i++)
            {
                g.fillRect((xPosTargets.get(i))*size[0], (squares - zPosTargets.get(i))*size[1], size[0], size[1]);
            }
        }
    }
}

我认为变量的值不正确,但当我尝试用System.out.println()打印它们时,它们就是它们应该是的。变量类型如下: xPosTargetsyPosTargetszPosTargetsArrayList<Integer> sizeint[] playScreenJPanel squares是一个int,是的,当它第一次绘画时确实有一个值

1 个答案:

答案 0 :(得分:0)

也许尝试var boardIdGlobal =[boardid1,boardid2]; var boardIdGlobalTemp, urlChecklistsGlobal, urlListsGlobal; contactStatusGlobalArray = []; function checklistStatus(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ var checklist = JSON.parse(xmlhttp.responseText); for(i=0;i<checklist.length;i++){ for(t=0;t<checklist[i].cards.length;t++){ for(x=0;x<checklist[i].checkItems.length;x++){ if(checklist[i].checkItems[x].name.search("Contact")===0){ contactStatusGlobalArray.push(["unknown",checklist[i].cards[t].idList,checklist[i].checkItems[x].name,checklist[i].checkItems[x].state]); } } } }; }; }; xmlhttp.open("GET", urlChecklistsGlobal, true); xmlhttp.send(); console.log(contactStatusGlobalArray); } function init(){ for(i=0;i<boardIdGlobal.length;i++){ boardIdGlobalTemp = boardIdGlobal[i]; urlChecklistsGlobal = "https://api.trello.com/1/boards/" + boardIdGlobalTemp + "/checklists?cards=open&card_fields=idList&key=[key]&token=[token]" urlListsGlobal = "https://api.trello.com/1/boards/" + boardIdGlobalTemp + "/lists?key=[key]&token=[token]" checklistStatus(); } }; window.onload = init; 代替paint(Graphics g)