联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2018-06-08 03:40



import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.geom.AffineTransform;

import java.util.ArrayList;


class Scribbling {

      String what;

      int    xpos;

      int    ypos;

      double angle;

      Font   font;

      Color col;

}


class WritePanel extends JPanel {

   private Graphics2D g;

   private ArrayList<Scribbling> text = new ArrayList<Scribbling>();


   private void doWriting(Graphics g) {

       Graphics2D      g2d = (Graphics2D) g;

       Font            font = getFont();

       Color           col = g.getColor();

       double          angle = 0;

       AffineTransform orig = g2d.getTransform();


       for (Scribbling s: text) {

         if (s.angle != angle) {

           g2d.setTransform(orig);

           g2d.rotate(s.angle * Math.PI / 180.0);

           angle = s.angle;

         }

         if (s.font != null && !s.font.equals(font)) {

           g2d.setFont(s.font);

           font = g2d.getFont();

         }

         if (!s.col.equals(col)) {

           col = new Color(s.col.getRGB());

           g2d.setColor(col);

         }

         g2d.drawString(s.what, s.xpos, s.ypos);

       }

   }


   public void addText(String what, int xpos, int ypos,

                       double angle, Font font, Color col) {

      Scribbling s = new Scribbling();

      s.what = what;

      s.xpos = xpos;

      s.ypos = ypos;

      s.angle = angle;

      s.font = font;

      s.col = col;

      text.add(s);

   }


   @Override

   public void paintComponent(Graphics g) {


       super.paintComponent(g);

       doWriting(g);

   }

}


/**

*    Textboard is in fact a full graphical application that displays

*    text in a graphical Window

*/

public class TextBoard extends JFrame {

   private WritePanel board;

   private double     currentAngle;

   private Color      currentColor;

   private Font       currentFont;


   /**

    *    Only constructor.

    *

    *    @param width  Width of the window in pixels

    *    @param height Height of the window in pixels

    *    @param col    Background color

    */

   public TextBoard(int width, int height, Color col) {


       board = new WritePanel();

       // Put the WritePanel into the main window

       add(board);

       // Set its background color

       board.setOpaque(true);

       board.setBackground(col);

       // Initial settings

       currentAngle = 0;

       currentColor = Color.BLACK;

       currentFont = getFont();

       // Geometry of the main window

       setSize(width, height);

       setLocationRelativeTo(null);

       // When users close the window, we quit the application

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setVisible(false);

   }


   // Overloaded methods to write text

   /**

    *  Write text to the screen

    *  <p>

    *  The text can be written at any position, it can be

    *  rotated, the font and color can be changed. Every piece

    *  of text is added to a list, and only written when the

    *  window is drawn.

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    *  @param angle Rotation angle in degrees

    *  @param font  Font to use to write the text

    *  @param col   Text color

    */

   public void write(String what, int xpos, int ypos,

                     double angle, Font font, Color col) {


       if (currentAngle != angle) {

         currentAngle = angle;

       }

       if (currentFont != null && !currentFont.equals(font)) {

         currentFont = font;

       }

       if (!currentColor.equals(col)) {

         currentColor = new Color(col.getRGB());

       }

       board.addText(what, xpos, ypos, angle, font, col);

   }


   /**

    *  Write text to the screen

    *  <p>

    *  Use the last set angle (0 by default), font and color

    *  (black by default).

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    */

   public void write(String what, int xpos, int ypos) {

       write(what, xpos, ypos, currentAngle, currentFont, currentColor);

   }


   /**

    *  Write text to the screen

    *  <p>

    *  Use the last set font and color (black by default), set the angle.

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    *  @param angle Rotation angle in degrees

    */

   public void write(String what, int xpos, int ypos, double angle) {

       write(what, xpos, ypos, angle, currentFont, currentColor);

   }


   /**

    *  Write text to the screen

    *  <p>

    *  Use the last set font, set angle and color.

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    *  @param angle Rotation angle in degrees

    *  @param col   Text color

    */

   public void write(String what, int xpos, int ypos,

                                  double angle, Color col) {

       write(what, xpos, ypos, angle, currentFont, col);

   }


   /**

    *  Write text to the screen

    *  <p>

    *  Use the last set angle(0 by default) and font, set the color.

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    *  @param col   Text color

    */

   public void write(String what, int xpos, int ypos, Color col) {

       write(what, xpos, ypos, currentAngle, currentFont, col);

   }


   /**

    *  Write text to the screen

    *  <p>

    *  Use the last set angle (0 by default), set font and color.

    *

    *  @param what  The text to write

    *  @param xpos  Horizontal position in pixels, from the left hand side

    *  @param ypos  Vertical position in pixels, from the top

    *  @param font  Font to use to write the text

    *  @param col   Text color

    */

   public void write(String what, int xpos, int ypos,

                     Font font, Color col) {

       write(what, xpos, ypos, currentAngle, font, col);

   }


   /**

    *    Show the window

    */

   public void display() {

      this.setVisible(true);

   }

}


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp