2016-02-01 44 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?)

相关问题