-3

我有两个类.. 类面板扩展 JPanel,

和另一个控制该 jpanel 油漆每秒的类..(我使用 swing.Timer)

我下面的代码是失败的

继承人我到目前为止尝试..

类面板扩展 JPanel :

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Control control = new Control(this,g);
        repaint();
    }

类控制:

public class Control implements ActionListener{

    private int XX=0;
    private int YY=0;

    private Graphics2D g2;
    private JPanel panel;

    Timer tim = new Timer(1000, this);


    public Control(JPanel el,Graphics g) {


        this.g2=(Graphics2D)g.create();
        this.panel=el;
        tim.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        XX++;
        YY++;

    /////////////////////
    //my priority 
        GradientPaint gp = new GradientPaint(XX, YY, Color.BLUE, panel.getWidth(), panel.getHeight(), Color.WHITE);
    //////////////////////

        g2.setPaint(gp);
        g2.fillRect(0, 0, panel.getWidth(), panel.getHeight());
        panel.repaint();
    }
}

我需要每秒改变 GradientPaint 的起点

然后每秒在 jpanel 中绘制它

我该怎么办?

谢谢 ..

4

3 回答 3

5
  1. 不要在/ ( )Object内部创建任何东西,在之前准备 this/these (s) (您可以将元素放入 Array 到内部并仅在内部循环),这个想法在运行时创建一堆 Object,直到 NPE 成为paintpaintComponentControl control = new Control(this,g);ObjectpaintComponent

  2. 您的代码设计错误,必须阅读Oracles 2D 图形教程这里有大量示例

  3. 必须PreferredSize覆盖JPanel

  4. 你也可以创建BufferedImage

于 2012-12-07T07:10:34.563 回答
5

mKorbel 所说的一切以及...

  • 永远不要维护Graphics对绘制子系统传递给您的上下文的引用。它在油漆周期之间变化
  • 永远不要从方法中调用repaint或任何可能导致重绘请求的paintXxx方法,这将导致重绘管理器在未来某个时间安排新的重绘并最终循环你的 CPU 100%
  • 在 AWT 和 Swing 中绘画
  • Swing 中的自定义绘画
  • 如何摆动计时器


public class TestPaintTimer {

    public static void main(String[] args) {
        new TestPaintTimer();
    }

    public TestPaintTimer() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new GradientPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class GradientPanel extends JPanel {

        private Color startColor = Color.RED;
        private Color endColor = Color.BLUE;
        private float progress = 0f;
        private float direction = 0.1f;

        public GradientPanel() {
            Timer timer = new Timer(125, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    progress += direction;
                    if (progress > 1f) {
                        direction *= -1;
                        progress = 1f;
                    } else if (progress < 0) {
                        direction *= -1;
                        progress = 0f;
                    }

                    startColor = calculateProgress(Color.RED, Color.BLUE, progress);
                    endColor = calculateProgress(Color.BLUE, Color.RED, progress);

                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            LinearGradientPaint lgp = new LinearGradientPaint(
                            new Point(0, 0),
                            new Point(0, getHeight()),
                            new float[]{0f, 1f},
                            new Color[]{startColor, endColor});
            g2d.setPaint(lgp);
            g2d.fill(new Rectangle(getWidth(), getHeight()));
            g2d.dispose();
        }

        public Color calculateProgress(Color startValue, Color endValue, float fraction) {
            int sRed = startValue.getRed();
            int sGreen = startValue.getGreen();
            int sBlue = startValue.getBlue();
            int sAlpha = startValue.getAlpha();

            int tRed = endValue.getRed();
            int tGreen = endValue.getGreen();
            int tBlue = endValue.getBlue();
            int tAlpha = endValue.getAlpha();

            int red = calculateProgress(sRed, tRed, fraction);
            int green = calculateProgress(sGreen, tGreen, fraction);
            int blue = calculateProgress(sBlue, tBlue, fraction);
            int alpha = calculateProgress(sAlpha, tAlpha, fraction);

            return new Color(red, green, blue, alpha);
        }

        public int calculateProgress(int startValue, int endValue, float fraction) {
            int value = 0;
            int distance = endValue - startValue;
//        value = Math.round((float)distance * fraction);
            value = (int) ((float) distance * fraction);
            value += startValue;

            return value;
        }

    }

}
于 2012-12-07T07:25:03.030 回答
0

多亏了 mKorbel 和 MadProgrammer,我终于可以解决它了。

这是我要找的代码..

回应 mKorbel & madProgrammer 的回答

   public class again extends JPanel{
    int xx=0;
    int xxMax;
    String go = "RIGHT";

    Timer t;
    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(go.equals("RIGHT")){
                if(xx>=xxMax){go="LEFT";};
                xx += 10;

            }else if(go.equals("LEFT")){
                if(xx<=0){go="RIGHT";};
                xx -= 10;

            }
            repaint();
        }};

    public again() {
        t = new Timer(1, listener);
        t.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        GradientPaint gp = new GradientPaint(0, 0, Color.black, xx, getHeight(), Color.WHITE);
        g2.setPaint(gp);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.dispose();
        xxMax=getWidth();
    }
}


class Frame extends JFrame{

    public Frame(){
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        add(new again());
        setVisible(true);
    }

    public static void main (String[]args){
        new Frame().setExtendedState(MAXIMIZED_BOTH);
    }
}
于 2012-12-07T12:27:06.590 回答