-1

上周我问了一个关于 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;
        }
    }
}
4

1 回答 1

2

只是为了您的一点帮助,我发布了这段代码。尽管请尝试从中学习并提出可能会出现的有效问题,以了解整个事情。毫无疑问,如果您稍微搜索一下,您可能会找到这个精彩的文档,它在这个示例中准确地解释了您的需求。

每当用户单击或拖动鼠标时以编程方式重新绘制组件的代码

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();
            }
        });
    }
}
于 2012-12-17T17:08:30.273 回答