在黑色JFrame上绘制线条

时间:2013-05-11 10:19:16

标签: java swing jframe awt java-2d

我试图利用我在过去六个月的java编程中学到的东西重新创建经典的乒乓球比赛。但画线并不是其中之一,即使它看起来很基本。

public static void main(String[] args) {
    Pong p = new Pong();
    p.setVisible(true);
}
/**
 * The Constructor
 */
public Pong() {
    makeFrame();
    draw(getGraphics());
}

/**
 * Making the frame for the game
 */
public void makeFrame() {
    // Frame stuff goes here
}

/**
 * Drawing the in-games lines
 */
public void draw(Graphics g) {
    super.paint(g);
    g.setColor(Color.white);
    g.drawLine(400, 0, 400, 550);
}

我似乎无法让自己画线。关于我做错了什么的任何想法? 我希望屏幕中间有这条线。

修改 我想感谢那些回答这个问题的人。很多人向你们致敬!我非常感谢答案和提供的链接! :)

2 个答案:

答案 0 :(得分:2)

在Java Swing中,您不要自己调用“draw” - 而是创建一个JFrame(或其他顶级容器,如JApplet)并覆盖其“paint”方法 - 或组件的paint方法直接或间接包含在您的根元素中。

您的getGraphics()不会返回实际的图形上下文(仅为根摆动组件创建,或者在为屏幕外绘制构建位图缓冲区时)。所以什么都不会显示出来。看看official painting tutorial

推荐的方法(以及上面教程中使用的方法)是通过覆盖组件层次结构中包含的JPanel的paintComponent()方法来执行自定义绘制。

答案 1 :(得分:1)

在Swing中,您不负责绘画,该作业属于RepaintManager。它根据许多因素决定绘画的内容和时间。

执行自定义绘制的推荐机制是创建自定义类,从JPanel扩展并覆盖它的paintComponent方法。如果您想更新组件,可以通过调用它的repaint方法请求重新绘制组件。

毫无疑问,这只是一个请求。

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class QuickPaint {

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

    public QuickPaint() {
        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 PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private int paints = 0;

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

        @Override
        protected void paintComponent(Graphics g) {
            paints++;
            super.paintComponent(g); 
            int width = getWidth();
            int height = getHeight();
            g.drawLine(width / 2, 0, width / 2, height);
            g.drawLine(0, height / 2, width, height / 2);
            g.drawLine(0, 0, width, height);
            g.drawLine(0, height, width, 0);
            String text = "Repainted " + paints + " times";
            FontMetrics fm = g.getFontMetrics();
            int x = (width - fm.stringWidth(text)) / 2;
            int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
            g.drawString(text, x, y);
        }

    }

}

详细了解Performing Custom PaintingPainting in AWT and Swing了解更多详情......