0

I have some available x,y values to show some spesific points on a picture and i will send the x,y values to make it draw lines. I am foreign to swing and paint so firstly i made a prototype like this but i have an issue with measures (dimensions).

import javax.swing.*;
import java.awt.*;
class form1 extends JPanel{

public void paint(Graphics g){
    super.paint(g);
    Stroke stroke = new BasicStroke(4f);
    Graphics2D g2d = (Graphics2D) g;
    ImageIcon img = new ImageIcon("picture.png"); // not sure if i should use this way to upload a pic
    img.paintIcon(this,g,0,0);
    g2d.setStroke(stroke);
    g2d.drawLine(282,421,547,251); // parameters are x1,y1,x2,y2

  }
}
public class test {
public static void main(String[] args) {
    JFrame frame = new JFrame("TITLE");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    form1 f = new form1();
    frame.add(f);
    frame.setSize(1920,1024);
    frame.setVisible(true);
  }
}

Output is in this picture

So my x,y values are drawing the correct lines on the true points but my image doesnt be opened in full size, it s like 3/5 of the full image in the shown picture above, image has 2470x1262 size. If i make it smaller via photoshop, then my x,y values wont show the correct points + image is being blurred, i couldn't find a method to set the sizes of image in java, how can i solve this ?

krystal
  • 35
  • 1
  • 8
  • 1
    If you're going to use an ImageIcon, then don't override paintComponent. Simply put the ImageIcon in a JLabel and the JLabel into your GUI. If you want to draw onto an image, then don't use an ImageIcon but rather get the image as a BufferedImage, in the constructor, ***not*** in a painting method, and then draw the image with `g.drawImage(...)` within paintComponent. – Hovercraft Full Of Eels Apr 28 '20 at 17:25
  • [For example](https://stackoverflow.com/questions/28482153/drawing-shapes-in-jpanel-over-an-image) – Hovercraft Full Of Eels Apr 28 '20 at 17:27
  • so do you say using bufferedimage instead of imageicon would fix my issue with the image size ? – krystal Apr 28 '20 at 17:42
  • It can -- you can resize the image to any desired size (although with the risk of image degradation inherent in all image resizing) by using one of the `Graphics#drawImage(...)` method overloads. – Hovercraft Full Of Eels Apr 28 '20 at 17:56

0 Answers0