我正在使用NetBeans IDE
来编写GUI应用程序。它将计算各种原始2D / 3D形状的面积/体积。
我在其中创建了一个新的JFrame
- JTabbedPane
,它有两个用于2D和3D形状的标签。
我计划为每个形状的标签创建自定义jpegs,这不是问题,但我无法在形状类型标签内绘制形状(见图片)。
只有一个.java
文件,我在这里粘贴了它的内容。请帮助我,我对如何解决这个问题感到非常困惑。
// @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
}
答案 0 :(得分:1)
要添加对自定义绘图有用的组件,我的工作是:
这是一个以这种方式使用的示例类:
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));
}
}
对于3D绘图,它有些不同。 Swing不支持3D,所以我们需要使用一些JavaFX。对于JavaFX而不是扩展JPanel扩展JFXPanel而不是覆盖paintComponent,创建一个场景对象并使用setScene。此外,需要在JavaFX Thread上调用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);
}
}