当前位置: 技术问答>java相关
画线的问题
来源: 互联网 发布时间:2015-03-03
本文导语: 在Applet中画线,如何确定线的颜色?线的粗线程度? | java.awt.Graphics 仔细看看 public abstract void setColor(Color c) 粗细比较麻烦! 一条线应当是1个像素宽吧! 你多画几条,或者干脆画矩形! ...
在Applet中画线,如何确定线的颜色?线的粗线程度?
|
java.awt.Graphics
仔细看看
public abstract void setColor(Color c)
粗细比较麻烦!
一条线应当是1个像素宽吧!
你多画几条,或者干脆画矩形!
仔细看看
public abstract void setColor(Color c)
粗细比较麻烦!
一条线应当是1个像素宽吧!
你多画几条,或者干脆画矩形!
|
Draw a line with a thickness
import java.awt.*;
import java.applet.*;
public class thickLine extends Applet {
public void init( ) { }
public void paint( Graphics g ) {
drawThickLine(g, 0, 0, getSize().width, getSize().height, 5, new Color(0).black);
drawThickLine(g, 0, getSize().height, getSize().width, 0, 5, new Color(0).red);
drawThickLine
(g, getSize().width/2, 0, getSize().width/2, getSize().height, 8, new Color(0).green);
drawThickLine
(g, 0, getSize().height/2, getSize().width, getSize().height/2, 12, new Color(0).blue);
}
public void drawThickLine(
Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {
// The thick line is in fact a filled polygon
g.setColor(c);
int dX = x2 - x1;
int dY = y2 - y1;
// line length
double lineLength = Math.sqrt(dX * dX + dY * dY);
double scale = (double)(thickness) / (2 * lineLength);
// The x and y increments from an endpoint needed to create a rectangle...
double ddx = -scale * (double)dY;
double ddy = scale * (double)dX;
ddx += (ddx > 0) ? 0.5 : -0.5;
ddy += (ddy > 0) ? 0.5 : -0.5;
int dx = (int)ddx;
int dy = (int)ddy;
// Now we can compute the corner points...
int xPoints[] = new int[4];
int yPoints[] = new int[4];
xPoints[0] = x1 + dx; yPoints[0] = y1 + dy;
xPoints[1] = x1 - dx; yPoints[1] = y1 - dy;
xPoints[2] = x2 - dx; yPoints[2] = y2 - dy;
xPoints[3] = x2 + dx; yPoints[3] = y2 + dy;
g.fillPolygon(xPoints, yPoints, 4);
}
}
Using JDK1.2 public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int width = 10;
g2d.setStroke(new BasicStroke(width));
g2d.drawLine(x1, y1, x2, y2);
}
On a Component int width = 10;
BasicStroke bs = new BasicStroke(width);
JLabel l = new JLabel();
l.getGraphics().setStroke(bs);
l.drawLine(0,0,100,100);
Using JDK1.3 import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class TestLine extends JFrame{
private MyPanel panel;
public TestLine() {
setSize(200, 200);
panel = new MyPanel();
getContentPane().add( panel, "Center" );
}
public static void main( String [] args ){
TestLine tl = new TestLine();
tl.setVisible( true );
}
}
class MyPanel extends JPanel {
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
public MyPanel(){}
public void paintComponent( Graphics g ){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke( stroke );
g2.draw(new Line2D.Double(10.0, 10.0, 100.0, 10.0));
g2.setStroke( wideStroke );
g2.draw(new Line2D.Double(10.0, 50.0, 100.0, 50.0));
}
}
import java.awt.*;
import java.applet.*;
public class thickLine extends Applet {
public void init( ) { }
public void paint( Graphics g ) {
drawThickLine(g, 0, 0, getSize().width, getSize().height, 5, new Color(0).black);
drawThickLine(g, 0, getSize().height, getSize().width, 0, 5, new Color(0).red);
drawThickLine
(g, getSize().width/2, 0, getSize().width/2, getSize().height, 8, new Color(0).green);
drawThickLine
(g, 0, getSize().height/2, getSize().width, getSize().height/2, 12, new Color(0).blue);
}
public void drawThickLine(
Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {
// The thick line is in fact a filled polygon
g.setColor(c);
int dX = x2 - x1;
int dY = y2 - y1;
// line length
double lineLength = Math.sqrt(dX * dX + dY * dY);
double scale = (double)(thickness) / (2 * lineLength);
// The x and y increments from an endpoint needed to create a rectangle...
double ddx = -scale * (double)dY;
double ddy = scale * (double)dX;
ddx += (ddx > 0) ? 0.5 : -0.5;
ddy += (ddy > 0) ? 0.5 : -0.5;
int dx = (int)ddx;
int dy = (int)ddy;
// Now we can compute the corner points...
int xPoints[] = new int[4];
int yPoints[] = new int[4];
xPoints[0] = x1 + dx; yPoints[0] = y1 + dy;
xPoints[1] = x1 - dx; yPoints[1] = y1 - dy;
xPoints[2] = x2 - dx; yPoints[2] = y2 - dy;
xPoints[3] = x2 + dx; yPoints[3] = y2 + dy;
g.fillPolygon(xPoints, yPoints, 4);
}
}
Using JDK1.2 public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int width = 10;
g2d.setStroke(new BasicStroke(width));
g2d.drawLine(x1, y1, x2, y2);
}
On a Component int width = 10;
BasicStroke bs = new BasicStroke(width);
JLabel l = new JLabel();
l.getGraphics().setStroke(bs);
l.drawLine(0,0,100,100);
Using JDK1.3 import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class TestLine extends JFrame{
private MyPanel panel;
public TestLine() {
setSize(200, 200);
panel = new MyPanel();
getContentPane().add( panel, "Center" );
}
public static void main( String [] args ){
TestLine tl = new TestLine();
tl.setVisible( true );
}
}
class MyPanel extends JPanel {
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
public MyPanel(){}
public void paintComponent( Graphics g ){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke( stroke );
g2.draw(new Line2D.Double(10.0, 10.0, 100.0, 10.0));
g2.setStroke( wideStroke );
g2.draw(new Line2D.Double(10.0, 50.0, 100.0, 50.0));
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。