2014-11-25 62 views
0

我似乎無法弄清楚爲什麼我的同心圓不排列。我的循環對我來說看起來是正確的,測量結果是正確的,但由於某種原因,最後幾圈是偏離中心的。這是我遇到的第一個問題。第二個問題是,我似乎無法在每個方塊中打印同心圓。再次,我似乎無法在我的邏輯中發現任何問題,但顯然存在一個問題。任何幫助都會很棒。圖形漆組件和迴路故障

這應該是最終產品

enter image description here

*現在,這是我的源代碼 - ExampleGUI.java *

import javax.swing.*; 

public class ExampleGUI { 
public static void main(String args []) { 


    JFrame frame = new JFrame("Example Graphics"); 
    ExamplePanel panel = new ExamplePanel(); 

    frame.getContentPane().add(panel); 
    frame.setDefaultCloseOperation(3); 
    frame.pack(); 
    frame.setVisible(true); 

} 
} 

* ExamplePanel.java *

import java.awt.*; 

import javax.swing.*; 



public class ExamplePanel extends JPanel{ 

public ExamplePanel() { 
    setPreferredSize(new Dimension (600, 600)); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 


    int x = 0; 
    int x2 = 5; 
    int y = 500; 
    int y2 = 505; 
    int w = 100; 
    int w2 = 90; 
    int h = 100; 
    int h2 = 90; 
    int i, j, k; 

    for(j = 1; j < 7; j++) { 
     x = 0; 
     x2 = x + 5; 

     for(i = 1; i < 7; i++) { 
      if ((i + j) % 2 == 0) { 
       g.setColor(Color.white); 
      } else { 
       g.setColor(Color.yellow); 
      } 
      g.fillRect(x, y, w, h); 
      g.setColor(Color.black); 
      g.drawRect(x, y, w, h); 
      g.setColor(Color.green); 
      g.fillOval(x2, y2, w2, h2); 

       for(k = 1; k < 7; k++) { 
        g.setColor(Color.black); 
        g.drawOval(x2, y2, w2, h2); 
        x2 = x2 + 5; 
        y2 = y2 + 5; 
        w2 = w2 - 10; 
        h2 = h2 - 10; 
       } 
      x = x + w; 
      x2 = x2 + w2 + 10; 

     } 


     x = x + w; 

     y = y - h; 
     y2 = (y2 - h2) - 10; 
    } 

} 
} 

*這是我的程序在運行時的樣子。它看起來並不像其他圖片出於某種原因*

enter image description here

+1

我想幫助你,但是通過盯着圖形來描述。其實我無法從圖表中看出你在說什麼。 – MeBigFatGuy 2014-11-25 02:05:23

+0

所以...你的問題是什麼?簡單地說我的代碼不起作用不會幫助人們瞭解您的問題並提供幫助 – 2014-11-25 02:07:08

+0

該圖像是我想要的樣子,但我的代碼看起來不像這樣 - 它非常接近。 – KangarooRIOT 2014-11-25 02:07:20

回答

2

基本上,「螺旋」循環修改所需的其他地方

我會做什麼,是變量狀態創建一個新的一系列變量,初始化爲當前狀態,並修改這些,而不是...

   int ix = x2; 
       int iy = y2; 
       int ih = h2; 
       int iw = w2; 
       for (k = 1; k < 7; k++) { 
        g.setColor(Color.black); 
        g.drawOval(ix, iy, iw, ih); 
        ix = ix + 5; 
        iy = iy + 5; 
        iw = iw - 10; 
        ih = ih - 10; 
       } 

enter image description here

+0

我在哪裏放置你發佈的代碼?感謝您的幫助! – KangarooRIOT 2014-11-25 02:34:09

+0

用它替換你當前的螺旋循環,這是第三個「for-loop」位... – MadProgrammer 2014-11-25 02:37:48

+0

太棒了!謝謝 – KangarooRIOT 2014-11-25 03:13:49