3

I want to draw a line in java in eclipse. I made this code but I am getting error in line : 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.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • The method `paintComponent` returns void. The painting does not have to be called explicitly. Take a look at http://stackoverflow.com/questions/5801734/how-to-draw-lines-in-java. – hotzst Oct 05 '15 at 11:48
  • `paintComponent` methods return type is `void`....I think that is why the error. – Tirath Oct 05 '15 at 11:49
  • Don't call paintComponent yourself. Add your Game to the frame, and it will be called automatically. http://stackoverflow.com/a/5303712/14955 – Thilo Oct 05 '15 at 11:49
  • ok .. I tried `paint2d.add(frame);` and `frame.add(Game);` but they did not work ..thanks –  Oct 05 '15 at 11:51
  • 2
    `frame.add(paint2d)` – Pshemo Oct 05 '15 at 11:53
  • Does this answer your question? [How to draw lines in Java](https://stackoverflow.com/questions/5801734/how-to-draw-lines-in-java) – new Q Open Wid Apr 30 '20 at 13:32

2 Answers2

5

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

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

}
sebkur
  • 658
  • 2
  • 9
  • 18
  • God bless you , it works ... but why it does not work if I have `public void paintComponent(Graphics2D g)` instead of `public void paintComponent(Graphics g)` –  Oct 05 '15 at 12:03
  • See java api for help. https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#paintComponent-java.awt.Graphics- – Ramesh-X Oct 05 '15 at 12:07
  • Your `Game` class extends the `JPanel` class. The `JPanel` already defines a method called `paintComponent`. In your implementation, you redefine this method (override it). The compiler does only understand this mechanism, if your new method declaration matches the method of `JPanel` exactly. If you specify the `Graphics2D` as the parameter to the method, the compiler does not understand the overriding and your method won't be called at all, but the original method from the `JPanel` will run. – sebkur Oct 05 '15 at 12:08
0

There are two errors in your code/approach:

  1. The method paintComponent has a Graphics as parameter, not a Graphics2D.
  2. Game is a JPanel and must be added into the JFrame to be shown.

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