java保存画布图片代码,java怎么导入图片的代码
java 保存图片
ImageIO.write(BufferedImage, "JPG", File);
我们提供的服务有:成都网站设计、网站建设、微信公众号开发、网站优化、网站认证、三沙ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的三沙网站制作公司
================================
传入Component保存图像的方法,你试试看还有没有变色。
public void cutScreen(Component com) {
Rectangle rect = com.getBounds();
BufferedImage bi = (BufferedImage) com.createImage(rect.width,
rect.height);
Graphics g = bi.getGraphics();
com.paint(g);
g.dispose();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(".jpg");
}
public String getDescription() {
return "*.jpg";
}
});
int type = jfc.showSaveDialog(null);
if (type == 0) {
File file = jfc.getSelectedFile();
name = file.getName().toLowerCase();
if (!name.endsWith("jpg")) {
String path = file.getAbsolutePath();
file = new File(path + ".jpg");
for (int i = 0; file.exists(); i++) {
file = new File(path + "(" + i + ").jpg");
}
}
try {
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(bi, "JPG", file);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
java swing 画图怎么保存?求详细代码
package test;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class TestJLabel2Image {
public static void main(String ds[]) {
final JFrame f = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel() {
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
g.dispose();
}
};
panel.setPreferredSize(new Dimension(555, 555));
panel.add(label, BorderLayout.CENTER);
f.getContentPane().add(panel);
f.pack();
BufferedImage image = createImage(panel);
f.dispose();
try {
ImageIO.write(image, "png", new File("d:/test.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static BufferedImage createImage(JPanel panel) {
int totalWidth = panel.getPreferredSize().width;
int totalHeight = panel.getPreferredSize().height;
BufferedImage panelImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2D = (Graphics2D) panelImage.createGraphics();
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, totalWidth, totalHeight);
g2D.translate(0, 0);
panel.paint(g2D);
g2D.dispose();
return panelImage;
}
}
java中画图其数据怎么读出和保存比如画几个圆要代码啊
下面是我写的一个小东西,绘制了若干图形:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.border.*;
public class DrawingLeftPanel extends JFrame{
private int DX=100;
private int DY=200;
Point point;
DrawingLeftPanel(){
setBackground(Color.GRAY);
setSize(300,700);
setLayout(new GridLayout(1,1));
JPanel panel=new JPanel0();
JScrollPane scrollpane=new JScrollPane(panel);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add(scrollpane);
setVisible(true);
}
public static void main(String args[]){
new DrawingLeftPanel();
}
class JPanel0 extends JPanel {
public void paintComponent(Graphics g)
{ int w=getSize().width;
int h=getSize().height;
Shape shape1,shape2,shape3,shape4,shape5,shape6,shape7,shape8,shape9;
int[] xpoints=new int[4];
int[] ypoints=new int[4];
xpoints[0]=DX;
ypoints[0]=DY;
xpoints[1]=DX-50;
ypoints[1]=DY+25;
xpoints[2]=DX;
ypoints[2]=DY+50;
xpoints[3]=DX+50;
ypoints[3]=DY+25;
//画图父类的构造函数
//System.out.println("adfafd");
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//g2.setColor(Color.BLACK);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.0f));
g2.setColor(Color.LIGHT_GRAY);
Shape e=new Ellipse2D.Double(75,50,50,50);
g2.fill(e);
g2.setColor(Color.BLACK);
g2.draw(e);//画出圆形 概念
e=new Rectangle2D.Double(50,125,100,50);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(e);
g2.setColor(Color.BLACK);
g2.draw(e);//画出矩形 原理规则
e=new Polygon(xpoints,ypoints,4);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(e);
g2.setColor(Color.BLACK);
g2.draw(e);//画出四边形 态度价值观念
shape1=new Ellipse2D.Double(150,275,50,50);
shape2=new Rectangle2D.Double(75,275,100,50);
shape3=new Ellipse2D.Double(50, 275, 50, 50);
Area area1=new Area(shape1);
Area area2=new Area(shape2);
Area area3=new Area(shape3);
area2.subtract(area1);
area3.add(area2);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(area3);
g2.setColor(Color.BLACK);
g2.draw(area3);//画出事实范例
shape4=new Ellipse2D.Double(150,350,50,50);
shape5=new Rectangle2D.Double(75,350,100,50);
shape6=new Ellipse2D.Double(50, 350, 50, 50);
Area area4=new Area(shape4);
Area area5=new Area(shape5);
Area area6=new Area(shape6);
area5.add(area4);
area5.add(area6);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(area5);
g2.setColor(Color.BLACK);
g2.draw(area5);//画出过程
shape7=new Ellipse2D.Double(150,425,50,50);
shape8=new Rectangle2D.Double(75,425,100,50);
shape9=new Ellipse2D.Double(50, 425, 50, 50);
Area area7=new Area(shape7);
Area area8=new Area(shape8);
Area area9=new Area(shape9);
area8.subtract(area7);
area8.add(area9);
g2.setColor(Color.LIGHT_GRAY);
g2.fill(area8);
area8.add(area7);
g2.setColor(Color.BLACK);
g2.draw(area7);
g2.draw(area8);//画出事实范例
shape7=new Line2D.Float(75,525,175,525);
g2.draw(shape7);
GeneralPath p = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
p.moveTo(75, 600);
// adds a cubic curve to the path
p.curveTo(100, 650, 150, 550,200,600);
// p.moveTo(w*.2f, h*.6f);
// adds a quad curve to the path
// p.quadTo(w*.5f, h*1.0f, w*.8f, h*.6f);
g2.setColor(Color.lightGray);
g2.fill(p);
g2.setColor(Color.black);
g2.draw(p);
}
}
}
用JAVA怎么 将绘图保存成图片?
用ImageIO类提供的静态方法write(img,foamt,outStream);
先指定文件:
File f=new File("路径");
文件不存在就创建
if(!f.exists()){
f.createNewFile();
}
打开文件流
FileOutputStream fos=new FileOutputStream(f);
然后写入图片数据类型是BufferedImage
ImageIO.write(img,"png",fos);
java写的画图板保存图片是怎么实现的
首先是得到图片的保存路径,
然后截取图片的路径。。
然后再用文件的输入输出流。。
把文件读入数级组中。。
再它其写到指定的文件夹中。。
这样就实现了文件的保存……
祝你早日成功!
分享文章:java保存画布图片代码,java怎么导入图片的代码
URL标题:http://ybzwz.com/article/dsgggod.html