简体   繁体   English

Java - 如何在JFrame中的IDE生成的JPanel上绘制形状

[英]Java - How to draw shapes on a IDE-generated JPanel within a JFrame

I am using NetBeans IDE to program a GUI application. 我正在使用NetBeans IDE来编写GUI应用程序。 It is going to calculate the area/volume of all kinds of primitive 2D/3D shapes. 它将计算各种原始2D / 3D形状的面积/体积。 I have created a new JFrame , inside it - a JTabbedPane , which has two tabs for 2D and 3D shapes. 我在其中创建了一个新的JFrame - 一个JTabbedPane ,它有两个用于2D和3D形状的选项卡。

I plan to create custom jpegs for tabs of each shape, that's not a problem, but I can't really draw shapes inside shape-type tabs (see picture). 我计划为每个形状的标签创建自定义jpegs,这不是问题,但我无法在形状类型标签内绘制形状(见图片)。 在此输入图像描述

There's only one .java file, I'm pasting the contents of that in here. 只有一个.java文件,我在这里粘贴了它的内容。 Please help me, I am extremely confused as to how can I get around this. 请帮助我,我非常困惑,我怎么能解决这个问题。

// @author Žanas Stundys
package geometrinis.projektas;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//
public class PagrindinisLangas extends javax.swing.JFrame {


/* Creates new form PagrindinisLangas */
public PagrindinisLangas() {
    initComponents();

}
//
/* This method is called from within the constructor to initialize the form */
//@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jEditorPane1 = new javax.swing.JEditorPane();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel1 = new javax.swing.JPanel();
    jTabbedPane2 = new javax.swing.JTabbedPane();
    jPanel3 = new javax.swing.JPanel();
    jPanel4 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();

    jScrollPane1.setViewportView(jEditorPane1);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setResizable(false);
    setSize(new java.awt.Dimension(960, 480));

    jTabbedPane1.setToolTipText("");

    jTabbedPane2.setTabPlacement(javax.swing.JTabbedPane.LEFT);

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jTabbedPane2.addTab("", new javax.swing.ImageIcon(getClass().getResource("/Assets/2D/square.png")), jPanel3, "Kvadratas / Stačiakampis"); // NOI18N

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jTabbedPane2.addTab("", new javax.swing.ImageIcon(getClass().getResource("/Assets/2D/circle.png")), jPanel4, "Apskritimas"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 876, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jTabbedPane1.addTab("2D", new javax.swing.ImageIcon(getClass().getResource("/Assets/icon2d.png")), jPanel1); // NOI18N

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jTabbedPane1.addTab("3D", new javax.swing.ImageIcon(getClass().getResource("/Assets/icon3d.png")), jPanel2, ""); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 960, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/* @param args the command line arguments */
public static void main(String args[]){
    /* Create and display the form */

    java.awt.EventQueue.invokeLater(() -> {
        new PagrindinisLangas().setVisible(true);
    });

}
// Variables declaration - do not modify                     
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTabbedPane jTabbedPane2;
// End of variables declaration                   
}

To add a component that is useful for custom drawing, what I do is this: 要添加对自定义绘图有用的组件,我所做的是:

  1. Create a new class that exends JPanel 创建一个释放JPanel的新类
  2. Within the class override paintComponent() method to draw the rectangle or whatever other shapes you want. 在类中重写paintComponent()方法以绘制矩形或您想要的任何其他形状。 It is helpful if the constructor sets the preferred size to be non-zero. 如果构造函数将首选大小设置为非零,则会很有帮助。
  3. Compile the new class. 编译新类。
  4. Open the Design view of the JPanel or JFrame I want to use the JPanel in. 打开我想要使用JPanel的JPanel或JFrame的Design视图。
  5. Drag the new class from the Projects tab onto the JFrame / Jpanel you want to use it in. (If you are going to use the same custom Panel multiple times it may be helpful to select the class in the projects tab and right-click for Tools -> Add To Pallet so that it is on the Pallet in the design view. ) 将新类从Projects选项卡拖到要使用它的JFrame / Jpanel上。(如果要多次使用相同的自定义Panel,在项目选项卡中选择类并右键单击可能会有帮助。工具 - >添加到托盘,使其位于设计视图中的托盘上。)

Here is an example class to be used this way: 这是一个以这种方式使用的示例类:

public class PanelWithRectangle extends JPanel {

    public PanelWithRectangle() {
        setPreferredSize(new Dimension(200,100));
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(new Rectangle2D.Double(10, 10,20, 25));
    }
}

For 3D Drawing it is somewhat different. 对于3D绘图,它有些不同。 Swing doesn't support 3D, so we need to use some JavaFX. Swing不支持3D,所以我们需要使用一些JavaFX。 For JavaFX instead of extending JPanel extend JFXPanel and instead of overriding paintComponent, create a scene object and use setScene. 对于JavaFX而不是扩展JPanel扩展JFXPanel而不是覆盖paintComponent,创建一个场景对象并使用setScene。 Also JavaFX methods need to be called on the JavaFX Thread so we need to use Platform.runLater() to have functions called on that thread. 此外,需要在JavaFX线程上调用JavaFX方法,因此我们需要使用Platform.runLater()来在该线程上调用函数。

public class PanelWithBox extends JFXPanel {

    public PanelWithBox() {
        setPreferredSize(new Dimension(200,100));
        Platform.runLater(this::initFx);
    }

    private void initFx() {
        Group g = new Group();
        Scene scene = new Scene(g);
        Box box = new Box(20, 30, 40);
        box.setMaterial(new PhongMaterial(Color.RED));
        box.getTransforms().addAll(Transform.translate(50, 25), new Rotate(30, new Point3D(1, 1, 1)));
        g.getChildren().add(box);
        setScene(scene);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM