如何将图像插入Java小程序?
基本上,我必须编写一个Java小程序来生成名片。我一直在思考如何在applet中显示我自己的图像 我对我的方向感到困惑的是,它说: 这假设您在BlueJ项目的目录中有一个名为“businesscard.jpg”的映像 我怎样才能把我的照片放到我的BlueJ项目的目录中呢?然后如何以正确的方式获取代码,使其显示在小程序中如何将图像插入Java小程序?,java,Java,基本上,我必须编写一个Java小程序来生成名片。我一直在思考如何在applet中显示我自己的图像 我对我的方向感到困惑的是,它说: 这假设您在BlueJ项目的目录中有一个名为“businesscard.jpg”的映像 我怎样才能把我的照片放到我的BlueJ项目的目录中呢?然后如何以正确的方式获取代码,使其显示在小程序中 import javax.swing.JApplet; import java.awt.*; import java.net.*; import javax.imageio.*
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);
}
}
你不能把它复制到那里吗?我们不是来帮你工作的。如果您阅读了教程,他们将回答您的部分问题。处理图像:和I/O:我没有要求任何人为我做我的工作。正如我所说的,Java对我来说是非常新和令人困惑的,即使是一些小事情对我来说也是困难的。包括查找与我的问题特别相关的指南。因此,我非常感谢并感谢您提供的链接。我从来没有要求任何人帮我完成我的小程序,而是在寻找一个正确的方向,因为我非常缺乏经验。再次感谢您抽出时间。
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);
}
}