我正在使用物理模拟器并使用swing来渲染。我想通过添加菜单栏和其他组件来改进界面。但是,当我尝试剪切绘图时,菜单栏的高度没有考虑在内,因此我的绘图移动得比应该高 以下是一些示例代码:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import javax.swing.border.Border;
public class TestFrame extends javax.swing.JFrame {
public TestFrame() {
initComponents();
}
public static void main(String args[]) {
//initalize and show the frame
TestFrame frame= new TestFrame();
frame.setVisible(true);
//creat buffer strategy
frame.createBufferStrategy(2);
BufferStrategy bufferStrategy = frame.getBufferStrategy();
//loop through rendering loop
while(true)
{
//get the graphics context to draw on
Graphics2D drawGraphics=(Graphics2D) bufferStrategy.getDrawGraphics();
frame.paint(drawGraphics);
Rectangle bounds=frame.drawingPanel.getBounds();
//account for frame's insets
Insets insets=frame.getInsets();
bounds.x+=insets.left;
bounds.y+=insets.top;
Border border = frame.drawingPanel.getBorder();
//account for the Border's insets
insets=border.getBorderInsets(frame.drawingPanel);
bounds.x+=insets.left;
bounds.y+=insets.top;
bounds.width-=insets.left+insets.right;
bounds.height-=insets.top+insets.right;
//draw a test rectangle
drawGraphics.setColor(new Color(0,255,0,64));
drawGraphics.fill(bounds);
//show rendering and then dispose
bufferStrategy.show();
drawGraphics.dispose();
}
}
private javax.swing.JPanel drawingPanel;
private javax.swing.JMenu editMenu;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar menuBar;
}
initComponents()
的内容:
private void initComponents() {
drawingPanel = new javax.swing.JPanel();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
editMenu = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
drawingPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
javax.swing.GroupLayout drawingPanelLayout = new javax.swing.GroupLayout(drawingPanel);
drawingPanel.setLayout(drawingPanelLayout);
drawingPanelLayout.setHorizontalGroup(
drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
drawingPanelLayout.setVerticalGroup(
drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
fileMenu.setText("File");
menuBar.add(fileMenu);
editMenu.setText("Edit");
menuBar.add(editMenu);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(284, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(162, Short.MAX_VALUE))
);
pack();
}
我能想到的另一个选择是让绘图代码位于paint
的{{1}}方法中。
答案 0 :(得分:2)
我能想到的另一个选择是让绘图代码位于
paint
的{{1}}方法中。
是的,但请使用jPanel
。
paintComponent
然后只需在主框架中添加public class PhysicsPanel extends JPanel {
// (or extend JComponent if you don't need to `add` anything)
@Override
public void paintComponent(Graphics g) {
// painting code
}
}
;框架上的PhysicsPanel
也将重新绘制物理面板。