画五角星,
用JAVA写的代码来完成,不知道怎么编写.
用JAVA写的代码来完成,不知道怎么编写.
解决方案 »
- 帮忙改几行程序
- TfIdf算法实现 in java
- 如何用Java实现void swap(int a,int b)?
- 这种格式txt文件怎么弄成excel文件,求救
- Java i++ 和c++ i++ 的区别
- 怎么将JTextField设置成只读
- RedHat liunx 上的JAVA环境如何配呢!火急,在线等,200分,
- 怎么把double转化为java.lang.Double?!!
- 再贴WebLogic Server 和 Weblogic Integration 有何不同,各是用来做什么的?
- 哪位高手能给介绍一点关于Java初级方面的书籍,由于是新手只能给22分望大家谅解!!
- 循环内变量反问权限的问题
- 关于散列码
import java.util.*;
public class Scan {
public static void main(String[] args) {
Scanner scann = new Scanner(System.in);
int i = scann.nextInt();
System.out.print(i);
} } 在1.6下没问题
你在1.4下试试
* MainFrame.java
*
* Created on 2007年10月24日, 下午5:04
*/package pentacle2;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.math.*;/**
*
* @author qhj
*/
public class MainFrame extends javax.swing.JFrame {
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
// 变量声明结束 public void paint(Graphics g) {
super.paint(g);
int rectHeight,y ;
int rectWidth, x ;
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Rectangle rectWindow = getContentPane().getBounds(); //窗口的大小
Point2D.Double origin = new Point2D.Double(rectWindow.x+rectWindow.width / 2f,
rectWindow.y+rectWindow.height / 2f);//五角星的原点
PolarCoordinate plc = new PolarCoordinate(origin);//新建个极座标系
double rad = (rectWindow.x < rectWindow.y)?(rectWindow.width * 0.6)/2.0:(rectWindow.height * 0.6)/2.0; //五角星的外接圆半径
Point2D.Double points[] = new Point2D.Double[5];
double angle = 0;
for(int i = 1 ; i <=5; i++ )
{
points[i-1] = (Point2D.Double) plc.toCartesianWithAngle(rad, angle);
angle += 360.0/5;
}
GeneralPath pentacle = new GeneralPath(GeneralPath.WIND_NON_ZERO,5);
pentacle.moveTo(points[0].getX(), points[0].getY());
pentacle.lineTo(points[3].getX(), points[3].getY());
pentacle.lineTo(points[1].getX(), points[1].getY());
pentacle.lineTo(points[4].getX(), points[4].getY());
pentacle.lineTo(points[2].getX(), points[2].getY());
pentacle.closePath();
g2.setStroke(new BasicStroke(2.0f));
x = (int)pentacle.getBounds().getX();
y = (int)pentacle.getBounds().getX();
rectWidth = (int)pentacle.getBounds().getWidth();
GradientPaint redtowhite = new GradientPaint(x,y,Color.RED,x+rectWidth-5, y,Color.white);
g2.setPaint(redtowhite);
g2.fill(pentacle);
}
}class PolarCoordinate
{
private Point2D.Double origin = new Point2D.Double();
public PolarCoordinate(Point2D.Double origin)
{
this.origin.setLocation(origin);
}
public PolarCoordinate(double x, double y)
{
origin.setLocation(x, y);
}
public Point2D toCartesianWithAngle(double radius, double angleInAngle)
{
Point2D.Double result = new Point2D.Double();
return toCartesianWithRadians(radius, angleInAngle*Math.PI/180);
}
public Point2D toCartesianWithRadians(double radius, double angleInRadians)
{
Point2D.Double result = new Point2D.Double();
result.setLocation(origin.getX()+Math.cos(angleInRadians)*radius, origin.getY()+Math.sin(angleInRadians)*radius);
return result;
}
}
运行过的, 没问题.
里面有netbeans生成的代码.如果是其它的ide, 重写下 public void pain(Graphics g);
还有复制下那个class PolarCoordinate
//package pentacle2;
就OK了