-1

Considering the points p0x, p0y, p1x and p1y, in addition to their relationships,

deltaX = (p1x-p0x)

and

deltaY = (p1y - p0y).

Devise a way to draw a line on an image, at any possible inclination. I want make this using JFrame but I don't now how to make this:

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Template extends JFrame {
    private BufferedImage img;

    public Template() {
        img = new BufferedImage(1600, 1200, BufferedImage.TYPE_INT_RGB);
        setBounds(10,10,1600,1200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Template");
        drawLine();
        setVisible(true);
    }

    private void drawLine(int p0x,int p0y, int p1x ,int p1y) {
        p0x = 50;
        p0y = 50;
        p1x = 200;
        p1y = 200;
        int deltaX = (p1x-p0x);
        int deltaY = (p1y - p0y);
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, this);
    }
    
    public static void main(String[] args) {
        Template t = new Template();
    }
}

An algorithm to make a line in any possible inclination.

Abra
  • 19,142
  • 7
  • 29
  • 41
PhoenixTCD
  • 11
  • 1

2 Answers2

1

You should not be painting directly into a JFrame. Standard technique is to paint into a JPanel that is in the frame.

  • I reduced the size of the image for demonstration purposes.
  • The constructor is pretty standard to set up the frame, add the panel, and center it on the screen.
  • drawLine is called which draws the line over the image and calls repaint.
    • Here I changed the color of the line to red
    • and set its width to 3 pixels
  • overriding paintComponent is how you paint in JPanel
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

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

public class Template extends JPanel {

    private BufferedImage img;
    private static int WIDTH = 500;
    private static int HEIGHT = 400;
    JFrame frame = new JFrame("TempLate");
    private int p0x, p0y, p1x, p1y;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Template().start());
    }

    public Template() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null); // center
        frame.setVisible(true);
    }

    public void start() {
        p0x = 50;
        p0y = 50;
        p1x = 200;
        p1y = 200;
        drawLine();
        frame.repaint();
    }
    public void drawLine() {
        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D imgG2D = (Graphics2D)img.getGraphics();
        // ensure a smooth line
        imgG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        imgG2D.setColor(Color.RED);
        imgG2D.setStroke(new BasicStroke(3));
        imgG2D.drawLine(p0x, p0y, p1x, p1y);
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img == null) {
             return;
        }
       
        g.drawImage(img, 0, 0, this);
    }

}

For more in painting check out The Java Tutorials

WJS
  • 36,363
  • 4
  • 24
  • 39
-2

In Java 2D, an object should generally be an instance of Component class to be drawn on the screen.

JFrame is an indirect subclass of Component class. See here for the complete hierarchy.

The paint method of the component describes how does that object appear on the screen.

Therefore, let's have a Template Class that has a constructor that takes these parameters, and overrides the paint method.:

import java.awt.Graphics;
import javax.swing.JFrame;
class Template extends JFrame{

    int p0x;
    int p0y;
    int p1x;
    int p1y;

    public Template (int p0x,int p0y, int p1x ,int p1y){
        super("Demo");
        this.p0x = p0x;
        this.p1x = p1x;
        this.p0y = p0y;
        this.p1y = p1y;
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g); //to draw a JFrame!
        g2d.drawLine(p0x, p0y, p1x, p1y);
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Template(120, 50, 360, 50).setVisible(true);
            }
        });
    }


}

One last note: You don't need to use BufferedImage to draw lines.

wolfenblut
  • 131
  • 1
  • 11
  • 2
    That sounds so much like ChatGP response, even if it’s not, you should avoid overriding paint of top level containers, like JFrame as it will allow you to paint under the frame decorations, which is never pleasant. JFrame is also a compound component, and because of the way painting works in Swing, it can paint child components with having to paint the patent, which can cause it to paint over it – MadProgrammer Mar 17 '23 at 16:34
  • @MadProgrammer it may not be the best solution, however it is a solution. Also, it is not a solution from an Artificial intelligence. I tried to explain starting from the basics. Yes, we may think to avoid painting on the JFrame directly but it answers the question of the author. – wolfenblut Mar 20 '23 at 07:26
  • While I appreciate you think it "answers" the OP's question, it clearly demonstrates an incorrect approach which is going to lead the OP to more issues (and posting more questions, leading us to further highlight the low quality of the answer you've posted) - For [example](https://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914); [example](https://stackoverflow.com/questions/17499375/java-awt-drawstring-does-not-display-on-window/17499425#17499425); – MadProgrammer Mar 20 '23 at 07:42
  • And [example](https://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319) - which demonstrates the "core" reasons why you shouldn't do what you're proposing the user should do. So instead, we should all be aiming to provide the "best" answers – MadProgrammer Mar 20 '23 at 07:45
  • Ok, I'll edit my answer soon. – wolfenblut Mar 20 '23 at 08:51