2012-09-18 3 views
1

기본적으로 명함을 생성하는 Java 애플릿을 작성해야합니다. 내가 붙어있는 부분은 애플릿에 자신의 이미지를 표시하는 방법을 알아내는 것입니다.Java 애플릿에 이미지를 삽입하려면 어떻게해야합니까?

내 방향에 대한 혼란 스러워요 부분은 그것이 말하는 것입니다 :

이것은 당신이 'businesscard.jpg'

라는 BlueJ의 프로젝트의 디렉토리에 이미지가 가정 내 BlueJ 프로젝트의 디렉토리에도 내 사진을 어떻게 얻을 수 있습니까? 그런 다음 코드를 올바른 방법으로 가져와 애플릿에 표시하려면 어떻게해야합니까?

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

나는 내게 어떤 일을하라고 요구하지 않았다. 자바는 매우 새롭고 혼란 스럽습니다. 제가 말했듯이, 심지어는 거의 어려운 일이 아닙니다. 내 문제와 관련이있는 위치 안내를 포함합니다. 그래서 저는 매우 감사 드리며 제공하신 링크에 감사드립니다. 나는 애플릿을 끝내기 위해 아무에게도 한 번도 물어 보지 못했지만 오히려 나는 경험이 거의 없기 때문에 올바른 방향으로 움직이기를 원했다. 시간 내 주셔서 다시 한번 감사드립니다. – 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); 
    } 
} 
관련 문제