问题描述
我正在尝试创建一个图形用户界面,用于绘制圈数,并将它们绘制到随机位置/大小的drawPanel中。在我的actionListener上,当我尝试绘制圆时,它会在我的drawOval中给出红线
第一类:
> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!