Gui和计时器问题

时间:2012-12-17 16:29:09

标签: java swing user-interface

上周我问了一个关于Gui和Timers的问题......经过大量的研究后我发现没有任何东西可以用于我的代码......如果有人能拿出我的代码并以某种方式操纵它来工作那将是惊人的。 ..我希望我的代码显示一个框,然后使其与背景颜色相同的框重叠,然后在其旁边显示一个新框,延迟

第一个代码:

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;

public class MovingSomething extends JPanel
{
    public void paintComponent (final Graphics g)
    {     
        int i = 20;
        int cons = 50;
        int red = 40;
        int green = 50;

        g.setColor(Color.GREEN);
        g.fillRect(50, 50, 100, 100);

        while (i >= 0)
        {
            i -= 1;

            g.setColor(Color.RED);
            g.fillRect(red, cons, 100, 100);
            red += 10;

            g.setColor(Color.GREEN);
            g.fillRect(green, cons, 100, 100);
            green += 10;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

为了您的小帮助,我发布此代码。虽然请尽量从中吸取教训并提出有效的问题,但可能会出现,以便体会整个事物。如果你更难以搜索它,毫无疑问,你可以找到这个精彩的Doc,这可以解释你在这个例子中的需求。

Code to programatically repaint the component whenever the user clicks or drags the mouse

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovingSquare
{
    private int x = 0;
    private int y = 100;
    private final int WIDTH = 100;
    private CustomPanel canvas;
    private Timer drawingTimer;
    private ActionListener timerAction = 
        new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if ((x + WIDTH == 500))
            {
                x = 0;
                canvas.setValues(x, y, Color.BLUE);
            }
            else
            {
                x += WIDTH;
                canvas.setValues(x, y, Color.BLUE);
            }
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Moving Sqaure");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        canvas = new CustomPanel();
        frame.setContentPane(canvas);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        drawingTimer = new Timer(1000, timerAction);
        drawingTimer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MovingSquare().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    private final int WSQUARE = 100;
    private final int HSQUARE = 50;

    private int x = 0;
    private int y = 100;
    private Color cSquare = Color.BLUE;

    /* 
     * This is where we updating the state
     * of different variables needed, and
     * thus calling repaint.
     */
    public void setValues(int x, int y, Color color)
    {
        cSquare = color;
        repaint(this.x, this.y, WSQUARE, HSQUARE);
        this.x = x;
        this.y = y;
        repaint(x, y, WSQUARE, HSQUARE);
    }

    /*
     * Always make this one customary
     * habbit, to override this method
     * when you extending a JComponent.
     */
    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(WIDTH, HEIGHT));
    }

    /* 
     * This is where the actual Painting
     * Portion of the whole thingy will
     * reside. Better is, not to put any
     * calculations in this part, just
     * update the state at some another
     * location and convey it to repaint
     * as needed.
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(cSquare);
        g.fillRect(x, y, WSQUARE, HSQUARE);
    }
}

最新编辑:

请尝试修改此代码,CustomPanel类与之前相同:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class MovingSquare
{
    private int x = 0;
    private int y = 100;
    private final int WIDTH = 100;
    private final int HEIGHT = 100;
    private Random random;
    private CustomPanel canvas;
    private Timer drawingTimer;
    private ActionListener timerAction = 
        new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            /*if ((x + WIDTH > 500) && (y + HEIGHT > 500))
            {
                x = random.nextInt(500 - WIDTH);
                canvas.setValues(x, y, Color.BLUE);
            }
            else
            {
                x += WIDTH;
                canvas.setValues(x, y, Color.BLUE);
            }*/
            x = random.nextInt(500 - WIDTH);
            y = random.nextInt(500 - HEIGHT);
            canvas.setValues(x, y, new Color(
                random.nextFloat(), random.nextFloat()
                , random.nextFloat(), random.nextFloat()));
        }
    };

    public MovingSquare()
    {
        random = new Random();
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Moving Sqaure");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        canvas = new CustomPanel();
        frame.setContentPane(canvas);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        drawingTimer = new Timer(1000, timerAction);
        drawingTimer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MovingSquare().displayGUI();
            }
        });
    }
}