Java 为什么我所有的形状都卡在我的JPanel的左上角?

Java 为什么我所有的形状都卡在我的JPanel的左上角?,java,swing,user-interface,jpanel,shapes,Java,Swing,User Interface,Jpanel,Shapes,我已经创建了一个JPanel,其中充满了圆、线和矩形,它们应该在JPanel周围随机生成,但是圆和矩形只显示在JPanel的左上角,而不是在整个JPanel中随机出现,而线工作得很好。我想知道是否有人能帮我 import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel; /** * * */ public class RandomShapesPane

我已经创建了一个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);
    }
    }
}
导入java.awt.Color;
导入java.awt.Graphics;
导入java.util.Random;
导入javax.swing.JPanel;
/**
*
* 
*/
公共类RandomShapePanel扩展了JPanel
{
私人色彩;
私有随机rGen;
私有随机数;
@凌驾
受保护组件(图形g)
{
超级组件(g);
int x=this.getWidth();
int y=getHeight();
int-x1,y1,x2,y2;
rGen=新随机数();
num=新随机数();

对于(int i=1;i您必须这样定义:

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);
}

用于(int i=1;哦,来吧,这应该是很明显的——看看你对nextInt的调用有什么不同,因为线条、矩形和圆圈是你的代码,还是你借用了你不懂的代码?很抱歉,你的问题被问到了,好像你根本没有看自己的代码。将来,请至少看看你自己您的代码在发布之前…………您好?