如何在Java中绘制一条直线?

3

我想在Eclipse中用Java画一条线。我写了这段代码,但是在这一行出现了错误:paint2d.add(paintComponent());

import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel {

    public void paintComponent (Graphics2D g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawLine(30, 40, 80, 100);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(400, 420);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Game paint2d = new Game();
        paint2d.add(paintComponent()); // The method paintComponent(Graphics2D) in the type Game is not applicable for the arguments ()
        frame.setVisible(true);
    }
}

如何修复该错误?

我的代码是否适用于绘制一条线的目的?

谢谢。


方法paintComponent返回void。绘画不必显式调用。请查看https://dev59.com/zW025IYBdhLWcg3wyJCV。 - hotzst
paintComponent 方法的返回类型是 void,我认为这就是错误的原因。 - Tirath
不要自己调用paintComponent。将您的游戏添加到框架中,它会自动被调用。https://dev59.com/H1XTa4cB1Zd3GeqPzTa8#5303712 - Thilo
好的,我尝试了 paint2d.add(frame);frame.add(Game); 但它们都没有起作用..谢谢。 - user5309808
2
frame.add(paint2d) - Pshemo
这个回答解决了你的问题吗?如何在Java中绘制线条 - new QOpenGLWidget
2个回答

5
您没有正确地覆盖方法。参数paintComponent的类型为Graphics而不是Graphics2D,但您可以将其强制转换为Graphics2D。此外,您需要将Game面板添加到框架作为内容窗格:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel
{

    @Override
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.drawLine(30, 40, 80, 100);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(400, 420);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Game game = new Game();
        frame.setContentPane(game);

        frame.setVisible(true);
        frame.invalidate();
    }

}

上帝保佑你,它有效了...但如果我使用public void paintComponent(Graphics2D g)而不是public void paintComponent(Graphics g),为什么它就无法工作呢? - user5309808
请参考Java API获取帮助。 https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#paintComponent-java.awt.Graphics- - Ramesh-X
你的Game类继承了JPanel类。JPanel已经定义了一个名为paintComponent的方法。在你的实现中,你重新定义了这个方法(覆盖它)。编译器只有在你的新方法声明与JPanel的方法完全匹配时才能理解这种机制。如果你将Graphics2D指定为该方法的参数,编译器就无法理解覆盖,你的方法将不会被调用,而是运行JPanel的原始方法。 - sebkur

0

你的代码/方法中有两个错误:

  1. paintComponent 方法的参数应该是 Graphics 而不是 Graphics2D
  2. Game 是一个 JPanel,必须将其添加到 JFrame 中才能显示。

下面的源代码可以解决以上问题。

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel {

    public void paintComponent (Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawLine(30, 40, 80, 100);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(400, 420);

        // Adds Game panel into JFrame
        frame.add(new Game());

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接