2013-10-31 72 views
0

試圖製作2^n大小的網格,詢問用戶'n'。我沒有在2^n部分編碼,這對我來說也有點困惑。但是現在當我從用戶那裏得到輸入時,我的主板不能正確顯示。我的drawLine是貫穿整個棋盤的對角線。使用drawPanel創建網格時遇到問題

如何讓電路板正確顯示?

這裏是我的代碼:

import java.awt.*; 
import java.util.*; 
public class DrawingPanelTest2{ 

    public static void main(String args[]){ 

//  System.out.println("How big do you want your Tromino grid?"); 
//  System.out.println("Please enter a perfect power of 2."); 
//  int size = stdin.nextInt(); 

     //create a drawing panel of width=400px and height=400px 
     DrawingPanel panel = new DrawingPanel(400, 400); 
     //set the background of the panel to CYAN 
     panel.setBackground(Color.LIGHT_GRAY); 
     //create a graphic object for the panel 
     Graphics g = panel.getGraphics(); 

     //draw square 
     drawFigure_1(g,0,0); 

    } 

    public static void drawFigure_1(Graphics g,int x, int y) { 
     Scanner stdin = new Scanner(System.in); 

     System.out.println("How big do you want your Tromino grid?"); 
      System.out.println("Please enter a perfect power of 2."); 
      int size = stdin.nextInt(); 
     //set your drawing color to red 
     g.setColor(Color.BLACK); 
     for (int i = 1; i <= size; i++) { 
      //draw a rectangle, (x,y) is the top-left cordiante of the rectangle, and ((i*z), (i*z)) 
      //are the width and height of the rectangle 
      g.drawRect(x, y, i * size, i * size); 
      g.drawLine(x, y, i *size, i *size); 
     } 
     g.setColor(Color.BLACK); 
    } 
} 

回答

2

Graphics g = panel.getGraphics();並不怎麼風俗畫完成。

Scanner stdin = new Scanner(System.in);是不是你應該如何與用戶從GUI

開始的範圍內,採取一看Creating a GUI with SwingPerforming Custom Painting

看看在Graphics Java Docs

Graphics#drawRect交互需要4個參數,rectenagle的x,y位置(左上角)和widthheight,而Graphics#drawLine需要x1,y1,這是起點,並且x2,y2這是終點。

所以,你要畫一條水平線,你需要使用更多的東西一樣g.drawLine(x, y, i * size, i);或垂直線,更多的東西一樣g.drawLine(x, y, i, i * size);

如果你想畫一個網格,那麼你就需要循環,一個水平和一個垂直。您還需要更新的長方形的x/y,使它們被放在糾正,因此而不是修改的尺寸參數,你應該修改位置參數

1

嘗試是這樣的:

import java.util.*; 
import java.awt.*; 
import javax.swing.*; 
class myjava{ 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     double pw = input.nextDouble(); 
     myPan panel = new myPan(pw); 
     JFrame application = new JFrame(); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.add(panel);   
     application.setSize(400, 400); 
     application.setVisible(true); 
    } 
} 

class myPan extends JPanel{ 
    public double pow; 
    public myPan(double p){ 
     pow = p; 
    } 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     double num = Math.pow(2,pow); 
     double across; 
     double up; 
     if(pow % 2 == 0){ //is a square 
      System.out.println("square"); 
      across = Math.pow(num,0.5); 
      up = across; 
     } 
     else{ 
      System.out.println("not"); 
      double x = Math.floor(pow/2); 
      double y = x + 1; 
      across = Math.pow(2,x); 
      up = Math.pow(2,y); 
     } 
     System.out.println(across); 
     System.out.println(up); 
     // 
     // 
     double wid = 400/across; //width of one 
     double hi = 400/up; //height of one 
     double nowX = 0; 
     double nowY = 0; 
     for(int i = 0; i < up; i++){ //top to bottom 
      nowX = 0; 
      for(int j = 0; j < across; j++){ 
       //System.out.print("*"); 
       g.setColor(Color.BLACK); 
       g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi); 
       nowX = nowX + wid; 
      } 
      nowY = nowY + hi; 
      //System.out.print("\n"); 
     } 
    } 
} 
+0

你是這個男人從來沒有見過我的評論!!!! – jSeesFor3ver

+0

這裏現在我有了這個,我有一個tromino拼圖程序編碼,但需要結合我的圖形。這就是我所得到的,我差不多完成了。你能看看嗎? http://stackoverflow.com/questions/19717359/tromino-program-using-graphics – jSeesFor3ver