2012-09-18 52 views
1

基本上我必須編寫一個生成名片的Java小程序。我堅持的部分是弄清楚如何在applet中顯示自己的圖像。如何將圖像插入到我的Java小程序中?

我在我的方向感到困惑的部分是,它說:

這是假設你在BlueJ的項目被稱爲「businesscard.jpg」

目錄中有一個形象我如何將我的照片放入我的BlueJ項目的目錄中?然後,我如何以正確的方式獲得代碼,以便它出現在applet中?

import javax.swing.JApplet; 
import java.awt.*; 
import java.net.*; 
import javax.imageio.*; 
import java.io.*; 
import java.awt.Graphics2D; 
import java.awt.image.*; 


public class BusinessCard extends JApplet 
{ 


    public void paint(Graphics page) 
    { 
     //Variables used in rectangle 
     int x = 0; 
     int y = 0; 
     int width = 500; 
     int height = 300; 

     page.drawRect(x, y, width, height); //draws the rectangle using variables 

     //Displays name 
     Font f = new Font("Helvetica", Font.BOLD, 26); 
     page.setFont(f); 
     page.drawString ("anon", 300,100); 

     //Displays company 
     Font g = new Font("Helvetica", Font.PLAIN, 18); 
     page.setFont(g); 
     page.drawString ("anon", 320, 120); 

     //Displays email 
     Font h = new Font("serif", Font.ITALIC, 15); 
     page.setFont(h); 
     page.drawString ("email", 320,140); 

     //int for the logo 
     final int MID = 350; 
     final int TOP = 168; 

     page.setColor (Color.orange); 
     page.fillOval (MID, TOP, 60, 60); //bottom half of the trophy. the rounded part. 
     page.drawArc (MID-8, TOP+15, 25, 25, 100, 160); //left arc 
     page.drawArc (MID+43, TOP+15 , 25, 25, 280, 160); //right arc 
     page.fillRect (MID+1, TOP+1, 59, 25); //make the top of the trophy flat basically 
     page.fillRect (MID+22, TOP+60, 15, 25); //neck of the trophy 
     page.drawLine (MID+48, TOP+84, MID+10, TOP+84); //base of the trophy 

     page.setColor (Color.blue); 
     Font i = new Font("serif", Font.BOLD, 20); 
     page.setFont(i); 
     page.drawString ("#1", MID+20, TOP+30); //Creates the "#1" on the trophy. 


     //The following is all code for inserting my image 


      BufferedImage photo = null; 

     try { 
      URL u = new URL(getCodeBase(), "businesscard.jpg"); 
      photo = ImageIO.read(u); 
     } catch (IOException e) { 
      page.drawString("Problem reading the file", 100, 100); 
     } 

     page.drawImage(photo, 10, 10, 150, 225, null); 




    } 


} 
+0

難道你不能只複製它嗎? – MadProgrammer

+0

我們不在這裏爲你做你的工作。如果你閱讀了教程,他們會回答你的問題。使用圖像:http://docs.oracle.com/javase/tutorial/2d/images/index.html和I/O:http://docs.oracle.com/javase/tutorial/essential/io/index。 html – sorifiend

+2

我並沒有要求任何人爲我做我的工作。正如我所說的,Java是非常新的和令人困惑的事情,甚至對我來說小事情都很困難。包括與我的問題特別相關的定位指南。所以我非常感謝並感謝您提供的鏈接。我從來沒有要求任何人爲我完成我的小程序,而是我正在尋求向正確的方向微調,因爲我是一個難以置信的經驗不足的人。感謝你的寶貴時間。 – Boxwood

回答

1
import javax.swing.*; 
import java.awt.*; 
import javax.imageio.*; 
import java.net.*; 
import java.io.*; 
import java.awt.image.*; 
import java.applet.*; 
public class Photo extends Applet 
{ 
    public void paint(Graphics g) 
    {   
     int x = 0; 
     int y = 0; 
     int width = 500; 
     int height = 300;      
     BufferedImage photo = null; 
     try 
     { 
     URL u = new URL(getCodeBase(),"Bike.jpg"); 
     photo = ImageIO.read(u); 
     } 
     catch (IOException e) 
     { 
     g.drawString("Problem reading the file", 100, 100); 
     } 
     g.drawImage(photo,x, y, width, height,null); 
    } 
}