MyPanel:五角星面板
package swing0401;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.TextAttribute;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Path2D.Double;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
public class MyPanel extends JPanel
{
public MyPanel()
{
//true ,背景不透明 false ,背景透明
this.setOpaque(false);
//1、设置为背景透明
}
@Override
protected void paintComponent(Graphics g)
{
//2、调用父类的此方法
super.paintComponent(g);
int width=this.getWidth();
int height=this.getHeight();
Graphics2D g2d=(Graphics2D)g;
//平滑绘制
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//3、取消掉绘制背景
// g2d.setPaint(Color.WHITE);
// g2d.fillRect(0, 0, width, height);
//10个顶点的坐标
//注意:这里是以中心为原点,X轴向右,Y轴向上的坐标轴,标准的半径为1
Point2D.Double pA=new Point2D.Double(0, 1);
Point2D.Double pB=new Point2D.Double(-0.95106, 0.30902);
Point2D.Double pC=new Point2D.Double(-0.58779, -0.80902);
Point2D.Double pD=new Point2D.Double(0.58779, -0.80902);
Point2D.Double pE=new Point2D.Double(0.95106, 0.30902);
Point2D.Double pF=new Point2D.Double(0, -0.38197);
Point2D.Double pG=new Point2D.Double(0.36328, -0.11804);
Point2D.Double pH=new Point2D.Double(0.22452, 0.30902);
Point2D.Double pI=new Point2D.Double(-0.22452, 0.30902);
Point2D.Double pJ=new Point2D.Double(-0.36328, -0.11804);
Point2D.Double[] points= {pA,pI,pB,pJ,pC,pF,pD,pG,pE,pH};
//坐标转换(指定目标正方形,在里面画一个五角星)
Rectangle rect=new Rectangle(30,30,150,150);
int radius_x=rect.width/2;
int radius_y=rect.height/2;
for(Point2D.Double point:points)
{
point.x=rect.getCenterX()+radius_x*point.x;
point.y=rect.getCenterY()-radius_y*point.y; //坐标反转
}
//圆五角星
Path2D outline=new Path2D.Double();
outline.moveTo(points[0].x, points[0].y);
for(int i=1;i<points.length;i++)
{
outline.lineTo(points[i].x, points[i].y);
}
outline.closePath();
g2d.setPaint(Color.YELLOW);
g2d.fill(outline);
//画图
g2d.setPaint(Color.GREEN);
Shape circle=new Ellipse2D.Double(rect.x, rect.y, rect.width, rect.height);
//g2d.draw(circle);
}
}
BgPanel:背景图片的绘制
package swing0401;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class BgPanel extends JPanel
{
Image image = null ;
public BgPanel()
{
URL imageUrl = MyFrame.class.getResource("/images/jundui.jpg");
try{
image = ImageIO.read(imageUrl);
}catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g)
{
int width = this.getWidth();
int height = this.getHeight();
g.clearRect(0, 0, width, height);
// 画背景图
g.drawImage(image, 0, 0, width, height, null);
// 加上一层半透明的遮罩
g.setColor(new Color(255,255,255,0));
g.fillRect(0, 0, width, height);
}
}
此处省去 MyFrame+Demo类
-------《Java学习指南系列》