java - JAVA随机圈

标签 java user-interface geometry

我正在尝试创建一个 GUI,它将接收要绘制的圆圈数量,并在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;

/**
 * 
 * @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
    }
}
 }

二等

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


}
}

最佳答案

所以在你的中,我做了一些补充,首先,我将整个程序放在一个类中(CIRLCES PANEL),如果你想使用第二个类,你可以使用它.... 问题来了,你的程序不是读取绘图的ActionPerformed方法,意味着它不是与按钮一起定位的,现在我直接将它与你的按钮(DRAW)一起添加,textArea 值,并绘制圆圈。我将你的文本区域设置为最终的,所以你可以在任何地方使用它......

现在你需要做的事情---- - 这个程序在整个框架上绘制圆圈,意味着不在您的绘图面板上,您需要设置值,所以它将在您的绘图面板区域上绘制 - 此外,您还需要为椭圆形添加颜色,因为它会绘制黑色圆圈,您将看不到......

还有一件事我忘记提到你,那就是你的,你还需要为你的清晰方法添加代码......

 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;


 public class CirclesPanel extends JPanel{
 private JButton draw, clear;
 private JTextArea textArea;
 private JPanel panel, drawPanel, buttonPanel;
 private int count;

 public CirclesPanel(){

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


JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
setPreferredSize(new Dimension(620, 425));

JPanel drawPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(450,400));
drawPanel.setBackground(Color.BLACK);

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

  public void paintComponent(Graphics g) {
  Random generator = new Random();
  int x, y, diameter;
  for(int i = 0; i < count; i++){
     x = generator.nextInt(90);
    y = generator.nextInt(90);
    diameter = generator.nextInt(30);
    g.drawOval(x, y, diameter, diameter);


 }
 }


 }

enter image description here

关于java - JAVA随机圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205239/

相关文章:

java - 如何更改 "panel.add(new JLabel(""));"中 JLabel 的字体大小

javascript - 如何将平移和缩放控件添加到Processing.js Canvas ?

android - 当通过搜索栏更改大小时,Android v2 版 Google map 上的圆圈会闪烁

java - iText5 PDF 内容在页脚上被覆盖

java - GSON 解析 - 不同类型的相同 key

Ruby - Shoes GUI 能抓到滚轮鼠标吗?

html - 如何配置 Spotify 嵌入式播放列表?

opengl - 3D网格的隐藏线去除算法?

java - 为什么我们在 Deque 中有 offer(E e) 和 offerLast(E e) 方法,即使它们解决的是相同的目的

java - 谷歌的距离矩阵API也考虑 "Elevation"吗?