0

我正在研究一个物理模拟器,并正在使用 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.jPanel

4

1 回答 1

2

我能想到的另一个选择是让绘图代码位于paint.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到您的主框架;repaint在框架上也将重新绘制物理面板。

于 2013-05-06T21:56:32.090 回答