【问题标题】:Moving a rectangle down a JFrame在 JFrame 中向下移动一个矩形
【发布时间】:2013-02-19 05:56:41
【问题描述】:

我正在尝试编写一个程序来将矩形向下移动到 JFrame,谁能解释为什么这不起作用?

public class DrawingComponent extends JLabel {

public static int x = 0;

public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect = new Rectangle(50,x,50,50);
    g2.draw(rect);
    x = x+100;
}
}

public class GameL {

javax.swing.JFrame frame = new javax.swing.JFrame();

public static void main(String[] args) {
        GameL tetris = new GameL();
        tetris.start();     
}

public void start(){
    //setup frame
    frame.setSize(800,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    TimerEH timereh = new TimerEH();
    Timer timer = new Timer(5000,timereh);
    timer.start();          
}

class TimerEH implements ActionListener{        
    public void actionPerformed(ActionEvent e){
        DrawingComponent dc = new DrawingComponent();
        frame.add(dc);
    }       
}
}

【问题讨论】:

  • 您正在创建一个新的DrawingComponent 并将其添加到每个刻度周期的帧中。这是很多DrawComponents
  • 参见this 类似的答案及其variation。还有this 最后一个例子

标签: java swing graphics2d


【解决方案1】:

您正在创建一个新的DrawingComponent 并将其添加到每个滴答周期的框架中。很多DrawComponents

虽然可以通过移动组件来为组件设置动画,但您需要提供更多信息来说明为什么要这样做。首选方法是使用 JPanel 之类的东西,将其添加到使用 BorderLayout 的框架并覆盖面板 paintComponent 方法。

尝试“移动”组件的问题是组件有一个定义的空间,它们可以在其中渲染,从代码的外观来看,标签的大小可能为 0x0,没有太多空间可以绘制在所有...

更新示例

因此,下面的示例演示了如何使用布局管理器在组件范围内绘制矩形,该布局管理器会尝试将组件的大小调整为可用的可用空间。

它还向用户显示getPreferredSize 以向布局管理器提供有用的尺寸提示。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CreppyRectangle {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(0, 6));
                frame.add(new TestPane(Color.RED));
                frame.add(new TestPane(Color.GREEN));
                frame.add(new TestPane(Color.BLUE));
                frame.add(new TestPane(Color.ORANGE));
                frame.add(new TestPane(Color.PINK));
                frame.add(new TestPane(Color.MAGENTA));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int yPos;
        private int size = 25 + (int)Math.round(Math.random() * 50);
        private int yDelta = 5 + (int)Math.round(Math.random() * 10);

        public TestPane(Color foreground) {
            setForeground(foreground);
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    yPos += yDelta;
                    if (yPos < 0) {
                        yPos = 0;
                        yDelta *= -1;
                        System.out.println(yDelta);
                    } else if (yPos + size > getHeight()) {
                        yPos = getHeight() - size;
                        yDelta *= -1;
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getForeground());
            g2d.drawRect(0, yPos, getWidth() - 1, size);
            g2d.dispose();
        }
    }

}

【讨论】:

  • 非常感谢您的详细回复!这个社区太棒了,我迫不及待地希望有一天我可以提供答案而不是问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2012-01-02
  • 2021-02-06
  • 2022-06-29
  • 1970-01-01
  • 2012-09-02
相关资源
最近更新 更多