2014-08-31 43 views
-1

有人可以提供一个Java ui的基本示例,其中按钮点击在旁边的jpanel上绘制矩形?Java - >基本绘图 - > Howto - >按钮点击将形状添加到jpanel

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

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

感谢您的帮助,我们对此表示感谢。

+1

这个问题似乎是题外话,因为它是关于让代码示例。 – 2014-09-01 02:10:36

回答

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

感谢您的回应。就像我写的,我不想从组件中抽取组件。我期望从另一个组件的单击操作中绘制组件。你会知道该怎么做吗?再次感谢。 – InnerOrchestra 2014-08-31 21:35:52

+0

是的,我知道你想要做什么。但恐怕这是不可能的,开箱即用。当你点击一个按钮时,这个按钮可以(例如)创建一个自定义的形状对象,您的自定义面板可以绘制。创建一个ShapeList.this列表包含所有形状。您的自定义面板将在paintComponent的此列表中绘制所有这些形状。 – Ben 2014-08-31 21:48:08

0

这里是我做到了。请让我知道你会如何改善这个初学者代码! ;)

从本质上讲,这里的伎俩来获得一个按钮来绘制矩形:

  • 扩展您的主应用程序类,一个JFrame(或JComponent的)或???

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

  • 就在您的主应用程序类的顶部声明一个ArrayList来容纳您将绘制的东西。

  • 在您的主应用程序类的顶部声明绘图类的变量。在你的控件事件中(按钮在我的情况下)使用函数来准备绘制的东西(我使用了一个叫做AddRectangle())。

  • 在您的绘图类中,重写paintComponent并使用a为每个绘制您在数组中松鼠离开的所有东西。

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

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

}