Results 1 to 3 of 3
Thread: Cant put a in a image.
- 11-06-2009, 04:09 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Cant put a in a image.
:confused:I am looking to put in a image into this app that I am working on in class but every were i have looked online has not helped me. I can’t get a pick to work inside this code could someone help me I would like to put a image file name in, but I don’t even know where to start.....
I am so lost please helpJava Code:package Java; import java.awt.Color; //color class import java.awt.Graphics; //primitive graphics class import java.awt.Graphics2D; //Extends Graphics class import java.awt.Rectangle; //Rectangles class import java.awt.geom.Ellipse2D; //Ellipse and circle class import java.awt.geom.Line2D; //Line class import javax.swing.JPanel; import javax.swing.JComponent; public class FaceComponent extends JComponent { //Place drawing instructions inside this method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //Make graphics 2D drawGrid(g2); //Draw grid to make it easier to create component Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150); //Construct oval head (x, y, width, height) g2.draw(head); //Draw head Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90); //Construct left eye (x1, y1, x2, y2) g2.draw(eye1); Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90); //Construct right eye (x1, y1, x2, y2) g2.draw(eye2); Rectangle mouth = new Rectangle(30, 130, 50, 5); //Construct mouth (x, y, width, height) g2.setColor(Color.RED); //Color of mouth g2.fill(mouth); //Fill mouth with red g2.setColor(Color.BLUE); g2.drawString("Hello, Class!!!", 5, 175); //Draw greeting (“message”, x, y) } public void drawGrid(Graphics2D g2) { // The following two for loops draw a grid on the screen to help you // position your object. Once you have finished your object, you can // comment out the calling of this method //Draw horizontal lines g2.setColor(Color.LIGHT_GRAY); for (int i=50; i<=600; i=i+50) { g2.drawString(""+i, 5, i); Line2D.Double horizonalLine = new Line2D.Double(5, i, 700, i); g2.draw(horizonalLine); } //Draw vertical lines for (int i=50; i<=600; i=i+50) { g2.drawString(""+i, i, 10); Line2D.Double horizonalLine = new Line2D.Double(i, 10, i, 700); g2.draw(horizonalLine); } g2.setColor(Color.BLACK); //**End of grid } }
Thanks in advance.
- 11-06-2009, 04:27 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
The method to use is drawImage. Set the forum/net, there are plenty of examples out there.
- 11-06-2009, 05:29 AM #3
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
Here's how I do it:
a) the image is stored in a subdirectory of where the code is;
for code in project com.physpics.photos, the image is in
.../com/physpics/photos/images
b) I have this utility method in Utils
(Note that the log.severe() call could be changed to System.out.println()Java Code:/** Fetches an image from the class directory's /image/ directory. * NB: The class must be "built" so the images are * copied from the src/ directory to the build/ directory * @param path Path of resource, realtive to the .class file's directory * Typically, images are in a subdirectory of the class directory * and the path is of the form "images/icon.ext" * where ext is for an icon (ico) or an image (gif, png, or jpg). * @param description The description of the icon for accessible contexts * @return An Image, or null if there is an error. */ static public Image fetchImageResource(String path, String description) { java.net.URL imgURL = Tagger.class.getResource(path); Image o = null; if (imgURL != null) try { o = ImageIO.read(imgURL); } catch (IOException ex) { } // defer to error message below if (o != null) return o; log.severe("Couldn't load image resource: " + path); return null; }
c) To load the image:
d) For paint() to display the image at x,y: g.drawImage(cat, x, y, this);Java Code:static final Image cat = Utils.fetchImageResource("images/cat.jpg", "my first cat");
Similar Threads
-
Problems drawing a section of an image onto another image.
By Cain in forum Java 2DReplies: 1Last Post: 04-17-2009, 12:44 AM -
[SOLVED] manipulating the pixel values of an image and constructinf a new image from
By sruthi_2009 in forum AWT / SwingReplies: 14Last Post: 04-10-2009, 08:46 AM -
Canvas Image popups another image (SWT)
By SpaceY in forum New To JavaReplies: 2Last Post: 11-11-2008, 01:25 PM -
drawing an image to an offscreen image
By hunterbdb in forum Java 2DReplies: 9Last Post: 10-30-2008, 06:17 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks