当前位置:   article > 正文

java输出文字图片_JAVA实现在图片上打印文字

怎么在java图形界面上进行打印文字内容

在网上找了许多JAVA关于如何在背景图上打印文字的方法,但实施时却遇到了一些问题

不过还好,总算是解决了

package com.mhl.view;

import javax.imageio.ImageIO;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

public class Index extends JWindow implements Runnable {

public Index()

{

PaintThread pt=new

PaintThread();

this.add(pt);

int

width=Toolkit.getDefaultToolkit().getScreenSize().width;

int

height=Toolkit.getDefaultToolkit().getScreenSize().height;

this.setSize(400,300);

this.setLocation(width/2-200,height/2-150);

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method

stub

Index index=new Index();

Thread t=new

Thread(index);

t.start();

}

public void run() {

while(true)

{

try {

Thread.sleep(500*29);

} catch

(InterruptedException e) {

//

TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("登陆成功");

System.exit(0);

}

}

}

//开发闪屏类

class PaintThread extends JPanel implements Runnable

{

//诗歌的初始位置,定义为两个常量

public static final int POETRYX=200;

public static final int POETRYY=60;

//定义一幅背景图

//Image img;

//ImageIcon imgIcon;

BufferedImage buffImg;

//记录该打印的字数

int k=0;

//定义一个线程

Thread t;

//记录进度条的进度

int x=0;

//定义需要画的汉字

String[]

poetry={"满","汉","楼","融","满","汉","精","华","做","天","下","名","菜","招","八","方","食","客","结","四","海","良","朋","|","小","华","制","作"};

int poetryCount=0;

int charCount=0;

int rowCount=0;

//定义一个变量控制线程的终止

private boolean bStop=true;

//定义一个字体

Font f=new Font("隶书",Font.PLAIN,18);

public PaintThread()

{

//img=Toolkit.getDefaultToolkit().createImage("images\\index.jpg");

//imgIcon=new

ImageIcon(img);

File filePath=new

File("images\\index.jpg");

try {

buffImg=ImageIO.read(filePath);

} catch (IOException e)

{

// TODO

Auto-generated catch block

e.printStackTrace();

}

t=new

Thread(this); //初始化时启动PaintThread线程

t.start();

}

public void paintCompenet(Graphics g)

{

// Graphics

gOff=img.getGraphics();

//对进度条进行绘制

Color

c=this.getBackground();

g.setColor(Color.BLUE);

g.drawRect(10, 280, 380,

10);

if(x<375)

{ x+=13;

g.fillRect(12,

282, x, 6);

}

//对要显示的文字进行绘制

g.setFont(f);

g.setColor(Color.RED);

if(poetryCount

{ if(poetryCount<3)

{ g.drawString(poetry[poetryCount],POETRYX

, POETRYY+20+charCount*16);

poetryCount+=1;

if(charCount==2) {

rowCount++;

charCount=0;

}

else

charCount++;

}

else

if(poetryCount>=3&&poetryCount<=poetry.length-6)

{

g.drawString(poetry[poetryCount],POETRYX

-30*rowCount, POETRYY+charCount*16);

if(charCount==4)

{

rowCount+=1;

charCount=0;

poetryCount+=1;

}

else

{

charCount++;

poetryCount+=1;

}

}

else

if(poetryCount

{

g.drawString(poetry[poetryCount],POETRYX

-30*(rowCount+1), POETRYY+charCount*16+40);

poetryCount++;

charCount++;

}

} k++;

g.setColor(c);

}

public void paint(Graphics g)

{ Graphics2D

g2D=(Graphics2D)buffImg.getGraphics();

g.drawImage(buffImg,0, 0,400

,300, null);

paintCompenet(g2D);

}

public void run() {

while(bStop)

{

repaint();

try {

Thread.sleep(500);

} catch

(InterruptedException e) {

//

TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public void stop()

{

bStop=false;

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/179593
推荐阅读
相关标签
  

闽ICP备14008679号