1. 使JDialog位于屏幕的中央
public void setToScreenCenter(JDialog jd) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = jd.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
jd.setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) / 2);
}
2.为JDialog增加图片背景
public class MySwingTest extends JDialog{
private JPanel panelContent;
private BufferedImage image;
private Graphics g;
private Image srcImage;
String imageURL = "C:/NMS/PLATFORM/NNM5/client/resource/symbols/background/background.gif";
public MySwingTest(){
image = new BufferedImage(1200, 760, BufferedImage.TYPE_3BYTE_BGR);
image.flush();
g = image.getGraphics();
try {
srcImage = ImageIO.read(new File(imageURL));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(srcImage,0,0, null);
init();
}
private void init(){
this.setMinimumSize(new Dimension(700, 560));
this.setMaximumSize(new Dimension(700, 560));
/*try {//这样加载的是一个图片,而不是将图片作为背景
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
panelContent.add(label);
g.dispose();
}catch (Exception e) {
e.printStackTrace();
}*/
panelContent = new NewPanel();
this.setContentPane(panelContent);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setToScreenCenter(this);
this.setVisible(true);
}
public void setToScreenCenter(JDialog jd) {//上面的方法
}
public static void main(String [] args){
MySwingTest mst = new MySwingTest();
}
class NewPanel extends JPanel {
public NewPanel() {
}
public void paintComponent(Graphics g) {
int x = 0, y = 0;
ImageIcon icon = new ImageIcon(image);
g.drawImage(icon.getImage(), x, y, getSize().width,getSize().height, this);
while (true) {
g.drawImage(icon.getImage(), x, y, this);
if (x > getSize().width && y > getSize().height)
break;
// 这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口
if (x > getSize().width) {
x = 0;
y += icon.getIconHeight();
} else
x += icon.getIconWidth();
}
g.drawString("abcdefg", 200, 200);//在图片的特定位置加上文字
}
}
}
3.文字旋转显示
public class Rotate {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(new RotatePanel());
jf.setPreferredSize(new Dimension(500, 400));
jf.pack();
jf.setVisible(true);
}
}
class RotatePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
String s = "Java 2d 旋转";
Font f = new Font("宋体", Font.BOLD, 16);
Color[] colors = { Color.ORANGE, Color.LIGHT_GRAY };
g2d.setFont(f);
// 平移原点到图形环境的中心
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
// 旋转文本
for (int i = 0; i < 12; i++) {
g2d.rotate(30 * Math.PI / 180);
g2d.setPaint(colors[i % 2]);
g2d.drawString(s, 0, 0);
}
}
}
4.BufferedImage处理图像
BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
public BufferedImage getGrayPicture(BufferedImage originalImage)
{
int green=0,red=0,blue=0,rgb;
int imageWidth = originalImage.getWidth();
int imageHeight = originalImage.getHeight();
for(int i = originalImage.getMinX();i < imageWidth ;i++){
for(int j = originalImage.getMinY();j < imageHeight ;j++){
//图片的像素点其实是个矩阵,这里利用两个for循环来对每个像素进行操作
Object data = routeImage.getRaster().getDataElements(i, j, null);//获取该点像素,并以object类型表示
red = routeImage.getColorModel().getRed(data);
blue = routeImage.getColorModel().getBlue(data);
green = routeImage.getColorModel().getGreen(data);
red = (red*3 + green*6 + blue*1)/10;
green = red;
blue = green;
/*
这里将r、g、b再转化为rgb值,因为bufferedImage没有提供设置单个颜色的方法,只能设置rgb。rgb最大为8388608,当大于这个值时,应减去 255*255*25516777216
*/
rgb = (red*256 + green)*256+blue;
if(rgb>8388608){
rgb = rgb - 16777216;
}
//将rgb值写回图片
routeImage.setRGB(i, j, rgb);
}
}
return originalImage;
}