2016-04-10 7 views
-1

JPanelの周りに無作為にスポーンされるはずの円、線、矩形でいっぱいのJPanelを作成しましたが、円と長方形はJPanel全体でランダムに表示されるのではなく、JPanelの左上隅にのみ表示されます。ラインは完全に機能します。誰かが私を助けてくれるのかしら?JPanelの左上にすべてのシェイプが貼り付けられているのはなぜですか?

あなたはこのようにこれらを定義する必要が
import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 
import javax.swing.JPanel; 

/** 
* 
* 
*/ 
public class RandomShapesPanel extends JPanel 
{ 
    private Color rColor; 
    private Random rGen; 
    private Random num; 

    @Override 
    protected void paintComponent(Graphics g) 
    { 
    super.paintComponent(g); 
    int x = this.getWidth(); 
    int y = getHeight(); 
    int x1, y1, x2, y2; 
    rGen = new Random(); 
    num = new Random(); 
    for(int i = 1; i <= 35; i++) 
    { 
     rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
       rGen.nextInt(256)); 
     g.setColor(rColor); 
     x1 = rGen.nextInt(x - 1) + 1; 
     x2 = rGen.nextInt(x - 1) + 1; 
     y1 = rGen.nextInt(y - 1) + 1; 
     y2 = rGen.nextInt(y - 1) + 1; 
     g.drawLine(x1, y1, x2, y2); 
    } 
    for(int i = 1; i <= 35; i++) 
    { 
     rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
       rGen.nextInt(256)); 
     g.setColor(rColor); 
     x1 = num.nextInt(50) + 10; 
     x2 = num.nextInt(50) + 10; 
     y1 = num.nextInt(50) + 10; 
     y2 = num.nextInt(50) + 10; 
     g.drawRect(x1, y1, x2, y2); 
    } 
    for(int i = 1; i <= 35; i++) 
    { 
     rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
       rGen.nextInt(256)); 
     g.setColor(rColor); 
     x1 = num.nextInt(50) + 10; 
     x2 = num.nextInt(50) + 10; 
     y1 = num.nextInt(50) + 10; 
     y2 = num.nextInt(50) + 10; 
     g.fillRect(x1, y1, x2, y2); 
    } 
    for(int i = 1; i <= 35; i++) 
    { 
     rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
       rGen.nextInt(256)); 
     g.setColor(rColor); 
     x1 = num.nextInt(50) + 10; 
     x2 = num.nextInt(50) + 10; 
     y1 = num.nextInt(50) + 10; 
     y2 = num.nextInt(50) + 10; 
     g.drawOval(x1, y1, x2, y2); 
    } 
    for(int i = 1; i <= 35; i++) 
    { 
     rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
       rGen.nextInt(256)); 
     g.setColor(rColor); 
     x1 = num.nextInt(50) + 10; 
     x2 = num.nextInt(50) + 10; 
     y1 = num.nextInt(50) + 10; 
     y2 = num.nextInt(50) + 10; 
     g.fillOval(x1, y1, x2, y2); 
    } 
    } 
} 
+1

をああ、これは明白である必要があり、今に来て - あなたは行ごとに異なるnextIntを呼んでいる方法を見て、 –

+0

はです四角形や円これはあなたのコード、またはあなたが理解していないコードを借りましたか? –

+0

申し訳ありませんが厳しいですが、自分のコードを見ていないかのように質問されました。将来的には、投稿する前にコードを見てください。 –

答えて

-1

for(int i = 1; i <= 35; i++) 
{ 
    rColor = new Color(rGen.nextInt(256),rGen.nextInt(256), 
      rGen.nextInt(256)); 
    g.setColor(rColor); 
    x1 = num.nextInt(x-50) + 10; 
    x2 = num.nextInt(50) + 10; 
    y1 = num.nextInt(y-50) + 10; 
    y2 = num.nextInt(50) + 10; 
    g.drawRect(x1, y1, x2, y2); 
} 
関連する問題