⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sketchview.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import javax.swing.*;
import java.util.*;                  // For Observer
import java.awt.*;                   // For Graphics
import java.awt.geom.*;

class SketchView extends JComponent
                 implements Observer
{
  public void paint(Graphics g)
  {
    // Temporary code
    Graphics2D g2D = (Graphics2D)g;                 // Get a Java 2D device context

    g2D.setPaint(Color.red);                        // Draw in red

    // Position width and height of first rectangle
    Point2D.Float p1 = new Point2D.Float(50.0f, 10.0f);
    float width1 = 60;
    float height1 = 80;

    // Create and draw the first rectangle
    Rectangle2D.Float rect = new Rectangle2D.Float(p1.x, p1.y, width1, height1);
    g2D.draw(rect);

    // Position width and height of second rectangle
    Point2D.Float p2 = new Point2D.Float(150.0f, 100.0f);
    float width2 = width1 + 30;
    float height2 = height1 + 40;

    // Create and draw the second rectangle
    g2D.draw(new Rectangle2D.Float(
                       (float)(p2.getX()), (float)(p2.getY()), width2, height2));

    g2D.setPaint(Color.blue);                        // Draw in blue

    // Draw lines to join corresponding corners of the rectangles
    Line2D.Float line = new Line2D.Float(p1,p2);
    g2D.draw(line);

    p1.setLocation(p1.x + width1, p1.y);
    p2.setLocation(p2.x + width2, p2.y);
    g2D.draw(new Line2D.Float(p1,p2));

    p1.setLocation(p1.x, p1.y + height1);
    p2.setLocation(p2.x, p2.y + height2);
    g2D.draw(new Line2D.Float(p1,p2));

    p1.setLocation(p1.x - width1, p1.y);
    p2.setLocation(p2.x - width2, p2.y);
    g2D.draw(new Line2D.Float(p1, p2));

    p1.setLocation(p1.x, p1.y - height1);
    p2.setLocation(p2.x, p2.y - height2);
    g2D.draw(new Line2D.Float(p1, p2));
    
    g2D.drawString("Lines and rectangles", 60, 250); // Draw some text
  }

  public SketchView(Sketcher theApp)
  {
    this.theApp = theApp;
  }

  // Method called by Observable object when it changes
  public void update(Observable o, Object rectangle)
  {
    // Code to respond to changes in the model...
  }

  private Sketcher theApp;           // The application object
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -