多次显示字符串为金字塔

时间:2015-08-13 02:06:51

标签: java

要在新行中多次显示一个单词并将字体大小增加到帧的结尾,输出必须与图像一样 图像是我必须得到的输出 我写的代码是

import java.awt.*;

 class textzoom extends Frame 
 {
     int i=800,j=30,k=8;
    public void paint(Graphics g)
   {
    for(k=8;k<=80;k=k+2)
    {
        Font f1=new Font("Aerial",Font.BOLD,k);
        g.setFont(f1);
        g.drawString("MEGA",i,j);
        //i=i-20;
        j=j+20;
    }
   }
public static void main(String[] args)
{
    textzoom t1=new textzoom(); 
    t1.setSize(1600,900);
    t1.setVisible(true);
    t1.setLocation(40,40);
    t1.setResizable(false);
    t1.setTitle("sample");    
}
}

请知道我必须做些什么改变才能获得输出,如图所示

1 个答案:

答案 0 :(得分:0)

import javax.swing.*;
import java.awt.*;

public class JFontSizes extends JFrame {
   int x = 5;
   int y = 50;
   String homework = "This is the first homework assignment";
   public JFontSizes() {
      super("Increasing Font Sizes");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
   }   
   public void paint(Graphics brush) {
      super.paint(brush);
      // This works sometimes. I am not sure if it is a jGRASP issue or something else.
      // If I resize the frame, the text disappears, and I cannot get the text to start at the top of the frame
      for(int n = 6; n<= 20; ++n) {
         brush.setFont(new Font("Serif", Font.PLAIN, n));
         brush.drawString(homework, x, y);
         y += 15;
      }   
   }              
   public static void main(String[] args) {
      JFontSizes frame = new JFontSizes();
      frame.setSize(400, 500);
      frame.setVisible(true);      
   }   
}

参考: Resizeable Number Pyramid Panel with Swing