Java - >基本绘图 - > Howto - >按钮单击会向jpanel添加形状

时间:2014-08-31 20:37:48

标签: swing paintcomponent java-2d drawrectangle

有人可以提供Java ui的基本示例,其中按钮单击会在旁边的jpanel上绘制一个矩形吗?

您可以找到捕获鼠标的绘图示例,或者从加载ui中绘制静态图,但我找不到一个使用(点击)的组件的示例借鉴另一个组成部分。

我有一个用户定义框数(行和列)的ui,而ok按钮应该在模拟一张纸的JPanel上绘制这些框。

感谢您的帮助,感谢您。

2 个答案:

答案 0 :(得分:0)

如果您想在某个组件上绘制某些内容,请覆盖其paintComponent-Method。

基本示例,使用JPanel:

public class MyPanel extends JPanel
{
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      //here your draw stuff
      //like: 
      Graphics2D g2d = (Graphics2D)g;
      g.drawLine(...);
   }
}

答案 1 :(得分:0)

我是这样做的。请告诉我你如何改进这个初学者代码! ;)

基本上,这里是'获取按钮来绘制矩形的技巧:

  • 将您的主app类扩展为JFrame(或JComponent)或???

  • 在主应用程序的顶部声明要绘制的类(DrawCanvas)并扩展到JPanel。

  • 在主应用程序类的顶部,声明了一个ArrayList来保存你要绘制的东西。

  • 在主应用类的顶部为绘图类声明一个变量。

  • 在你的控制事件中(我的情况下的按钮)使用一个函数来准备要绘制的东西(我使用了一个叫做AddRectangle())。

  • 在你的绘图类中,覆盖paintComponent并使用a来绘制你在数组中所有东西。

由于无法直接控制绘图,因此必须了解每次调用repaint()时都会调用绘图函数。这意味着你必须把你所有的东西藏起来像血腥的松鼠,以便绘图方法正确地绘制或重绘屏幕。最后它通常是一堆你将最终使用的数组和一堆for每个循环都要通过它们。

package views;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;



public class appMainWindow extends JFrame
{
class PdfLocation
{
    public double xPos;
    public double yPos; 
}

class DrawCanvas extends JPanel
{
      @Override
      public void paintComponent(Graphics g) 
      {
         super.paintComponent(g);
         for (PdfLocation p : PdfLocations)
         {
             g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
             repaint();
         }
      }
}

public void AddRectangle()
{
    PdfImagesCount++;
    lblPdfcount.setText(Integer.toString(PdfImagesCount));

    PdfLocation rect = new PdfLocation();

    if (PdfLocations.isEmpty() == false)
    {
        PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
        rect.xPos = spot.xPos + 45;
        rect.yPos = 10;
    }   
    else
    {
        rect.xPos = 10;
        rect.yPos = 10;         
    }
    PdfLocations.add(rect);             
}

private JFrame frame;
public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
public int PdfImagesCount = 0;


public static final int CANVAS_HEIGHT = 700;
public static final int CANVAS_WIDTH = 1000;

private DrawCanvas canvas;
private JLabel lblPdfcount;

public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

public appMainWindow() 
{ 

    // Set up a custom drawing JPanel
    canvas = new DrawCanvas();
    canvas.setBackground(Color.WHITE);
    canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
    initialize(); 
}

private void initialize() 
{
    frame = new JFrame();
    frame.setBounds(100, 100, 1280, 850);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddARectangle = new JButton("Add a rectangle");
    btnAddARectangle.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            AddRectangle();
            repaint();
        }
    });
    btnAddARectangle.setBounds(0, 6, 151, 29);
    frame.getContentPane().add(btnAddARectangle);


    frame.getContentPane().add(canvas);

    lblPdfcount = new JLabel("PdfCount");
    lblPdfcount.setBounds(10, 43, 61, 16);
    frame.getContentPane().add(lblPdfcount);
}

}