java小程序师哥师姐帮忙,谢谢.

Dream_1986 2007-10-22 04:23:18
画五角星,
用JAVA写的代码来完成,不知道怎么编写.
...全文
223 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hivvyeah 2007-10-25
  • 打赏
  • 举报
回复
就是 找个假想圆, 把它分五份. 五个分点转成笛卡尔坐标, 然后把五个点连起来.

我是用极坐标转的
Dream_1986 2007-10-25
  • 打赏
  • 举报
回复
不过觉得有点复杂,自己也编了一个就是的出的五角星不规范....
Dream_1986 2007-10-25
  • 打赏
  • 举报
回复
在EditPlus中运行,注释顶上面的
//package pentacle2;
就OK了
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复
是在netbeans里面做的.
运行过的, 没问题.
里面有netbeans生成的代码.

如果是其它的ide, 重写下 public void pain(Graphics g);
还有复制下那个class PolarCoordinate
沉沦 2007-10-24
  • 打赏
  • 举报
回复
..........?????是对的么??...1.6上面..怎么显示一大堆错误啊。.
Dream_1986 2007-10-24
  • 打赏
  • 举报
回复
第一次给分,给几分呢???
呵呵,想想...
Dream_1986 2007-10-24
  • 打赏
  • 举报
回复
呵呵,记得记得,谢了....
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复
能用记得结贴,给分哦
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复


/*
* 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;
}



}
zbl0201 2007-10-22
  • 打赏
  • 举报
回复
不好意思 发错了!!
zbl0201 2007-10-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下试试

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧