-1

I'm learning JavaSwing. I wanted to draw an Image on the frame, but I want the picture resolution drawn to be like 300*300. Here is my code for drawing an Image on the panel.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Picture 
{
    JFrame frame;
    public static void main(String[] args)
    {
        Picture poo = new Picture();
        poo.go();
    }
    private void go()
    {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.getContentPane().add(new ImageOne());
    }
    class ImageOne extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            Image img = new ImageIcon("image.jpg").getImage();
            g.drawImage(img, 2, 2, this);
        }
    }
}

Can anyone tell me how to do this? I searched the net, but I could find explanations relating to BufferedImage, and I don't know how to use that.
Thanks in advance.

Arindam
  • 163
  • 1
  • 13
  • Override `getPreferredSize()`, for [example](http://stackoverflow.com/a/5129757/230513). – trashgod Oct 19 '16 at 23:12
  • @trashgod I used `getPrefferedSize()` inside `class ImageOne` but it didn't change anything. – Arindam Oct 19 '16 at 23:36
  • Please edit your question to include a [mcve] that shows your revised approach; also "If you do not honor the [_opaque property_](http://www.oracle.com/technetwork/java/painting-140037.html#props) you will likely see visual artifacts."—[`paintComponent()`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#paintComponent-java.awt.Graphics-); see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Oct 19 '16 at 23:48
  • 1) `public void paintComponent(Graphics g)` Use the `@Override` notation. 2) Any overridden paint method should start with a call to the super method. 3) `Image img = new ImageIcon("image.jpg").getImage();` Never try to load an image within a paint method. Load it within the constructor (create one) and store it as an attribute of the class. 4) But don't use `ImageIcon` to load images, becuase it fails silently. Instead use `ImageIO.read(..)` 5) Don't try to load the image as a `File`. It is apparenlty part of the app. and will become an embedded resource. .. – Andrew Thompson Oct 20 '16 at 02:48
  • .. Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL.6) **If you consult the [Java Docs for the `Graphics` API methods](http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#method.summary) you might notice there are a number of variants of `drawImage(..)`, at least one of which solves this problem.** – Andrew Thompson Oct 20 '16 at 02:48

1 Answers1

2

I believe you are looking for image.getScaledInstance(w,h, type). This scales the image to the given width (w) and height (h) to the type of scaling. The scaling type you would want to use is Image.SCALE_DEFAULT. Due to the way getScaledInstace works you have to create a new BufferedImage.

So your code would look something like this

        BufferedImage img = null;
        try {
            img = ImageIO.read(new FileInputStream(new File("image.jpg")));
        } catch (Exception e) {}

        //Scale image to 300x300
        int width = 300;
        int height = 300;
        Image scaled = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);

        //Create new buffered image
        BufferedImage tempBuff = new BufferedImage(width, height, img.getType());

         // Create Graphics object
        Graphics2D tempGraph = tempBuff.createGraphics();
        // Draw the resizedImg from 0,0 with no ImageObserver then dispose
        tempGraph.drawImage(scaled,0,0,null);
        tempGraph.dispose();

        g.drawImage((Image)tempBuff, 2, 2, this);
Kyal Bond
  • 296
  • 1
  • 12