5个回答
展开全部
java swing中是可以绘制虚线的,主要是使用paint画笔工具绘制,如下代码:
package com.qiu.lin.he;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ceshi {
public static void main(String[] args) {
JPanel p = new JPanel() {
//使用画笔绘制虚线
public void paint(Graphics gr) {
Graphics2D g = (Graphics2D) gr;
g.setBackground(Color.black);
g.clearRect(0, 0, getWidth(), getHeight());
Stroke dash = new BasicStroke(2.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 3.5f, new float[] { 15, 10, },
0f);
g.setStroke(dash);
g.setColor(Color.yellow);
g.drawRect(20, 20, getWidth() - 40, getHeight() - 40);
g.dispose();
}
};
//新建一个窗口
JFrame f = new JFrame("Dash Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
p.setPreferredSize(new Dimension(400, 300));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
运行结果如下:
展开全部
如何还没解决,请参考:http://java.sun.com/javase/6/docs/api/java/awt/BasicStroke.html
这是一个笔触类,其中这个构造函数就是用来创建虚线的:
BasicStroke
public BasicStroke(float width,
int cap,
int join,
float miterlimit,
float[] dash,
float dash_phase)
Constructs a new BasicStroke with the specified attributes.
Parameters:
width - the width of this BasicStroke. The width must be greater than or equal to 0.0f. If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting.
cap - the decoration of the ends of a BasicStroke
join - the decoration applied where path segments meet
miterlimit - the limit to trim the miter join. The miterlimit must be greater than or equal to 1.0f.
dash - the array representing the dashing pattern
dash_phase - the offset to start the dashing pattern
Throws:
IllegalArgumentException - if width is negative
IllegalArgumentException - if cap is not either CAP_BUTT, CAP_ROUND or CAP_SQUARE
IllegalArgumentException - if miterlimit is less than 1 and join is JOIN_MITER
IllegalArgumentException - if join is not either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER
IllegalArgumentException - if dash_phase is negative and dash is not null
IllegalArgumentException - if the length of dash is zero
IllegalArgumentException - if dash lengths are all zero.
正在下载JDK中,如果一个小时后你仍没解决,我给你一个小实例
DashStroke.java
-----------------
import java.awt.*;
import javax.swing.*;
public class DashStroke{
public static void main(String[] args){
JPanel p = new JPanel(){
public void paint(Graphics gr){
Graphics2D g = (Graphics2D)gr;
g.setBackground(Color.black);
g.clearRect(0,0,getWidth(),getHeight());
Stroke dash = new BasicStroke(2.5f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,
3.5f,new float[]{15,10,},0f);
g.setStroke(dash);
g.setColor(Color.yellow);
g.drawRect(20,20,getWidth()-40,getHeight()-40);
g.dispose();
}};
JFrame f= new JFrame("Dash Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
p.setPreferredSize(new Dimension(400,300));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}}
这是一个笔触类,其中这个构造函数就是用来创建虚线的:
BasicStroke
public BasicStroke(float width,
int cap,
int join,
float miterlimit,
float[] dash,
float dash_phase)
Constructs a new BasicStroke with the specified attributes.
Parameters:
width - the width of this BasicStroke. The width must be greater than or equal to 0.0f. If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting.
cap - the decoration of the ends of a BasicStroke
join - the decoration applied where path segments meet
miterlimit - the limit to trim the miter join. The miterlimit must be greater than or equal to 1.0f.
dash - the array representing the dashing pattern
dash_phase - the offset to start the dashing pattern
Throws:
IllegalArgumentException - if width is negative
IllegalArgumentException - if cap is not either CAP_BUTT, CAP_ROUND or CAP_SQUARE
IllegalArgumentException - if miterlimit is less than 1 and join is JOIN_MITER
IllegalArgumentException - if join is not either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER
IllegalArgumentException - if dash_phase is negative and dash is not null
IllegalArgumentException - if the length of dash is zero
IllegalArgumentException - if dash lengths are all zero.
正在下载JDK中,如果一个小时后你仍没解决,我给你一个小实例
DashStroke.java
-----------------
import java.awt.*;
import javax.swing.*;
public class DashStroke{
public static void main(String[] args){
JPanel p = new JPanel(){
public void paint(Graphics gr){
Graphics2D g = (Graphics2D)gr;
g.setBackground(Color.black);
g.clearRect(0,0,getWidth(),getHeight());
Stroke dash = new BasicStroke(2.5f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,
3.5f,new float[]{15,10,},0f);
g.setStroke(dash);
g.setColor(Color.yellow);
g.drawRect(20,20,getWidth()-40,getHeight()-40);
g.dispose();
}};
JFrame f= new JFrame("Dash Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
p.setPreferredSize(new Dimension(400,300));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-03-21
展开全部
可以的,看一下Graphics2D 的一个方法 setStroke (Stroke stroke);
然后将stroke设置成虚线就成了!
然后将stroke设置成虚线就成了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-03-21
展开全部
有~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看javaME
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询