java 如何在java中画一条线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32947844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:00:44  来源:igfitidea点击:

How to draw a line in java?

javagraphics2d

提问by sebkur

I want to draw a line in java in eclipse. I made this code but I am getting error in line : paint2d.add(paintComponent());

我想在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);
    }
}

How to fix that error?

如何修复该错误?

Is my code suitable for the purpose of drawing a line?

我的代码适合画线吗?

Thanks.

谢谢。

回答by sebkur

You're not overriding the method correctly. The argument to paintComponentis of type Graphics, not Graphics2D, but you can cast to Graphics2D. Also you need to add the Gamepanel to the frame as a content pane:

您没有正确覆盖该方法。to 的参数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();
    }

}

回答by johnrobert

There are two errors in your code/approach:

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

  1. The method paintComponenthas a Graphicsas parameter, not a Graphics2D.
  2. Game is a JPanel and must be added into the JFrame to be shown.
  1. 方法paintComponent有一个Graphics作为参数,而不是Graphics2D
  2. Game 是一个 JPanel,必须添加到 JFrame 中才能显示。

The source code below solves the problems.

下面的源代码解决了这些问题。

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);
    }
}