50,528
社区成员
发帖
与我相关
我的任务
分享
package com.yiibai.gui;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SwingControlDemo extends JPanel {
public void init() {
//this.setOpaque(true);
setBackground(Color.red);
//setForeground(Color.red);
}
public void paint(Graphics g) {
//super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.black);
int offsetX=150;
int offsetY=7;
//g2.draw(new Line2D.Double(offsetX, offsetY, 20, 200));
g2.drawString("银行存款", offsetX, 20);
drawTriangle(g2,Color.black,offsetX+57,offsetY+5,offsetX+65,offsetY+15,offsetX+50,offsetY+15);
g2.setPaint(Color.magenta);
drawAL(140,90,200,25,g2);
}
public static void main(String s[]) {
JFrame f = new JFrame("Line");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
SwingControlDemo applet = new SwingControlDemo();
JLabel headerLabel = new JLabel("");
f.getContentPane().add(applet);
//f.setLayout(null);
//applet.init();
f.pack();
f.setSize(new Dimension(300, 300));
f.setVisible(true);
f.add(headerLabel);
headerLabel.setText("Control in action: Button");
//headerLabel.setBounds(230,180,500,200);
//headerLabel.setLocation(50,50);
}
public static void drawAL(int sx, int sy, int ex, int ey, Graphics2D g2){
double H = 15; // 箭头高度
double L = 5; // 底边的一半
int x3 = 0;
int y3 = 0;
int x4 = 0;
int y4 = 0;
double awrad = Math.atan(L / H); // 箭头角度
double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度
double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);
double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);
double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点
double y_3 = ey - arrXY_1[1];
double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点
double y_4 = ey - arrXY_2[1];
Double X3 = new Double(x_3);
x3 = X3.intValue();
Double Y3 = new Double(y_3);
y3 = Y3.intValue();
Double X4 = new Double(x_4);
x4 = X4.intValue();
Double Y4 = new Double(y_4);
y4 = Y4.intValue();
// 画线
g2.drawLine(sx, sy, ex, ey);
//
GeneralPath triangle = new GeneralPath();
triangle.moveTo(ex, ey);
triangle.lineTo(x3, y3);
triangle.lineTo(x4, y4);
triangle.closePath();
//实心箭头
g2.fill(triangle);
//非实心箭头
//g2.draw(triangle);
}
// 计算旋转向量坐标
public static double[] rotateVec(int px, int py, double ang, boolean isChLen, double newLen) {
double mathstr[] = new double[2];
// 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
double vx = px * Math.cos(ang) - py * Math.sin(ang);
double vy = px * Math.sin(ang) + py * Math.cos(ang);
if (isChLen) {
double d = Math.sqrt(vx * vx + vy * vy);
vx = vx / d * newLen;
vy = vy / d * newLen;
mathstr[0] = vx;
mathstr[1] = vy;
}
return mathstr;
}
//给出三个顶点画三角形
public void drawTriangle(Graphics2D g,Color color,int x1,int y1,int x2,int y2,int x3,int y3) {
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
filledPolygon.addPoint(x2,y2);
filledPolygon.addPoint(x3,y3);
g.setColor(color);
//g.drawPolygon(filledPolygon);
g.fill(filledPolygon);
}
}
请教大家怎么控制那个JLabel的位置,加入
f.setLayout(null);
后,原来graphics2D画的图都看不见了,而且调整
headerLabel.setBounds(230,180,500,200);
headerLabel.setLocation(50,50);
这两条语句的坐标好像都没什么用。