咋用java代码绘制星星 咋用java代码绘制星星图形
用java编程: 在JFrame窗体中绘制五角星,并用红色填充
import java.awt.Color;
目前创新互联公司已为1000多家的企业提供了网站建设、域名、虚拟空间、绵阳服务器托管、企业网站设计、井研网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JLabel;
public class Pentagram extends JLabel {
private static final long serialVersionUID = -3847465547528967067L;
private Point pointA;
private Point pointB;
private Point pointC;
private Point pointD;
private Point pointE;
private Point pointLeftInner;
private Point pointRightInner;
private int pix;
private static Color bgColor = Color.YELLOW;
private static Color fieldColor = Color.RED;
public static void setBgColor(Color bgColor) {
Pentagram.bgColor = bgColor;
}
public static void setFieldColor(Color fieldColor) {
Pentagram.fieldColor = fieldColor;
}
/**只有width有效,把label弄成正方的*/
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, width);
}
/**只有width有效,把label弄成正方的*/
public void setSize(int width, int height) {
super.setSize(width, width);
}
public void setSize(int size){
setSize(size, size);
}
public Pentagram(int size) {
pix = size/12;
setSize(pix*12);
initPoints();
}
private void initPoints() {
pointA = new Point(6*pix, 0); //上边中间点
pointB = new Point(0, 4*pix); //左边上面点
pointC = new Point(12*pix, 4*pix); //右边上面点
pointD = new Point(3*pix, 12*pix); //左边下面点
pointE = new Point(9*pix, 12*pix); //右边下面点
pointLeftInner = new Point(5*pix, 4*pix); //左边里面的点
pointRightInner = new Point(7*pix, 4*pix); //右边里面的点
}
private void draw(Graphics g) {
fillPolygon(g,pointA,pointLeftInner,pointRightInner);
fillPolygon(g,pointB,pointE,pointRightInner);
fillPolygon(g,pointC,pointD,pointLeftInner);
}
/**
* 已知点,填充多边形。
* @param g 画笔
* @param points 已知的'点'们
*/
private void fillPolygon(Graphics g,Point...points){
int[] xPoints = new int[points.length];
int[] yPoints = new int[points.length];
for (int i = 0; i points.length; i++) {
xPoints[i] = points[i].x;
yPoints[i] = points[i].y;
}
g.fillPolygon(xPoints, yPoints, points.length);
}
public void paint(Graphics g) {
g.setColor(bgColor);
g.fillRect(0, 0, 12*pix, 12*pix);
g.setColor(fieldColor);
draw(g);
}
}
/**以下是测试代码*/
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(200, 200, 400, 400);
JLabel label = new Pentagram(frame.getSize().height/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.setVisible(true);
}
}
实现星星闪动的java代码
package panel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
import main.MainTank;
public class TipPanel extends JPanel implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
//偶数打印,画面板
int time=0;
public void paintComponent(Graphics g){
super.paint(g);
g.fillRect(0, 0, MainTank.getWidthOfGame(), MainTank.getHeightOfGame());//绘制提示窗口
if (time%2==0){//偶数打印,画面板,造成闪烁效果
g.setColor(Color.ORANGE);
Font font=new Font("华文楷体",Font.BOLD,30);
g.setFont(font);//选用字体
g.drawString("Ready", 140, 130);
}
}
@Override
public void run() {
while (true){
try{
Thread.sleep(250);
}catch (Exception e){
e.getMessage();
}
time++;//绘图开关
this.repaint();
}
}
}//TipPanel
类似的,修改下就行
Java 画星星
public class A {
public void drow(int n){//n表示高度(打印的行数)
for(int i=n;i0;i--){
for(int j=i-1;j0;j--){//先打印i-1个空格
System.out.print(" ") ;
}
System.out.print("*") ;//打印第一个*
for(int j=2*(n-i)-1;j0;j--){//中间部分的空格
System.out.print(" ") ;
}
if(i!=ni!=1){//除了首尾两行外每行中间的*
System.out.print("*") ;
}
for(int j=2*(i-1)-1;j0;j--){//右面部分的空格
System.out.print(" ") ;
}
System.out.println("*") ;//最后一个*
}
}
public static void main(String[] args){
A a = new A() ;
a.drow(3) ;//这里打印一个三层的
}
}
本文标题:咋用java代码绘制星星 咋用java代码绘制星星图形
分享链接:http://hbruida.cn/article/ddcpees.html