单击按钮向JPanel添加形状 [英] Adding shapes to JPanel on a button click

查看:98
本文介绍了单击按钮向JPanel添加形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的Class Circle和一个带有jPanel的Class我想要做的是当点击该按钮时,将在面板上绘制一个圆圈,每次我点击该按钮并更改x和y怎么没有在这里实现我在JPanel上一遍又一遍地圈了一圈。
如何做到这一点,或者有没有办法做我描述的,无论我的代码如何,但我希望类圈扩展Shape。

  public class Window {

private JFrame frame;
private JPanel panel = new JPanel();
Circle c = new Circle(frame,panel);
//其他一些按钮


//一些代码设置面板网格包的constaraints和背景然后
frame.getContentPane()。add(panel,gbc_panel);
}

然后圆圈类

  public class Circle extends Shape实现ActionListener {

private JPanel Panel;
private GridBagConstraints gbc_btnCircle;
private JButton btnCircle;

public void setPanel(JPanel panel){
Panel = panel;
}

public Circle(JFrame框架,JPanel面板){
btnCircle = new JButton(Circle);
//设置网格包约束的一些代码然后
frame.getContentPane()。add(btnCircle,gbc_btnCircle);
setPanel(面板);
btnCircle.addActionListener(this);

}

public void paint(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
g.fillOval(100,100,100,100);
Panel.add(this);
}

public void actionPerformed(ActionEvent arg0){
repaint();
}
}


解决方案

你有点错误的想法。在绘图面板中,您应该有列表< Circle> 。在绘图面板的 paintComponent 方法中,您应该遍历列表以绘制每个圆圈

  class Circle {
int x,int y,int width,int height;
public Circle(int x,int y,int width,int height){
... set em
}
public void draw(Graphics g){
g.fillOval(x,y,width,height);
}
}
类DrawingPanel扩展JPanel {
List< Circle> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(Circle circle:circles){
circle.draw(g);
}
}

//不要忘记覆盖公共维度getPreferredSize()
}

要在列表中添加更多圈子,只需在 DrawingPanel中有一个 addCircle 方法 class

  public void addCircle(Circle circle){
circles.add(circle);
repaint();
}

至于按钮,你应该在<$ c中创建它$ c> Window 类。在 ActionListener 中,只需创建一个 new Circle 并添加 DrawingPanel 通过调用 addCircle 方法






除此之外, Circle 不需要扩展 Shape Shape API已经有一个 Ellipse2D 等级,你可以从



$ b $创建圈子p> class DrawingPanel扩展JPanel {
List< Ellipse2D> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g.create();
for(Ellipse2D circle:circles){
g2.fill(circle);
}
g2.dispose();
}
//不要忘记覆盖公共维度getPreferredSize()
}








更新:完整示例

  import java.awt.BorderLayout; 
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

公共类CirclesDemo {

public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new CirclesDemo();
}
});
}

public CirclesDemo(){
JFrame frame = new JFrame();
frame.add(面板);
frame.add(createButton(),BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private final DrawingPanel panel = new DrawingPanel();

private JButton createButton(){
JButton button = new JButton(Add Circle);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int [] circleValues = generateRandomValues(300,300,50,150);
int x = circleValues [0];
int y = circleValues [1];
int width = circleValues [2];
int height = width;
Circle circle = new Circle( x,y,width,height);
panel.addCircle(circle);
}
});
返回按钮;
}

private int [] generateRandomValues(int maxX,int maxY,
int minSize,int maxSize){
Random random = new Random();
int [] values = new int [3];
values [0] = random.nextInt(maxX);
values [1] = random.nextInt(maxY);
values [2] = Math.min(random.nextInt(maxSize)+ minSize,maxSize);
返回值;
}

class Circle {

int x,y,width,height;

public Circle(int x,int y,int width,int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void draw(Graphics g){
g.drawOval(x,y,width,height);
}
}

类DrawingPanel扩展JPanel {

List< Circle> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(Circle circle:circles){
circle.draw(g);
}
}

public void addCircle(Circle circle){
circles.add(circle);
repaint();
}

@Override
public Dimension getPreferredSize(){
return new Dimension(400,400);
}
}
}


I have a Class Circle with a button and a Class with a jPanel what i want to do is when that button is clicked a circle will be drawn on the panel and each time i click that button and change x and y "some how not implemented here" i got a circle on the JPanel over and over . How to do that, or is there a way to do what i descriped regardless of my code but i want the class circle to extends Shape.

public class Window{

  private JFrame frame;
  private JPanel panel = new JPanel();
  Circle c = new Circle(frame, panel);
  // some other buttons 
  .
  .
 // some code to set the panel grid bag constaraints and background then
 frame.getContentPane().add(panel, gbc_panel);
}

then the Circle Class

public class Circle extends Shape implements ActionListener{

  private JPanel Panel;
  private GridBagConstraints gbc_btnCircle;
  private JButton btnCircle;

  public void setPanel(JPanel panel) {
      Panel = panel;
  }

 public Circle(JFrame frame, JPanel panel){
    btnCircle = new JButton("Circle");
    // some code to set grid bag constraint then
    frame.getContentPane().add(btnCircle, gbc_btnCircle);
    setPanel(panel);        
    btnCircle.addActionListener(this);

 }

  public void paint(Graphics g) {
      super.paintComponents(g);
      g.setColor(Color.red);        
      g.fillOval(100, 100, 100, 100);     
      Panel.add(this);
  }

  public void actionPerformed(ActionEvent arg0) {
    repaint();      
  }
}

解决方案

You kinda have the wrong idea. In your drawing panel, you should have a List<Circle>. And in the paintComponent method of the drawing panel, you should iterate through the list to draw each circle

class Circle {
    int x, int y, int width, int height;
    public Circle (int x, int y, int width, int height) {
        ... set em
    }
    public void draw(Graphics g) {
        g.fillOval(x, y, width, height);
    }
}
class DrawingPanel extends JPanel {
    List<Circle> circles = new ArrayList<>();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Circle circle : circles) {
            circle.draw(g);
        }
    }

    // Dont forget to override public Dimension getPreferredSize()
}

To add more Circles to the list, just have a addCircle method in the DrawingPanel class

public void addCircle(Circle circle) {
    circles.add(circle);
    repaint();
}

As far as the button, you should be creating it in the Window class. In the ActionListener, just create a new Circle and add it the DrawingPanel by calling the addCircle method


An aside, Circle doesn't need the extend Shape. The Shape API already has an Ellipse2D class, which you can create circles from

class DrawingPanel extends JPanel {
    List<Ellipse2D> circles = new ArrayList<>();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        for (Ellipse2D circle : circles) {
            g2.fill(circle);
        }
        g2.dispose();
    }
    // Dont forget to override public Dimension getPreferredSize()
}


UPDATE: full example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CirclesDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CirclesDemo();
            }
        });
    }

    public CirclesDemo() {
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.add(createButton(), BorderLayout.PAGE_END);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private final DrawingPanel panel = new DrawingPanel();

    private JButton createButton() {
        JButton button = new JButton("Add Circle");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int[] circleValues = generateRandomValues(300, 300, 50, 150);
                int x = circleValues[0];
                int y = circleValues[1];
                int width = circleValues[2];
                int height = width;
                Circle circle = new Circle(x, y, width, height);
                panel.addCircle(circle);
            }
        });
        return button;
    }

    private int[] generateRandomValues(int maxX, int maxY, 
                                       int minSize, int maxSize) {
        Random random = new Random();
        int[] values = new int[3];
        values[0] = random.nextInt(maxX);
        values[1] = random.nextInt(maxY);
        values[2] = Math.min(random.nextInt(maxSize) + minSize, maxSize);
        return values;
    }

    class Circle {

        int x, y, width, height;

        public Circle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public void draw(Graphics g) {
            g.drawOval(x, y, width, height);
        }
    }

    class DrawingPanel extends JPanel {

        List<Circle> circles = new ArrayList<>();

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Circle circle : circles) {
                circle.draw(g);
            }
        }

        public void addCircle(Circle circle) {
            circles.add(circle);
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

这篇关于单击按钮向JPanel添加形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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