Write it out mainly in the hope of encountering the guidance of the god, which is too slow and tired to learn while trying.I would like to thank you for having more useful materials or books about java drawing, JFrame,JPanel,JComponent,Graphics,Graphices2D,Graphices3D,Shape, these differences and associations with practical reference books!!!
In fact, the main idea is to use the component JComponent, some drawing methods are more convenient.However, the returned object does not know how to handle the export, so we try and experiment with these three export methods.
1. By exporting jfream window pictures (in this way, both jfream window and picture save will be displayed, jfream's black frame background will be displayed, ask God for your help)
package com.dasenlin.test;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import com.dasenlin.util.MyFrame;
public class TestJframePic {
public static final String DEST4 = "C:/Users/Toshiba/Desktop/testjframe.png";
/**
* @param args
*/
public static void main(String[] args) {
/*
* Create windows and components in AWT's event queue thread to ensure thread safety.
* That is, component creation, drawing, and event response need to be on the same thread.
*/
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// Create window object
MyFrame frame = new MyFrame("Broken line",250,300);
// Display window (must be displayed, otherwise it is a black box, or this is a bit problematic, jfream's black box background will be displayed, ask God for help)
frame.setVisible(true);
savePic(frame);
}
});
}
public static void savePic(JFrame jf){
//Get Window Content Panel
Container content=jf.getContentPane();
//Create Buffered Picture Object
BufferedImage img=new BufferedImage( jf.getWidth(),jf.getHeight(),BufferedImage.TYPE_INT_RGB);
//Get Graphic Objects
Graphics2D g2d = img.createGraphics();
//Output Window Content Panel to Graphic Object
content.printAll(g2d);
//Save as Picture
try{
ImageIO.write(img, "jpg", new File( DEST4));
}catch (IOException e){
e.printStackTrace();
}
//Release graphic objects
g2d.dispose();
}
}
2. Export Jpanel pictures (this way you can export without considering jfream's black frame background or even without displaying jfream)
package com.dasenlin.util;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
*
* @author Toshiba
*
*/
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String DEST4 = "C:/Users/Toshiba/Desktop/testjframe.png";
public MyFrame(String title,Integer windowW,Integer windowH) {
super();
initFrame(title,windowW,windowH);
}
private void initFrame(String title,Integer windowW,Integer windowH) {
// Set window title and window size
setTitle(title);
setSize(windowW, windowH);
// Set the default action for the window close button (exit the process when you click Close)
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Set the window position to the center of the screen
setLocationRelativeTo(null);
// Set up the content panel of the window
MyPanel panel = new MyPanel(this);
setContentPane(panel);
//Your own JPanel
// Must have size
panel.setSize(windowW, windowH);
BufferedImage image = new BufferedImage(windowW, windowH, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
panel.paint(g2);
try {
ImageIO.write(image, "jpg", new File(DEST4));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Export with graphics drawing method in direct graphics2D
public static void savePic(JFrame jf){
BufferedImage images= DepParserSvgUtil.depParserSvg(dataList);
try{
ImageIO.write(images, "PNG", new File( DEST4));
}catch (IOException e){
e.printStackTrace();
}
}
public static BufferedImage depParserSvg(List<Map<String,Object>> yicunList){
BufferedImage bi = new BufferedImage(DEFAULT_W, DEFAULT_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
//Set Brush (Set Background)
g2d.setPaint(Color.WHITE);
g2d.fillRect(5, 5, DEFAULT_W, DEFAULT_H);
//draw string
g2d.setPaint(Color.BLACK);
Font f = new Font("Song Style", Font.PLAIN, ZH_WORD_WIDTH);
g2d.setFont(f);
FontRenderContext context = g2d.getFontRenderContext();
for(int w=0; w<yicunList.size();w++){
String word = yicunList.get(w).get("word").toString();
String mark = yicunList.get(w).get("id").toString()+"|"+yicunList.get(w).get("post").toString();
String lable = yicunList.get(w).get("dep_type").toString();
float wordx = (float) WORDS_TRANSLATE.get(w).get("x");
float wordy = (float) WORDS_TRANSLATE.get(w).get("y");
g2d.drawString(word, wordx, wordy);
Rectangle2D bounds = f.getStringBounds(word, context);
g2d.draw(bounds);
double wordW=bounds.getWidth();//Text Width
double wordH=bounds.getHeight();//Text Height
double ascent=-bounds.getY();//Text Uphill Slope
LineMetrics metrics=f.getLineMetrics(word, context);
float descent=metrics.getDescent();//Text Downslope
float leading=metrics.getLeading();//Text Line Spacing
float markx = wordx;
float marky = (float) (wordy+descent+leading+wordH);
g2d.drawString(mark, markx, marky);
bounds = f.getStringBounds(mark, context);
g2d.draw(bounds);
double markW=bounds.getWidth();//Text Width
//Draw border rectangle
double borderW = (double) Math.max(wordW, markW);
Rectangle2D msgRect=new Rectangle2D.Double(wordx,wordy-ascent,borderW,wordH*2+descent*2+leading*2);//A rectangle enclosing the text
g2d.draw(msgRect);
//Draw relationship lines
int edge1x = EDGE_PATH.get(w).get(0).get("x");
int edge1y = EDGE_PATH.get(w).get(0).get("y");
int edge2x = EDGE_PATH.get(w).get(1).get("x");
int edge2y = EDGE_PATH.get(w).get(1).get("y");
int edge3x = EDGE_PATH.get(w).get(2).get("x");
int edge3y = EDGE_PATH.get(w).get(2).get("y");
int edge4x = EDGE_PATH.get(w).get(3).get("x");
int edge4y = EDGE_PATH.get(w).get(3).get("y");
g2d.drawLine(edge1x, edge1y, edge2x, edge2y);
g2d.drawLine(edge2x, edge2y, edge3x, edge3y);
g2d.drawLine(edge3x, edge3y, edge4x, edge4y);
//Draw arrows
int rectangle1x =edge4x-5;
int rectangle1y =edge4y;
int rectangle2x =edge4x+5;
int rectangle2y =edge4y;
int rectangle3x =edge4x;
int rectangle3y =edge4y+5;
g2d.drawLine(rectangle1x, rectangle1y, rectangle2x, rectangle2y);
g2d.drawLine(rectangle2x, rectangle2y, rectangle3x, rectangle3y);
g2d.drawLine(rectangle3x, rectangle3y, rectangle1x, rectangle1y);
//Draw lable
int lablex = EDGE_LABLE_INFO.get(w).get("x");
int labley = EDGE_LABLE_INFO.get(w).get("y");
int lableH = EDGE_LABLE_INFO.get(w).get("height");
int lableW = EDGE_LABLE_INFO.get(w).get("width");
f = new Font("SansSerif",Font.BOLD,ONE_LABLE_HEIGHT);
context = g2d.getFontRenderContext();
bounds = f.getStringBounds(lable,context);
g2d.setFont(f);
g2d.clearRect((int)(lablex*1.05 - bounds.getCenterX()), (int)(labley - (lableH/2) - bounds.getCenterY()), lableW, lableH);
g2d.drawString(lable, (float) (lablex*1.05 - bounds.getCenterX()), (float) (labley - (lableH/2) - bounds.getCenterY()));
}
/****************Graphics2DDrawEnd*****************/
g2d.dispose();//Destroy resources
return bi;
}
Combined with the above, it is better to inherit Jpanel and output pictures in a practical way, taking into account jfream's front-end output and background output.There are also some good ways to use practical components.The code structure is clear.
Here is the code to draw in Jpanel
package com.dasenlin.util;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private MyFrame frame;
public MyPanel(MyFrame frame) {
this.frame = frame;
}
/**
* Draw panel content: This method is called once after creating a JPanel to draw the content.
* Then, if the data changes and needs to be redrawn, the updateUI() method can be called to trigger
* The system calls the method again to draw the contents of the updated JPanel.
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Re-invoking Graphics'drawing method will automatically erase old content when drawing
/* Open the following comment to see each drawing effect */
// 1. Segment/Polyline
drawLine(g);
// 2. Rectangle/Polygon
// drawRect(g);
// 3. Arc/Sector
// drawArc(g);
// 4. Ellipse
// drawOval(g);
// 5. Pictures
// drawImage(g);
// 6. Text
// drawString(g);
}
/**
* 1. Segment/Polyline
*/
private void drawLine(Graphics g) {
frame.setTitle("1. line segment / Broken line");
// To create a copy of Graphics, you need to change the parameters of Graphics.
// Copies must be used here to avoid affecting Graphics settings
Graphics2D g2d = (Graphics2D) g.create();
// Anti-Aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// setpc
g2d.setColor(Color.RED);
}
Write it out mainly in the hope of encountering the guidance of the god, which is too slow and tired to learn while trying.I would be grateful if there is any more useful information or books about java drawing, JFrame,JPanel,JComponent,Graphics,Graphices2d,Graphices3D,Shape, these differences and contact information books!!!