在JPanel中绘制故障

时间:2016-05-25 23:27:37

标签: java swing jframe jpanel

我首先要说的是,我几乎可以保证这个问题将成为一个副本,但我找不到任何其他适合我需要的答案/在我的程序上工作。我需要画一个JPanel,但我似乎无法得到我需要绘制的线来显示。

JFrame drawF = new JFrame("Simulator");
        JPanel simPanel = new JPanel();
        drawF.setVisible(true);
        drawF.setSize(1000,650);
        drawF.add(simPanel);
        drawF.pack();
        simPanel.setLayout(null);
        simPanel.setSize(1000,650);
        simPanel.setVisible(true);
        simPanel.setBackground(Color.BLACK);

        //add drawing surface


        //start drawing
            simPanel.add(new JComponent()
            {
            public void paint(Graphics g)
            {
                g.setColor(Color.RED);
                int xLast,yLast;
                for(int i=0;i < 5000000; i++)  // five million
                {

                    double dX = (Math.E*Math.exp(-numd1*i)*Math.sin(i*numf1+nump1))+(Math.E*Math.exp(-numd2*i)*Math.sin(i*numf2*nump2));
                    double dY = (Math.E*Math.exp(-numd3*i)*Math.sin(i*numf3+nump3))+(Math.E*Math.exp(-numd4*i)*Math.sin(i*numf4*nump4));
                    int drawX = (int)dX;
                    int drawY = (int)dY;
                    if (i==0)
                    {
                        xLast = 0;
                        yLast = 0;
                        g.drawLine(xLast,yLast,drawX,drawY);

                        simPanel.revalidate();
                        simPanel.repaint();
                    }
                    else
                    {
                        xLast = drawX;
                        yLast = drawY;
                        g.drawLine(xLast,yLast,drawX,drawY);

                        simPanel.revalidate();
                        simPanel.repaint();
                    }
                }
            }
        });





        repaint();

JComponent添加JPanel是否有问题?

1 个答案:

答案 0 :(得分:2)

您的问题可能是布局管理员之一。默认情况下,JPanel使用FlowLayout,因此您的JComponent将调整为其自然首选大小[0,0],因此在这种情况下不会显示任何内容。尝试制作JPanel的布局BorderLayout并添加JComponent BorderLayout.CENTER。

其他问题:

  • 覆盖JComponent的paintComponent方法,而不是paint方法,否则不会考虑边框和子项的绘制而产生意外的副作用。
  • 如果您按照我的建议重写paintComponent,请在覆盖中调用超级绘画方法super.paintComponent(g);。这有助于清除任何脏像素。
  • 删除所有不必要的重新调用和重新验证以及setVisible。
  • 在将所有组件添加到GUI后,在JFrame 上调用setVisible。
  • 我自己,我更喜欢在JPanel的paintComponent方法中绘画,而不是JComponent,因为它不会自动绘制背景。
  • 我更喜欢在自己的独立课堂上画画,而不是埋没在一个匿名的内心阶层里。以这种方式埋葬这个功能太重要了。
  • 在绘制方法中调用的代码越长或CPU密集度越高,GUI的响应性就越低。在类的构造函数中以及绘制方法中卸载for循环和数学计算。将结果存储在数组或列表中,或者如果您希望绘制到BufferedImage,然后在paintComponent方法中显示该图像。

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawingStuff extends JPanel {
    private static final int PREF_W = 1000;
    private static final int PREF_H = 650;
    private static final Color BG = Color.BLACK;
    private static final Color GRAPHICS_COLOR = Color.RED;
    private static final int LOOP_COUNT = 5000000; // 5 million

    private BufferedImage image = null;
    private MathParams[] params;

    public DrawingStuff(MathParams[] params) {
        this.params = params;
        setBackground(BG);
        image = drawImage();
    }

    private BufferedImage drawImage() {
        BufferedImage img = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();

        g2.setColor(GRAPHICS_COLOR);
        int xLast, yLast;
        for (int i = 0; i < LOOP_COUNT; i++) {
            double dX = params[0].doCalc0(i) + params[1].doCalc1(i);
            double dY = params[2].doCalc0(i) + params[3].doCalc1(i);
            int drawX = (int) dX;
            int drawY = (int) dY;
            if (i == 0) {
                xLast = 0;
                yLast = 0;
                g2.drawLine(xLast, yLast, drawX, drawY);
            } else {
                xLast = drawX;
                yLast = drawY;
                g2.drawLine(xLast, yLast, drawX, drawY);
            }
        }

        g2.dispose();
        return img;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Drawing Stuff");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // TODO: fix this -- what numbers to use??? These don't work
        MathParams[] paramArray = { 
                new MathParams(1, 2, 3), 
                new MathParams(1, 2, 3), 
                new MathParams(3, 4, 5),
                new MathParams(3, 4, 5)
                };

        frame.getContentPane().add(new DrawingStuff(paramArray));
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

public class MathParams {
    private int numd;
    private int numf;
    private int nump;

    public MathParams(int numd, int numf, int nump) {
        this.numd = numd;
        this.numf = numf;
        this.nump = nump;
    }

    public double doCalc0(int i) {
        return (Math.E * Math.exp(-numd * i) * Math.sin(i * numf + nump));
    }

    public double doCalc1(int i) {
        return (Math.E * Math.exp(-numd * i) * Math.sin(i * numf * nump));
    }
}