in Education by
I want to load an image from my computer into 2D Graphics so that I can edit it afterwards and then I want to add it to JPanel. If you need to see my project I can send it to you. void loadImage() { FileDialog fd = new FileDialog(new Frame(), "Please choose a file:", FileDialog.LOAD); fd.show(); if (fd.getFile() != null) { File fil = new File(fd.getDirectory(), fd.getFile()); strDirectory = fd.getDirectory(); strFileType = fd.getFile(); mainImage.setIcon(new ImageIcon(fil.toString())); getFileList(strDirectory); checkFileType(strFileType); } } Thanks in advance JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To load your image into the memory, you can use ImageIO.read(File). To edit it afterwards, obtain a Graphics2D instance from it by calling createGraphics(): BufferedImage img = ImageIO.read(yourFile); Graphics2D g = img.createGraphics(); // Draw here on the graphics g.dispose(); You can even turn on anti-alias by setting a RenderingHint before drawing: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIASING_ON); Then, to add it to a JPanel, create your custom JComponent and add an instance of that component to your JPanel: public class JImageComponent extends JComponent { private BufferedImage img; public JImageComponent(BufferedImage bi) { img = bi; } @Override public void paintComponent(Graphics g) { g.drawImg(img, 0, 0, this); } }

Related questions

0 votes
    Currently I'm using Cloudinary.com to save my users images. I know how to do some of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have use the pesdk library to edit an image. public static int GALLERY_RESULT = 2; private void ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have use the pesdk library to edit an image. public static int GALLERY_RESULT = 2; private void ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am completely new with Android/Eclipse. I can't figure out how to add an image in the /res/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am completely new with Android/Eclipse. I can't figure out how to add an image in the /res/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I use eclipse (cdt) for c++ projects. It contains some test scripts in python. Is there anyway I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    True or false if a user has read permission on a file, he she can edit the content of the file Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    If the system is a three-wire system, the currents flowing towards the load in the three lines must add to ___ ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 17, 2021 in Education by JackTerrance
0 votes
    I have to write in MIPS a program that multiplies two number using the add and shift method. After ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    How to load a model from an HDF5 file in Keras? What I tried: model = Sequential() model.add(Dense(64, ... list index out of range Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    Currently I try to switch from vim to emacs and have some difficulty while editing code. For most of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
...