如何在java中画一条线?
How to draw a line in java?
我想在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
的参数是 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();
}
}
您的 code/approach 中有两个错误:
- 方法 paintComponent 有一个 Graphics 作为参数,而不是 Graphics2D.
- 游戏是一个 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);
}
}
我想在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
的参数是 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();
}
}
您的 code/approach 中有两个错误:
- 方法 paintComponent 有一个 Graphics 作为参数,而不是 Graphics2D.
- 游戏是一个 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);
}
}