随机圈JAVA [英] Random circles JAVA

查看:101
本文介绍了随机圈JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个图形用户界面,用于绘制圈数,并将它们绘制到随机位置/大小的drawPanel中。在我的actionListener上,当我尝试绘制圆时,它会在我的drawOval中给出红线



第一类:

  import java.awt.BorderLayout; 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/ **
*
* @作者Chris
*
* /
公共类CirclesPanel扩展JPanel {
private JButton绘制,清晰;
private JTextArea textArea;
私人JPanel面板,drawPanel,buttonPanel;
private int count;
$ b $ / **构造函数
*构建框架
* /
public CirclesPanel(){

//创建按钮和textArea
draw = new JButton(Draw);
clear = new JButton(Clear);
textArea = new JTextArea(1,10);
textArea.setBorder(BorderFactory.createTitledBorder(Circles));

//创建面板
JPanel面板= new JPanel();
panel.setLayout(new BorderLayout());
setPreferredSize(new Dimension(620,425));
//创建子面板drawPanel
JPanel drawPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(450,400));
drawPanel.setBackground(Color.BLACK);
//创建子面板buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,1));
//将所有内容添加到框架
add(panel);
add(buttonPanel,BorderLayout.WEST);
add(drawPanel,BorderLayout.EAST);
buttonPanel.add(textArea);
buttonPanel.add(draw);
buttonPanel.add(clear);
//读取单击绘图按钮
draw.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
count = Integer.parseInt(textArea.getText()); //获取
中的计数repaint(); //重新绘制图片以添加圆圈
}
});
//读取清除按钮被点击
clear.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
count = 0; //将计数设置为0因此没有任何内容被绘制
repaint(); //重新绘制窗口
}
});



$ b / **绘制组件
*绘制随机圆圈
* /
public void paintComponent(Graphics g){
Random generator = new Random();
int x,y,直径;
for(int i = 0; i< count; i ++){//循环接受计数并执行此操作x次
g.setColor(Color.BLUE); //设置颜色到蓝色
x = generator.nextInt(90); //随机位置为x
y = generator.nextInt(90); //随机位置为y
diameter = generator.nextInt(30) ; //随机大小
g.fillOval(x,y,diameter,diameter); //绘制圆形
}
}
}

第二课程

  import javax。 swing.JFrame; 


public class Circle {
public static void main(String [] args){
JFrame frame = new JFrame(Cicles HW9);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane()。add(new CirclesPanel());

frame.pack();
frame.setVisible(true);



$ b


解决方案

当您点击按钮时,您想在 drawPanel 上绘制一些随机圆圈 。我给你写了一个简体版,以显示工作情况。

我只保留 drawButton > paintPanel 让事情变得简单。

  public class PaintFrame extends JFrame {

private JPanel content = new JPanel();
private JButton drawButton = new JButton(Draw);
private PaintPanel paintPanel = new PaintPanel();

public PaintFrame(){

getContentPane()。add(content);
content.setLayout(new BorderLayout());

drawButton.setSize(100,500);
drawButton.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e){
// drawButton被触发,重新绘制paintPanel
paintPanel.repaint();
}
});
content.add(drawButton,BorderLayout.WEST);
content.add(paintPanel,BorderLayout.CENTER);
}

}

您需要一个新的类来扩展JPanel并重写 paintComponent 方法为您完成绘画作业。这确保您正在绘制面板

  class PaintPanel扩展JPanel {
$ b $ public PaintPanel(){
setSize(500, 500);
setBackground(Color.BLACK);


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Random random = new Random();
g.setColor(Color.WHITE);
//绘制5个随机圆圈
int count = 5; (250),
random.nextInt(250),
(int i = 0; i


$ b $ / code $ / pre
$ b $主要类


  public class DrawMain {

public static void main(String [] args){

JFrame frame = new PaintFrame();
frame.setDefaultCloseOperation(PaintFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setVisible(true);

}

}


I am trying to create a GUI that will take in the number of circles to draw, and draw them in drawPanel with random locations/sizes. On my actionListener, when I try to draw the circle, it gives me red lines on my drawOval

1st class:

 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Graphics;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Random;
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JPanel;
 import javax.swing.JTextArea;

/**
 * 
 * @author Chris
 *
 */
 public class CirclesPanel extends JPanel{
 private JButton draw, clear;
 private JTextArea textArea;
 private JPanel panel, drawPanel, buttonPanel;
 private int count;

 /**constructor
* builds the frame
*/
 public CirclesPanel(){

//creates buttons and textArea
  draw = new JButton("Draw"); 
  clear = new JButton("Clear");
  textArea = new JTextArea(1,10);
  textArea.setBorder(BorderFactory.createTitledBorder("Circles"));

//creats panel
 JPanel panel = new JPanel();
 panel.setLayout(new BorderLayout());
 setPreferredSize(new Dimension(620, 425));
//creates subpanel drawPanel
 JPanel drawPanel = new JPanel();
 drawPanel.setPreferredSize(new Dimension(450,400));
 drawPanel.setBackground(Color.BLACK);
//creates subpanel buttonPanel
 JPanel buttonPanel = new JPanel();
 buttonPanel.setLayout(new GridLayout(3,1));
//adds all the content to the frame
 add(panel);
 add(buttonPanel, BorderLayout.WEST);
 add(drawPanel, BorderLayout.EAST);
 buttonPanel.add(textArea);
 buttonPanel.add(draw);
 buttonPanel.add(clear);
//reads if the draw button is clicked
 draw.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
    {
      count =Integer.parseInt(textArea.getText());//takes the count in
      repaint();//repaints the picture to add the circles
    }
 }); 
//reads if the clear button is clicked
 clear.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
            {
            count=0;//sets the count to 0 so nothing is painted
              repaint();//repaints the window
            }
         }); 



 }
/**Paint component
 * draws the random circles
 */
  public void paintComponent(Graphics g) {
 Random generator = new Random();
 int x, y, diameter;
 for(int i = 0; i < count; i++){ //loop that takes the count and does this "x" times
  g.setColor(Color.BLUE);//sets color to blue
  x = generator.nextInt(90);//random location for x
  y = generator.nextInt(90);//random location for y
  diameter = generator.nextInt(30);//random size
  g.fillOval(x, y, diameter, diameter);//draws the circle
    }
}
 }

2nd class

import javax.swing.JFrame;


public class Circles { 
public static void main(String[]args){
JFrame frame = new JFrame("Cicles HW9");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new CirclesPanel());

frame.pack();
frame.setVisible(true);


}
}

解决方案

What you want to do is drawing some random circles on the drawPanel when button clicked. I write you a simplified version to show how things work.

I only keep the drawButton and paintPanel to keep things simple.

public class PaintFrame extends JFrame {

    private JPanel content = new JPanel();
    private JButton drawButton = new JButton("Draw");
    private PaintPanel paintPanel = new PaintPanel();

    public PaintFrame() {

        getContentPane().add(content);
        content.setLayout(new BorderLayout());

        drawButton.setSize(100, 500);
        drawButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // drawButton is fired, repaint the paintPanel
                paintPanel.repaint();
            }
        });
        content.add(drawButton, BorderLayout.WEST);
        content.add(paintPanel, BorderLayout.CENTER);
    }

}

You need a new class extending the JPanel and override the paintComponent method to do the paint job for you. This makes sure you are drawing on the panel.

class PaintPanel extends JPanel {

    public PaintPanel() {
        setSize(500, 500);
        setBackground(Color.BLACK);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Random random = new Random();
        g.setColor(Color.WHITE);
        // draw 5 random circles
        int count = 5;
        for (int i = 0; i < count; i++) {
            g.drawOval(random.nextInt(250), random.nextInt(250),
                    random.nextInt(250), random.nextInt(250));
        }
    }

}

Main class

public class DrawMain {

    public static void main(String[] args) {

        JFrame frame = new PaintFrame();
        frame.setDefaultCloseOperation(PaintFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 500);
        frame.setVisible(true);

    }

}

这篇关于随机圈JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆