簡體   English   中英

JPanel幫助-繪制矩形

[英]JPanel help- Drawing rectangles

我是第一次使用圖形,但是有一個問題,除了背景(標識為Bcolor)之外什么都沒有顯示。 我正在嘗試使用顏色為Lcolor的fill(new Rectangle)方法制作帶有線的網格。 我運行了調試器,似乎沒有任何語法,運行時或邏輯錯誤。 有任何解決這個問題的方法嗎? 謝謝!

    public static void drawWindow(int Lcolor, int lineSize, int cellSize, int Bcolor){
    Graphics g = createWindow(cellSize, Bcolor, lineSize).getGraphics();
    Graphics2D g2d = (Graphics2D)g;
    for(int i = 0; i <= (frameValues(cellSize, lineSize)[2]); i++){
        g2d.setColor(new Color(Lcolor, Lcolor, Lcolor));
        g2d.fill(new Rectangle(lineSize * i + cellSize * i, 0, lineSize, frameValues(cellSize, lineSize)[1]));
        //.drawLine(xpos,ypos,xsize,ysize)
    }//for loop
    for(int j = 0; j < (frameValues(cellSize, lineSize)[3]); j++){
        g2d.setColor(new Color(Lcolor, Lcolor, Lcolor));
        g2d.fill(new Rectangle(0, lineSize * j + cellSize * j, frameValues(cellSize, lineSize)[0], lineSize));
    }//for loop

}

這是createWindow()方法:

public static JPanel createWindow(int cellSize, int Bcolor, int lineSize){
    JFrame frame = new JFrame("Evolution Of Life");
    frame.setSize(new Dimension(frameValues(cellSize, lineSize)[0], frameValues(cellSize, lineSize)[1]));
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    //making it visible
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    //creating panel
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
    panel.setBackground(new Color(Bcolor, Bcolor, Bcolor));
    return panel;
}


//If you need anymore information, I'll be happy to supply.

不要在JPanel上調用getGraphics 如果要繪制到JPanel ,請重寫它的paintComponent方法,並按照教程中的說明使用傳遞給該方法的Graphics對象。

JPanel panel = new JPanel(){

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(...);
        g.fill(...);
    }
};
frame.add(panel);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM