Photos in JPanel

I am trying to write an application in which I want to add different drawings to Jpanel. Everything works fine except for the JPG format, which displays images with very poor quality. This is how I draw the picture:

class draw extends Canvas 
{
    Dimension canvasSize = new Dimension(400, 400);
    String fileName;

    public void paint(Graphics g) 
    {       
        if(this.fileName!=null)
        {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Image img = toolkit.getImage(fileName);
            g.drawImage(img, 0, 0, this);
        }                   
    }
    public void setFileName(String name)
    {
        this.fileName=name;
    }

    public Dimension getMinimumSize()
    {
        return canvasSize;  
    }

    public Dimension getPreferredSize()
    {   
        return canvasSize;
    }
}

Is there a way that JPG format is covered?

+3
source share
2 answers

, , ( ) . JPEG , . , , ( ) . JPEG Image . Sun , .

+4

, OP . , :

a) , JPanel, - Canvas. Swing. , , paintComponent() NOT .

b) , . . , . , Swing , , , AWT Canvas.

c) , super.paint(), super.paintComponent() .

d) , , ( ). ImageIcon JLabel. .

Swing . , , , , , .

+2

Source: https://habr.com/ru/post/1731670/


All Articles