polylinetest.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 78 行

JAVA
78
字号
/*
 * $Id: PolyLineTest.java,v 1.3 2004/02/26 21:17:11 lsantha Exp $
 */
package org.jnode.test.gui;

import org.jnode.awt.geom.PolyLine;
import org.jnode.driver.video.util.Curves;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class PolyLineTest {

    public static void main(String[] args) throws Exception {
        Frame wnd = new Frame();
        try {

            //final double[] linePoints = { 200, 50, 200, 100, 100, 100, 100, 75, 200, 50 };
            //final double[] linePoints = { 200, 50, 200, 100, 100, 150, 100, 100, 200, 50 };
            final double[] linePoints = new double[42];
            Curves.calculateQuadCurve(50, 200, 100, 100, 150, 300, linePoints);

            wnd.setSize(600, 400);
            wnd.add(new TestComponent(linePoints));
            wnd.show();

            System.in.read();
            //Thread.sleep(5000);

            wnd.hide();
        } finally {
            wnd.dispose();
        }
    }

    static class TestComponent extends Component {

        private final double[] linePoints;

        public TestComponent(double[] linePoints) {
            super();
            this.linePoints = linePoints;
        }

        public void paint(Graphics g) {
            System.out.println("Paint called");
            paintComponent(g);
        }

        private void paintComponent(Graphics g) {

            g.setColor(Color.GREEN);
            final Rectangle2D bounds = PolyLine.getBounds(linePoints);
            for (int row = 0; row < bounds.getHeight(); row++) {
                for (int col = 0; col < bounds.getWidth(); col++) {
                    final int x = (int) bounds.getX() + col;
                    final int y = (int) bounds.getY() + row;
                    if (PolyLine.contains(linePoints, x, y)) {
                        g.drawLine(x, y, x, y);
                    }
                }
            }

            g.setColor(Color.RED);
            final int cnt = linePoints.length;
            for (int i = 0; i < cnt - 2; i += 2) {
                g.drawLine((int) linePoints[i], (int) linePoints[i + 1], (int) linePoints[i + 2], (int) linePoints[i + 3]);
            }
        }
    }
}

⌨️ 快捷键说明

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