0

我正在尝试创建一个 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);


}
}
4

2 回答 2

1

您要做的是在单击按钮绘制一些随机圆圈。我给你写了一个简化的版本来展示事情是如何工作的。drawPanel

样本

我只保留drawButtonandpaintPanel以保持简单。

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

}

您需要一个扩展 JPanel 的新类并覆盖该paintComponent方法来为您完成绘画工作。这可以确保您在面板上绘图

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

}

主班

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

    }

}
于 2013-04-25T04:03:50.313 回答
1

所以在你的里面,我做了一点补充,首先,我把整个程序放在一个类中(CIRLCES PANEL),如果你想使用第二类,你可以使用它....问题来了,你的程序是不是读取绘图的ActionPerformed方法,意味着它不在按钮上,现在我直接用你的按钮(DRAW)添加它,现在每当你点击按钮时,它会自动读取你的textArea值,并绘制你的界。我已将您的文本区域设为 FINAL,因此您可以在任何地方使用它......

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

还有一件事我忘了提到你,你的,你还需要为你的 clear 方法添加代码......

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


 }
 }


 }

在此处输入图像描述

于 2013-04-25T03:47:52.073 回答