java優(yōu)秀的代碼實(shí)例 JAVA編程實(shí)例

java中的單例模式的代碼怎么寫

我從我的博客里把我的文章粘貼過來吧,對(duì)于單例模式模式應(yīng)該有比較清楚的解釋:

10年積累的做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有羅定免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

單例模式在我們?nèi)粘5捻?xiàng)目中十分常見,當(dāng)我們?cè)陧?xiàng)目中需要一個(gè)這樣的一個(gè)對(duì)象,這個(gè)對(duì)象在內(nèi)存中只能有一個(gè)實(shí)例,這時(shí)我們就需要用到單例。

一般說來,單例模式通常有以下幾種:

1.饑漢式單例

public class Singleton {

private Singleton(){};

private static Singleton instance = new Singleton();

public static Singleton getInstance(){

return instance;

}

}

這是最簡單的單例,這種單例最常見,也很可靠!它有個(gè)唯一的缺點(diǎn)就是無法完成延遲加載——即當(dāng)系統(tǒng)還沒有用到此單例時(shí),單例就會(huì)被加載到內(nèi)存中。

在這里我們可以做個(gè)這樣的測試:

將上述代碼修改為:

public class Singleton {

private Singleton(){

System.out.println("createSingleton");

};

private static Singleton instance = new Singleton();

public static Singleton getInstance(){

return instance;

}

public static void testSingleton(){

System.out.println("CreateString");

}

}

而我們?cè)诹硗庖粋€(gè)測試類中對(duì)它進(jìn)行測試(本例所有測試都通過Junit進(jìn)行測試)

public class TestSingleton {

@Test

public void test(){

Singleton.testSingleton();

}

}

輸出結(jié)果:

createSingleton

CreateString

我們可以注意到,在這個(gè)單例中,即使我們沒有使用單例類,它還是被創(chuàng)建出來了,這當(dāng)然是我們所不愿意看到的,所以也就有了以下一種單例。

2.懶漢式單例

public class Singleton1 {

private Singleton1(){

System.out.println("createSingleton");

}

private static Singleton1 instance = null;

public static synchronized Singleton1 getInstance(){

return instance==null?new Singleton1():instance;

}

public static void testSingleton(){

System.out.println("CreateString");

}

}

上面的單例獲取實(shí)例時(shí),是需要加上同步的,如果不加上同步,在多線程的環(huán)境中,當(dāng)線程1完成新建單例操作,而在完成賦值操作之前,線程2就可能判

斷instance為空,此時(shí),線程2也將啟動(dòng)新建單例的操作,那么多個(gè)就出現(xiàn)了多個(gè)實(shí)例被新建,也就違反了我們使用單例模式的初衷了。

我們?cè)谶@里也通過一個(gè)測試類,對(duì)它進(jìn)行測試,最后面輸出是

CreateString

可以看出,在未使用到單例類時(shí),單例類并不會(huì)加載到內(nèi)存中,只有我們需要使用到他的時(shí)候,才會(huì)進(jìn)行實(shí)例化。

這種單例解決了單例的延遲加載,但是由于引入了同步的關(guān)鍵字,因此在多線程的環(huán)境下,所需的消耗的時(shí)間要遠(yuǎn)遠(yuǎn)大于第一種單例。我們可以通過一段測試代碼來說明這個(gè)問題。

public class TestSingleton {

@Test

public void test(){

long beginTime1 = System.currentTimeMillis();

for(int i=0;i100000;i++){

Singleton.getInstance();

}

System.out.println("單例1花費(fèi)時(shí)間:"+(System.currentTimeMillis()-beginTime1));

long beginTime2 = System.currentTimeMillis();

for(int i=0;i100000;i++){

Singleton1.getInstance();

}

System.out.println("單例2花費(fèi)時(shí)間:"+(System.currentTimeMillis()-beginTime2));

}

}

最后輸出的是:

單例1花費(fèi)時(shí)間:0

單例2花費(fèi)時(shí)間:10

可以看到,使用第一種單例耗時(shí)0ms,第二種單例耗時(shí)10ms,性能上存在明顯的差異。為了使用延遲加載的功能,而導(dǎo)致單例的性能上存在明顯差異,

是不是會(huì)得不償失呢?是否可以找到一種更好的解決的辦法呢?既可以解決延遲加載,又不至于性能損耗過多,所以,也就有了第三種單例:

3.內(nèi)部類托管單例

public class Singleton2 {

private Singleton2(){}

private static class SingletonHolder{

private static Singleton2 instance=new Singleton2();

}

private static Singleton2 getInstance(){

return SingletonHolder.instance;

}

}

在這個(gè)單例中,我們通過靜態(tài)內(nèi)部類來托管單例,當(dāng)這個(gè)單例被加載時(shí),不會(huì)初始化單例類,只有當(dāng)getInstance方法被調(diào)用的時(shí)候,才會(huì)去加載

SingletonHolder,從而才會(huì)去初始化instance。并且,單例的加載是在內(nèi)部類的加載的時(shí)候完成的,所以天生對(duì)線程友好,而且也不需要

synchnoized關(guān)鍵字,可以說是兼具了以上的兩個(gè)優(yōu)點(diǎn)。

4.總結(jié)

一般來說,上述的單例已經(jīng)基本可以保證在一個(gè)系統(tǒng)中只會(huì)存在一個(gè)實(shí)例了,但是,仍然可能會(huì)有其他的情況,導(dǎo)致系統(tǒng)生成多個(gè)單例,請(qǐng)看以下情況:

public class Singleton3 implements Serializable{

private Singleton3(){}

private static class SingletonHolder{

private static Singleton3 instance = new Singleton3();

}

public static Singleton3 getInstance(){

return SingletonHolder.instance;

}

}

通過一段代碼來測試:

@Test

public void test() throws Exception{

Singleton3 s1 = null;

Singleton3 s2 = Singleton3.getInstance();

//1.將實(shí)例串行話到文件

FileOutputStream fos = new FileOutputStream("singleton.txt");

ObjectOutputStream oos =new ObjectOutputStream(fos);

oos.writeObject(s2);

oos.flush();

oos.close();

//2.從文件中讀取出單例

FileInputStream fis = new FileInputStream("singleton.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

s1 = (Singleton3) ois.readObject();

if(s1==s2){

System.out.println("同一個(gè)實(shí)例");

}else{

System.out.println("不是同一個(gè)實(shí)例");

}

}

輸出:

不是同一個(gè)實(shí)例

可以看到當(dāng)我們把單例反序列化后,生成了多個(gè)不同的單例類,此時(shí),我們必須在原來的代碼中加入readResolve()函數(shù),來阻止它生成新的單例

public class Singleton3 implements Serializable{

private Singleton3(){}

private static class SingletonHolder{

private static Singleton3 instance = new Singleton3();

}

public static Singleton3 getInstance(){

return SingletonHolder.instance;

}

//阻止生成新的實(shí)例

public Object readResolve(){

return SingletonHolder.instance;

}

}

再次測試時(shí),就可以發(fā)現(xiàn)他們生成的是同一個(gè)實(shí)例了。

求這段JAVA程序代碼

//Example.java

class A{

float a;

static float b;

void setA(float a ){

this.a = a;

}

void setB(float b){

this.b = b;

}

float getA() {

return a;

}

float getB() {

return b;

}

void inputA() {

System.out.println(a);

}

static void inputB() {

System.out.println(b);

}

}

public class Example {

public static void main (String args[]){

/*代碼5] //通過類名操作類變量b,并賦值100

[代碼6] //通過類名調(diào)用方法inputB()

A cat=new A();

A dog=new A();

[代碼7] //cat調(diào)用方法setA(int a)將cat的成員a的值設(shè)置為200

[代碼8] //cat調(diào)用方法setB(int b)將cat的成員b的值設(shè)置為400

[代碼9] //dog調(diào)用方法setA(int a)將dog的成員a的值設(shè)置為300

[代碼10] //dog調(diào)用方法setB(int b)將dog的成員b的值設(shè)置為800

[代碼11] //cat調(diào)用方法inputA()

[代碼12] //cat調(diào)用方法inputB()

[代碼13] //dog調(diào)用方法inputA()

[代碼14] //dog調(diào)用方法inputB()*/

A.b = 100;

A.inputB();

A cat = new A();

A dog = new A();

cat.setA(200);

cat.setB(300);

dog.setA(300);

dog.setB(800);

cat.inputA();

cat.inputB();

dog.inputA();

dog.inputB();

}

}

有一個(gè)要說明的是,setA()與setB()的形參是浮點(diǎn)型的,所以如樓上所說,樓主代碼7到代碼10的形參設(shè)錯(cuò)了。但200,400,300,800是可以的。只是將int改為float.

求一計(jì)算器java代碼,最好簡單一點(diǎn)(圖形界面)

import java.awt.*;

import java.awt.event.*;

import java.lang.*;

import javax.swing.*;

public class Counter extends Frame

{

//聲明三個(gè)面板的布局

GridLayout gl1,gl2,gl3;

Panel p0,p1,p2,p3;

JTextField tf1;

TextField tf2;

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;

StringBuffer str;//顯示屏所顯示的字符串

double x,y;//x和y都是運(yùn)算數(shù)

int z;//Z表示單擊了那一個(gè)運(yùn)算符.0表示"+",1表示"-",2表示"*",3表示"/"

static double m;//記憶的數(shù)字

public Counter()

{

gl1=new GridLayout(1,4,10,0);//實(shí)例化三個(gè)面板的布局

gl2=new GridLayout(4,1,0,15);

gl3=new GridLayout(4,5,10,15);

tf1=new JTextField(27);//顯示屏

tf1.setHorizontalAlignment(JTextField.RIGHT);

tf1.setEnabled(false);

tf1.setText("0");

tf2=new TextField(10);//顯示記憶的索引值

tf2.setEditable(false);

//實(shí)例化所有按鈕、設(shè)置其前景色并注冊(cè)監(jiān)聽器

b0=new Button("Backspace");

b0.setForeground(Color.red);

b0.addActionListener(new Bt());

b1=new Button("CE");

b1.setForeground(Color.red);

b1.addActionListener(new Bt());

b2=new Button("C");

b2.setForeground(Color.red);

b2.addActionListener(new Bt());

b3=new Button("MC");

b3.setForeground(Color.red);

b3.addActionListener(new Bt());

b4=new Button("MR");

b4.setForeground(Color.red);

b4.addActionListener(new Bt());

b5=new Button("MS");

b5.setForeground(Color.red);

b5.addActionListener(new Bt());

b6=new Button("M+");

b6.setForeground(Color.red);

b6.addActionListener(new Bt());

b7=new Button("7");

b7.setForeground(Color.blue);

b7.addActionListener(new Bt());

b8=new Button("8");

b8.setForeground(Color.blue);

b8.addActionListener(new Bt());

b9=new Button("9");

b9.setForeground(Color.blue);

b9.addActionListener(new Bt());

b10=new Button("/");

b10.setForeground(Color.red);

b10.addActionListener(new Bt());

b11=new Button("sqrt");

b11.setForeground(Color.blue);

b11.addActionListener(new Bt());

b12=new Button("4");

b12.setForeground(Color.blue);

b12.addActionListener(new Bt());

b13=new Button("5");

b13.setForeground(Color.blue);

b13.addActionListener(new Bt());

b14=new Button("6");

b14.setForeground(Color.blue);

b14.addActionListener(new Bt());

b15=new Button("*");

b15.setForeground(Color.red);

b15.addActionListener(new Bt());

b16=new Button("%");

b16.setForeground(Color.blue);

b16.addActionListener(new Bt());

b17=new Button("1");

b17.setForeground(Color.blue);

b17.addActionListener(new Bt());

b18=new Button("2");

b18.setForeground(Color.blue);

b18.addActionListener(new Bt());

b19=new Button("3");

b19.setForeground(Color.blue);

b19.addActionListener(new Bt());

b20=new Button("-");

b20.setForeground(Color.red);

b20.addActionListener(new Bt());

b21=new Button("1/X");

b21.setForeground(Color.blue);

b21.addActionListener(new Bt());

b22=new Button("0");

b22.setForeground(Color.blue);

b22.addActionListener(new Bt());

b23=new Button("+/-");

b23.setForeground(Color.blue);

b23.addActionListener(new Bt());

b24=new Button(".");

b24.setForeground(Color.blue);

b24.addActionListener(new Bt());

b25=new Button("+");

b25.setForeground(Color.red);

b25.addActionListener(new Bt());

b26=new Button("=");

b26.setForeground(Color.red);

b26.addActionListener(new Bt());

//實(shí)例化四個(gè)面板

p0=new Panel();

p1=new Panel();

p2=new Panel();

p3=new Panel();

//創(chuàng)建一個(gè)空字符串緩沖區(qū)

str=new StringBuffer();

//添加面板p0中的組件和設(shè)置其在框架中的位置和大小

p0.add(tf1);

p0.setBounds(10,25,300,40);

//添加面板p1中的組件和設(shè)置其在框架中的位置和大小

p1.setLayout(gl1);

p1.add(tf2);

p1.add(b0);

p1.add(b1);

p1.add(b2);

p1.setBounds(10,65,300,25);

//添加面板p2中的組件并設(shè)置其的框架中的位置和大小

p2.setLayout(gl2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.setBounds(10,110,40,150);

//添加面板p3中的組件并設(shè)置其在框架中的位置和大小

p3.setLayout(gl3);//設(shè)置p3的布局

p3.add(b7);

p3.add(b8);

p3.add(b9);

p3.add(b10);

p3.add(b11);

p3.add(b12);

p3.add(b13);

p3.add(b14);

p3.add(b15);

p3.add(b16);

p3.add(b17);

p3.add(b18);

p3.add(b19);

p3.add(b20);

p3.add(b21);

p3.add(b22);

p3.add(b23);

p3.add(b24);

p3.add(b25);

p3.add(b26);

p3.setBounds(60,110,250,150);

//設(shè)置框架中的布局為空布局并添加4個(gè)面板

setLayout(null);

add(p0);

add(p1);

add(p2);

add(p3);

setResizable(false);//禁止調(diào)整框架的大小

//匿名類關(guān)閉窗口

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e1)

{

System.exit(0);

}

});

setBackground(Color.lightGray);

setBounds(100,100,320,280);

setVisible(true);

}

//構(gòu)造監(jiān)聽器

class Bt implements ActionListener

{

public void actionPerformed(ActionEvent e2)

{

try{

if(e2.getSource()==b1)//選擇"CE"清零

{

tf1.setText("0");//把顯示屏清零

str.setLength(0);//清空字符串緩沖區(qū)以準(zhǔn)備接收新的輸入運(yùn)算數(shù)

}

else if(e2.getSource()==b2)//選擇"C"清零

{

tf1.setText("0");//把顯示屏清零

str.setLength(0);

}

else if(e2.getSource()==b23)//單擊"+/-"選擇輸入的運(yùn)算數(shù)是正數(shù)還是負(fù)數(shù)

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText(""+(-x));

}

else if(e2.getSource()==b25)//單擊加號(hào)按鈕獲得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);//清空緩沖區(qū)以便接收新的另一個(gè)運(yùn)算數(shù)

y=0d;

z=0;

}

else if(e2.getSource()==b20)//單擊減號(hào)按鈕獲得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=1;

}

else if(e2.getSource()==b15)//單擊乘號(hào)按鈕獲得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=2;

}

else if(e2.getSource()==b10)//單擊除號(hào)按鈕獲得x的值和z的值并空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=3;

}

else if(e2.getSource()==b26)//單擊等號(hào)按鈕輸出計(jì)算結(jié)果

{

str.setLength(0);

switch(z)

{

case 0 : tf1.setText(""+(x+y));break;

case 1 : tf1.setText(""+(x-y));break;

case 2 : tf1.setText(""+(x*y));break;

case 3 : tf1.setText(""+(x/y));break;

}

}

else if(e2.getSource()==b24)//單擊"."按鈕輸入小數(shù)

{

if(tf1.getText().trim().indexOf(′.′)!=-1)//判斷字符串中是否已經(jīng)包含了小數(shù)點(diǎn)

{

}

else//如果沒數(shù)點(diǎn)有小

{

if(tf1.getText().trim().equals("0"))//如果初時(shí)顯示為0

{

str.setLength(0);

tf1.setText((str.append("0"+e2.getActionCommand())).toString());

}

else if(tf1.getText().trim().equals(""))//如果初時(shí)顯示為空則不做任何操作

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

}

}

y=0d;

}

else if(e2.getSource()==b11)//求平方根

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText("數(shù)字格式異常");

if(x0)

tf1.setText("負(fù)數(shù)沒有平方根");

else

tf1.setText(""+Math.sqrt(x));

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b16)//單擊了"%"按鈕

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText(""+(0.01*x));

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b21)//單擊了"1/X"按鈕

{

x=Double.parseDouble(tf1.getText().trim());

if(x==0)

{

tf1.setText("除數(shù)不能為零");

}

else

{

tf1.setText(""+(1/x));

}

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b3)//MC為清除內(nèi)存

{

m=0d;

tf2.setText("");

str.setLength(0);

}

else if(e2.getSource()==b4)//MR為重新調(diào)用存儲(chǔ)的數(shù)據(jù)

{

if(tf2.getText().trim()!="")//有記憶數(shù)字

{

tf1.setText(""+m);

}

}

else if(e2.getSource()==b5)//MS為存儲(chǔ)顯示的數(shù)據(jù)

{

m=Double.parseDouble(tf1.getText().trim());

tf2.setText("M");

tf1.setText("0");

str.setLength(0);

}

else if(e2.getSource()==b6)//M+為將顯示的數(shù)字與已經(jīng)存儲(chǔ)的數(shù)據(jù)相加要查看新的數(shù)字單擊MR

{

m=m+Double.parseDouble(tf1.getText().trim());

}

else//選擇的是其他的按鈕

{

if(e2.getSource()==b22)//如果選擇的是"0"這個(gè)數(shù)字鍵

{

if(tf1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

y=Double.parseDouble(tf1.getText().trim());

}

}

else if(e2.getSource()==b0)//選擇的是“BackSpace”按鈕

{

if(!tf1.getText().trim().equals("0"))//如果顯示屏顯示的不是零

{

if(str.length()!=1)

{

tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字符串越界異常

}

else

{

tf1.setText("0");

str.setLength(0);

}

}

y=Double.parseDouble(tf1.getText().trim());

}

else//其他的數(shù)字鍵

{

tf1.setText(str.append(e2.getActionCommand()).toString());

y=Double.parseDouble(tf1.getText().trim());

}

}

}

catch(NumberFormatException e){

tf1.setText("數(shù)字格式異常");

}

catch(StringIndexOutOfBoundsException e){

tf1.setText("字符串索引越界");

}

}

}

public static void main(String args[])

{

new Counter();

}

}

有沒有java語言中的spark graphx代碼實(shí)例

首先,在SparkShell中運(yùn)行一下代碼importorg.apache.spark._importorg.apache.spark.graphx._//TomakesomeoftheexamplesworkwewillalsoneedRDDimportorg.apache.spark.rdd.RDD//AssumetheSparkContexthasalreadybeencon

java圖片加水印代碼 最好有實(shí)例?。?!先謝了??!

文字水印

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.swing.*;

import com.sun.image.codec.jpeg.*;

public class WaterSet {

/**

* 給圖片添加水印

*

* @param filePath

* 需要添加水印的圖片的路徑

* @param markContent

* 水印的文字

* @param markContentColor

* 水印文字的顏色

* @param qualNum

* 圖片質(zhì)量

* @return

*/

public boolean createMark(String filePath, String markContent,

Color markContentColor, float qualNum) {

ImageIcon imgIcon = new ImageIcon(filePath);

Image theImg = imgIcon.getImage();

int width = theImg.getWidth(null);

int height = theImg.getHeight(null);

BufferedImage bimage = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

Graphics2D g = bimage.createGraphics();

g.setColor(markContentColor);

g.setBackground(Color.white);

g.drawImage(theImg, 0, 0, null);

g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和設(shè)置水印文字出現(xiàn)的內(nèi)容

g.dispose();

try {

FileOutputStream out = new FileOutputStream(filePath);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);

param.setQuality(qualNum, true);

encoder.encode(bimage, param);

out.close();

} catch (Exception e) {

return false;

}

return true;

}

}

圖片水印

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtils {

public ImageUtils() {

}

/*

* public final static String getPressImgPath() { return ApplicationContext

* .getRealPath("/template/data/util/shuiyin.gif"); }

*/

/**

* 把圖片印刷到圖片上

*

* @param pressImg --

* 水印文件

* @param targetImg --

* 目標(biāo)文件

* @param x

* --x坐標(biāo)

* @param y

* --y坐標(biāo)

*/

public final static void pressImage(String pressImg, String targetImg,

int x, int y) {

try {

//目標(biāo)文件

File _file = new File(targetImg);

Image src = ImageIO.read(_file);

int wideth = src.getWidth(null);

int height = src.getHeight(null);

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

g.drawImage(src, 0, 0, wideth, height, null);

//水印文件

File _filebiao = new File(pressImg);

Image src_biao = ImageIO.read(_filebiao);

int wideth_biao = src_biao.getWidth(null);

int height_biao = src_biao.getHeight(null);

g.drawImage(src_biao, (wideth - wideth_biao) / 2,

(height - height_biao) / 2, wideth_biao, height_biao, null);

//水印文件結(jié)束

g.dispose();

FileOutputStream out = new FileOutputStream(targetImg);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(image);

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 打印文字水印圖片

*

* @param pressText

* --文字

* @param targetImg --

* 目標(biāo)圖片

* @param fontName --

* 字體名

* @param fontStyle --

* 字體樣式

* @param color --

* 字體顏色

* @param fontSize --

* 字體大小

* @param x --

* 偏移量

* @param y

*/

public static void pressText(String pressText, String targetImg,

String fontName, int fontStyle, int color, int fontSize, int x,

int y) {

try {

File _file = new File(targetImg);

Image src = ImageIO.read(_file);

int wideth = src.getWidth(null);

int height = src.getHeight(null);

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

g.drawImage(src, 0, 0, wideth, height, null);

// String s="";

g.setColor(Color.RED);

g.setFont(new Font(fontName, fontStyle, fontSize));

g.drawString(pressText, wideth - fontSize - x, height - fontSize

/ 2 - y);

g.dispose();

FileOutputStream out = new FileOutputStream(targetImg);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(image);

out.close();

} catch (Exception e) {

System.out.println(e);

}

}

public static void main(String[] args) {

pressImage("F:/logo.png", "F:/123.jpg", 0, 0);

}

}

網(wǎng)頁題目:java優(yōu)秀的代碼實(shí)例 JAVA編程實(shí)例
鏈接地址:http://www.muchs.cn/article10/hggigo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站搜索引擎優(yōu)化品牌網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管