如何将图像插入到我的 Java 小程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12486768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I insert an image into my Java applet?
提问by Boxwood
Basically I have to write a Java applet that produces a business card. The part I'm stuck on is figuring out how to display an image of myself in the applet.
基本上,我必须编写一个生成名片的 Java 小程序。我坚持的部分是弄清楚如何在小程序中显示我自己的图像。
The part I'm confused about in my directions is that it says:
我对我的方向感到困惑的部分是它说:
This assumes you have an image in the directory of your BlueJ project called 'businesscard.jpg'
这假设您在 BlueJ 项目的目录中有一个名为“businesscard.jpg”的图像
How do I get my picture into the directory of my BlueJ project even? How do I then get the code the right way so it appears in the applet?
我如何将我的图片放入我的 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);
}
}
回答by user2318160
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);
}
}