Select Page

Big Java学习笔记——图形界面Graphical Applications

Big Java学习笔记——图形界面Graphical Applications

[danger]态度声明:本站所有原创学习类文章,只为分享学习乐趣、拓展知识广度、提供讨论平台,不以应试,实用、探讨答案为目的,本站中所有文章中的引用会要求作者尽量标出出处,所有提供下载文件均为个人整理分享学习资料,如果有版权异议,请与博主沟通。本站每学习文必置顶此项,望大家共同监督。乐于分享,支持版权,转载请列明出处,想了解更多……[/danger]


[title]框架Frame Windows[/title]

要显示一个框架,需要以下几步:

  1. 首先,构建一个JFrame的对象JFrame frame = new JFrame();
  2. 设置大小frame.setSize(300, 400);
  3. 设置Frame的题目frame.setTitle(“name”);
  4. 设置默认关闭设置frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. 设置可见性frame.setVisible(true);
    import javax.swing.JFrame;
    
    public class EmptyFrameViewer
    {
      public static void main(String[] args)
      {
        JFrame frame = new JFrame();
        frame.setSize(300,400);
        frame.setTitle("firstframe");
        frame.setDefaultCloseOperation(JFrame.EXID_ON_CLOSE);
        frame.setVisible(true);
      }
    }

[title]画一个元素[/title]

要在一个框架内画画,需要从JComponent类中拓展一个类。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import Javax.swing.JComponent;

/**
   A component that draws two rectangles.
*/
public class RectangleComponent extends JComponent
{
   Public void paintComponent(Graphics g)
   {
      // Recover Graphics2D
      Graphics2D g2 = (Graphics2D) g;
      
      // Construct a rectangle and draw it
      // 是通过box这个对象,然后draw相当于描边
      Rectangle box = new Rectangle(5,10,20,30);
      g2.draw(box);
      
      // Move rectangle 15 units to the right and 25 units down
      box.translate(15,25);
      
      // Draw moved rectangle
      g2.draw(box);
    }
}

[title]在框架内显示元素[/title]

  1. 构建一个框架对象并且设置;
  2. 构建一个显示元素类的对象;
  3. 在框架中添加一个元素;
  4. 设置框架可见。
import javax.swing.JFrame;

public class RectangleViewer
{
   public static void main(String args)
   {
      // construct a frame
      JFrame frame = new JFrame();
      
      // configure the frame
      frame.setSize(300,400);
      frame.setTitle(“Two rectangle”);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      // construct a new component
      RectangleComponent component = new RectangleComponent();
      
      //add a component to the frame
      frame.add(component);
      
      //set the visible of the frame, normally set it at last
      frame.setVisible(true);     
    }
}

[title]椭圆和圆形[/title]

构建椭圆:Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height);

构建圆形:Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);

g2.draw(circle);

[title]线条[/title]

/**
     Drawing a line hat two ways
*/

//first way, specify two ends of a line together
Line2D.Double segment = new Line2D.Double( x1, y1, x2, y2);

//second way, specify each end of point as an object
Point2D.Double from = new Point2D.Double(x1, y1);
Point2D.Double to = new Point2D.Double(x2, y2);

Line2D.Double segment = new Line2D.Double(from, to);

[title]文本[/title]

drawString 函数从左起的basepoint画一个文本。

g2.drawString(“Message”, 50, 100);

[title]色彩[/title]

Color magenta = new Color(255, 0, 255);//紫色的

//画一个其他默认颜色描边的圆形

g2.setColor(Color.RED);

g2.draw(circle);

//填充一个圆形

g2.fill(circle);

接下来我们给出一个完整的程序,画一个绿眼睛红嘴的人脸:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;

/**
     A component that draws a face with green eyes and red mouth
*/
public class FaceComponent extends JComponent
{
    public void paintComponent( Graphics g)
        {
            // Recover Graphics2D
            Graphics2D g2 = (Graphics2D) g;

            // Draw the head
            Ellipse2D.Double head = new Ellipse2D.Double( 5, 10, 100, 150);
            g2.draw(head);

            // Draw the eyes
            g2.setColor(Color.GREEN);
            Rectangle eye = new Rectangle(25, 70, 15, 15);//draw links eye
            g2.fill(eye);
            eye.translate(50, 0);//eye object move
            g2.fill(eye);//draw right eye

            // Draw the mouth
            Line2D.Double mouth = new Line2D.Double(30, 110, 80, 110);
            g2.setColor(Color.RED);
            g2.draw(mouth);

            // Draw the greeting
            g2.setColor(Color.BLUE);
            g2.drawString(“Hello, World!”, 5, 175);
     }
}

接下来是测试类:

import javax.swing.JFrame;

public class Faceviewer
{
    public static void main( String  args)
    {
         JFrame frame = new JFrame();
         frame.setSize(150, 250);
         frame.setTitle(“An Alien Face”;
         frame.setDefaultCloseOperation(JFrame.EXID_ON_CLOSE);

         FaceComponent component = new FaceComponent();
         frame.add(component);

         frame.setVisible(true);
    }
}

[title]形状类[/title]

对于任何可能出现超过一次的图形最好新建一个自己的类。

对于比较复杂的形状,我们最好能够分成每一部分单独建类。每个类应该有自己的draw函数来画这个图形,还应还有构建器来设置每个形状的位置。

要画一个比较复杂的画面,最好在一个空白纸上手绘草稿,确保知道每个点的坐标和线长度。

例如,我们以画一个简笔画小汽车——两个圆轮子,一个长方形车身和一个梯形顶棚,我们的思考过程如下:

  1. 首先手绘上述小汽车,确定坐标,注意,要和平时区分开,和编程一样,用左上角坐标系;
  2. 按照之前的文章所提到的思路,我们确定需要新建一个car类,为了讨论两种方法的便捷性,我们在其中新建两个对象,每个对象画一个小车;新建一个CarComponent类进行绘图;新建一个CarViewer类显示包含了CarComponent的框架;
  3. 然后确定函数,函数很简单就是各个图形的形成函数;
  4. 确定参数,我们用两种方法需要设定参数,也就是坐标原点作为explicit parameter,每个函数的坐标点用local variables;

首先我们先对构建器进行编程:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponet;

/**
   This Component draws two cars in two way
*/
public class CarComponet extends JComponet
{
   public void paintComponet(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      
      //the first car use 0,0 as coordinate
      Car car1 = new Car(0,0);
      
      //in order to make the second way, we need to compute the coordinates 
      int x = getWidth() - 60;//60 is the width that we drew on the paper
      int y = getHeight() - 30;
      
      //the second car use the flexible coordinate
      Car car2 = new Car(x,y);
      
      car1.draw(g2);
      car2.draw(g2);
  }
}

然后完善整个画车的对象体:

import java.awt.Graphics2D;
import java.awt.rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom:Point2D;

/**
   A car shape that can be positioned anywhere on the screen.
*/
public class Car
{
   private int xLeft;
   private int yTop;
   
   /**
      Constructs a car with a given top left corner.
      @param x the x-coordinate of the top-left corner
      @param y the y-coordinate of the top-left corner
   */
  public Car( int x, int y)
  {
     xLeft = x;
     yTop = y;
  }
  
  /**
     Draws the car
     @param g2 the graphics context
  */
  public void draw(Graphics2D g2)
  {
     //the car body in the Mitte
     Rectangle body = new Rectangle(xLeft, yTop + 10,60,10);
     Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
     Ellipse2D.Double backTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
  
     // We draw the Points, because we need to line up miteinander, is easier way
     // The bottom of the front windshield
     Point2D.Double r1 = new Point2D.Double(xLeft + 10; yTop + 10);
     // The point front of the roof
     Point2D.Double r2 = new Point2D.Double(xLeft + 20; yTop);
     // The point rear of the roof
     Point2D.Double r3 = new Point2D.Double(xLeft + 40; yTop);
     // The bottom of the back windshield
     Point2D.Double r4 = new Point2D.Double(xLeft + 50; yTop + 10);
  
     // Now we need to line the points up
     Line2D.Double frontWindowShield = new Line2D.Double(r1, r2);
     Line2D.Double topRoof = new Line2D.Double(r2, r3);
     Line2D.Double backWindowShield = new Line2D.Double(r3, r4);
  
     // Let’s happy draw together
     g2.draw(body);
     g2.draw(frontTire);
     g2.draw(backTire);
     g2.draw(frontWindowShield);
     g2.draw(topRoof);
     g2.draw(backWindowShield);
   }
}

最后测试输出类:

import javax.swing.JFrame

public class CarViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      
      frame.setSize(300, 400);
      frame.setTitle(“Two Cars”)
      frame.setDefaultCloseOperation(JFrame.EXID_ON_CLOSE);
      
      CarComponent component = new Carcomponent();
      frame.add(component);#
      
      frame.setVisible(true);
   }
}

思路,思路,思路,重要的事情说三遍。

 

About The Author

乐观的无头苍蝇

无论什么情况下,每天都要对自己说:“Guten Morgen!”