当前位置: 技术问答>java相关
JAVA画虚线,谢谢!
来源: 互联网 发布时间:2015-07-23
本文导语: 画线条是graphics.drawLine 那么画虚线又怎么画呢?谢谢! | Here is a demo code. Enjoy it. import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.font.TextLayout; import java.awt.font.FontRenderCon...
画线条是graphics.drawLine
那么画虚线又怎么画呢?谢谢!
那么画虚线又怎么画呢?谢谢!
|
Here is a demo code. Enjoy it.
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import javax.swing.*;
/**
* Various shapes stroked with a dashing pattern.
*/
public class Dash extends JApplet {
public void init() {
setBackground(Color.white);
}
public void drawDemo(int w, int h, Graphics2D g2) {
FontRenderContext frc = g2.getFontRenderContext();
Font font = g2.getFont();
TextLayout tl = new TextLayout("Dashes", font, frc);
float sw = (float) tl.getBounds().getWidth();
float sh = (float) tl.getAscent() + tl.getDescent();
g2.setColor(Color.black);
tl.draw(g2, (float) (w/2-sw/2), sh+5);
BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0);
g2.setStroke(dotted);
g2.drawRect(3,3,w-6,h-6);
int x = 0; int y = h-34;
BasicStroke bs[] = new BasicStroke[6];
float j = 1.1f;
for (int i = 0; i
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import javax.swing.*;
/**
* Various shapes stroked with a dashing pattern.
*/
public class Dash extends JApplet {
public void init() {
setBackground(Color.white);
}
public void drawDemo(int w, int h, Graphics2D g2) {
FontRenderContext frc = g2.getFontRenderContext();
Font font = g2.getFont();
TextLayout tl = new TextLayout("Dashes", font, frc);
float sw = (float) tl.getBounds().getWidth();
float sh = (float) tl.getAscent() + tl.getDescent();
g2.setColor(Color.black);
tl.draw(g2, (float) (w/2-sw/2), sh+5);
BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0);
g2.setStroke(dotted);
g2.drawRect(3,3,w-6,h-6);
int x = 0; int y = h-34;
BasicStroke bs[] = new BasicStroke[6];
float j = 1.1f;
for (int i = 0; i