用星号输出一个正方形的问题!!

swiminthesea 2003-10-03 11:56:18
我是个JAVA的初学者,有这样一道题,我不知道如何下手,请各位帮帮忙!

write an applet that reads in the size of the a square and displays a hollow(中空) square of that size out of asterisks,by using drawString method inside your applet's paint method.Use an input dialog to read the size from the user.Your pragram should work for squares of all lengths of side between 1 and 20.
...全文
656 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
swiminthesea 2003-10-03
  • 打赏
  • 举报
回复
谢谢啊
swiminthesea 2003-10-03
  • 打赏
  • 举报
回复
楼上的程序好长,我也去试试
happyegg 2003-10-03
  • 打赏
  • 举报
回复
Square.html
<html>
<head>
<title>Square Test (1.1)</title>
</head>
<body>
<h1>Square (1.1)</h1>
<hr>
<applet code=Square.class width=400 height=400>
</applet>
</body>
</html>

Square.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Square extends Applet {
SquareControls controls;
SquareCanvas canvas;

public void init() {
setLayout(new BorderLayout());
canvas = new SquareCanvas();
add("Center", canvas);
add("South", controls = new SquareControls(canvas));
}

public void destroy() {
remove(controls);
remove(canvas);
}

public void start() {
controls.setEnabled(true);
}

public void stop() {
controls.setEnabled(false);
}

public void processEvent(AWTEvent e) {
if (e.getID() == Event.WINDOW_DESTROY) {
System.exit(0);
}
}

public static void main(String args[]) {
Frame f = new Frame("Square");
Square arcTest = new Square();
arcTest.init();
arcTest.start();
f.add("Center", arcTest);
f.setSize(300, 300);
f.show();
}

public String getAppletInfo() {
return "An interactive test of the Graphics.drawArc and \nGraphics.fillArc routines. Can be run \neither as a standalone application by typing 'java Square' \nor as an applet in the AppletViewer.";
}
}
class SquareCanvas extends Canvas {
int startAngle = 0;
int endAngle = 45;
boolean filled = false;
Font font;

public void paint(Graphics g) {
int sx = 100;
int sy = 100;
for(int i = 0;i <this.startAngle; i++) {
g.drawString("*", sx, sy + i*10 );
g.drawString("*", sx + (this.startAngle-1)*10 , sy + i*10 );
g.drawString("*", sx + i*10, sy);
g.drawString("*", sx + i*10 , sy + (this.startAngle-1)*10 );
}
}

public void redraw(boolean filled, int start) {
this.filled = filled;
this.startAngle = start;
repaint();
}
}

class SquareControls extends Panel implements ActionListener {
TextField s;
TextField e;
SquareCanvas canvas;

public SquareControls(SquareCanvas canvas) {
Button b = null;

this.canvas = canvas;
add(s = new TextField("5", 4));
b = new Button("Run");
b.addActionListener(this);
add(b);
}

public void actionPerformed(ActionEvent ev) {
String label = ev.getActionCommand();

canvas.redraw(label.equals("Fill"),Integer.parseInt(s.getText().trim()));
}
}

swiminthesea 2003-10-03
  • 打赏
  • 举报
回复
谢谢一块两毛钱,我去试试先
laziogo 2003-10-03
  • 打赏
  • 举报
回复
老了,我连星号长啥模样都忘了,改成*
laziogo 2003-10-03
  • 打赏
  • 举报
回复
// 菜鸟的事情菜鸟办!:)
import java.awt.Graphics;
import java.applet.Applet;

public class PrintStar extends Applet{


public void paint(Graphics g){


int size = 5; // 暂为这个,接受数据你自己处理
int row, col;
for(row=1; row<=size; row++){
if(row==1){ // 第一行

for(col=1; col<=size; col++){
g.drawString("#", col*10, row*10);
}
}else if(row==size){ // 最后一行
for(col=1; col<=size; col++){
g.drawString("#", col*10, row*10);
}
}else{ // 其他
col=1;
g.drawString("#", col*10, row*10);
for(col=2; col<=size-1; col++){
g.drawString(" ", col*10, row*10);
}
g.drawString("#", col*10, row*10);
}
}
}
}


VVV_lucky 2003-10-03
  • 打赏
  • 举报
回复
有什么难点吗?
helldream2002 2003-10-03
  • 打赏
  • 举报
回复
不好意思,看不懂你的英文,不过用星号输正方形很容易的,用for循环就可以了!
swiminthesea 2003-10-03
  • 打赏
  • 举报
回复
没人肯帮忙............................

62,628

社区成员

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

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