请问一下如何能在java中根据给定的坐标,长宽,画出矩形,这个矩形是由*号或其他符号拼成的。如下面的格式
我用先拼成字符串形式,然后调用drawString()方法,只能打印出一行,无法识别出换行。不知道该用什么方法解决?
[code="java"]
package assignment.task1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Window {
private int numberOfRows;
private int numberOfColumns;
private char borderCharacter;
Frame f = new Frame("Drawing");
Window(int numberOfRows,int numberOfColumns,char borderCharacter){
this.numberOfRows=numberOfRows;
this.numberOfColumns=numberOfColumns;
this.borderCharacter=borderCharacter;
}
public void addShape(Object obj) {
}
public void removeShape(){
}
public void display(){
MyCanvas p = new MyCanvas();
WL wl = new WL();
String str = drawWin().toString();
p.getWin(str);
System.out.println(str);
f.add(p);
f.setSize(600,400);
f.setVisible(true);
f.addWindowListener(wl);
}
public StringBuffer drawWin(){
StringBuffer str=new StringBuffer();
int m =numberOfRows;
int n =numberOfColumns;
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0){
str.append(borderCharacter+" ");
}else if(i>0 && i<m){
if(j==0 || j==n){
str.append(borderCharacter+" ");
}else{
str.append(" ");
}
}else if(i==m){
str.append(borderCharacter+" ");
}
}
str.append("\n");
}
return str;
}
class WL extends WindowAdapter{
public void windowClosing(WindowEvent e){
f.setVisible(false);
}
}
}
class MyCanvas extends Canvas{
String win;
public void getWin(String str){
this.win = str;
}
public void paint(Graphics g){
String s = win;
g.drawString(s,10,20);
}
}
[/code]
[code="java"]
package org.zergle.test.swing;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*/
public class StarRectangle {
private Point location;
private int width;
private int height;
public StarRectangle(Point location, int width, int height) {
this.location = location;
this.width = width;
this.height = height;
}
/**
@param g
/
public void draw(Graphics g) {
char[] top = new char[width];
char[] mid = new char[width];
Arrays.fill(top, '');
Arrays.fill(mid, ' ');
mid[0] = '*';
mid[mid.length - 1] = '*';
Font font = g.getFont();
// 设置字体为等宽字体
g.setFont(new Font("Courier New", Font.PLAIN, 10));
FontMetrics fm = g.getFontMetrics();
int x = location.x;
int y = location.y;
int h = fm.getHeight();
g.drawChars(top, 0, top.length, x, y);
if (this.height >= 2) {
y += h;
for (int i = 0; i < this.height - 2; i++) {
g.drawChars(mid, 0, mid.length, x, y);
y += h;
}
g.drawChars(top, 0, top.length, x, y);
}
g.setFont(font);
}
/**
}
/**
*/
class Canvas extends JFrame {
public Canvas() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setContentPane(new JPanel() {
public void paint(Graphics g) {
new StarRectangle(new Point(130, 100), 50, 5).draw(g);
}
});
this.setSize(600, 400);
this.setVisible(true);
}
}
[/code]
/**
* Draws the text given by the specified string, using this
* graphics context's current font and color. The baseline of the
* leftmost character is at position (<i>x</i>, <i>y</i>) in this
* graphics context's coordinate system.
* @param str the string to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @throws NullPointerException if <code>str</code> is <code>null</code>.
* @see java.awt.Graphics#drawBytes
* @see java.awt.Graphics#drawChars
*/
public abstract void drawString(String str, int x, int y);
drawString("****", 10, 20)
drawString("* *", 10, 40)
y轴要变的