2016-02-01 48 views
0

我在這裏有一些代碼,它以指數形式表示,但是,它表示了錯誤的方式。負指數增長。這是我的代碼,我試圖翻轉它。我會努力的,但如果你有答案,請告訴我。如何翻轉drawRect以便繪製?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class Graphics extends JPanel{ 
    public static void main(String[] args) {  
     JFrame f =new JFrame ("Function"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Graphics g = new Graphics(); 
     f.add(g); 
     f.setSize(700, 700); 
     f.setVisible(true); 
} 
public void paintComponent (java.awt.Graphics g) { 
    super.paintComponent(g); 
    int i = this.getWidth()-this.getWidth() + 5; 

    int xoffset = this.getWidth()/2; 
    int yoffset = this.getHeight()/2; 

    for (int x = 0 ; x < 20 ; x++){ 
     g.setColor(Color.BLACK); 
     this.setBackground(Color.WHITE); 
     int p = x*x; 
     g.fillRect(i+xoffset,10+yoffset,5,p); 
     i = i + 10; 
     } 
    } 
    } 

有沒有人知道如何解決這個問題?

+1

更改'x' /'''屬性 – MadProgrammer

回答

1

更改其在該矩形開始從繪製x/y位置(它總是吸引右/下)

所以不是

g.fillRect(i+xoffset,10+yoffset,5,p); 

你可以有...

g.fillRect(i + xoffset, 10 + yoffset - p, 5, p); 

Onwards and upwards

我不知道你的意圖是爲int i = this.getWidth()-this.getWidth() + 5;,但顯然沒有意義(width - width == 0?)

相關問題