可调整角度

时间:2011-06-14 16:29:49

标签: java awt graphics2d

我在一个框架中有一个角度(我用两条线绘制)。我想让它灵活到框架;我的意思是,当用户扩展框架角度时也会扩展,反之亦然。我尝试了很多例子,但我无法解决它。有人可以帮忙吗?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class LineDraw extends Frame {
    Line2D line1 = new Line2D.Double(200, 200, 100, 300);
    Stroke drawingStroke1 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
            new float[] { 9 }, 0);
    Line2D line2 = new Line2D.Double(200, 200, 200, 300);

    public void paint(Graphics g) {
        Graphics2D graph = (Graphics2D) g;
        graph.setPaint(Color.red);
        graph.draw(line2);
        graph.setStroke(drawingStroke1);
        graph.setPaint(Color.green);
        graph.draw(line1);

    }

    public static void main(String args[]) {
        Frame frame = new LineDraw();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.setSize(300, 250);
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:1)

如果坚持使用AWT,我会使用ComponentListener来跟踪Frame的大小更改并相应地重置线坐标。

您可能会在Frame.paint()上下文中创建/更新行,但这不是一个非常干净的实现,有很多隐含的逻辑和假设,因此可能存在一些问题。

所以这是ComponentListener方法。我不得不做一些关于你希望你的线从哪里得到的假设,因为你不清楚这一点。 (如果您对此更清楚,我可以更新示例。)

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class LineDraw extends Canvas implements ComponentListener {
    Line2D line1, line2;

    public LineDraw() {
        this.addComponentListener(this);
    }

    // set up lines every size update
    public void componentResized(ComponentEvent e) {
        Dimension size = getSize();
        line1 = new Line2D.Double(size.width/2, size.height-10, 10, 10);
        line2 = new Line2D.Double(size.width/2, size.height-10, size.width-10, 10);
    }

    // required to satisfy ComponentListener interface
    public void componentHidden(ComponentEvent e) { }
    public void componentMoved(ComponentEvent e) { }
    public void componentShown(ComponentEvent e) { }

    // paint, main both as before

    Stroke drawingStroke1 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
            new float[] { 9 }, 0);

    public void paint(Graphics g) {
        Graphics2D graph = (Graphics2D) g;
        graph.setPaint(Color.red);
        graph.draw(line2);
        graph.setStroke(drawingStroke1);
        graph.setPaint(Color.green);
        graph.draw(line1);
    }

    public static void main(String args[]) {
        Frame frame = new Frame();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.add(new LineDraw());
        frame.validate();
        frame.setSize(300, 250);
        frame.setVisible(true);
    }
}

答案 1 :(得分:1)

这是在Swing中完成的。一切都在EDT中完成,就像Swing一样,因为它不是线程安全的。它是双缓冲的。为什么JLabel / Icon组合?就我所发现而言,这只是最好的方法,我很难给你一个历史/技术解释原因 - 这就是它的设计方式。另一种方法是参与BufferStrategy,但开始变得更加复杂恕我直言。

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

public class LineDrawSwing extends JLabel implements Icon {
    Line2D line1, line2;

    public LineDrawSwing() { this.setIcon(this); }

    Stroke drawingStroke1 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
            new float[] { 9 }, 0);

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.add(new LineDrawSwing());
        frame.validate();
        frame.setSize(300, 250);
        frame.setVisible(true);
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Dimension size = getSize();
        line1 = new Line2D.Double(size.width/2, size.height-10, 10, 10);
        line2 = new Line2D.Double(size.width/2, size.height-10, size.width-10, 10);
        Graphics2D graph = (Graphics2D) g;
        graph.setPaint(Color.red);
        graph.draw(line2);
        graph.setStroke(drawingStroke1);
        graph.setPaint(Color.green);
        graph.draw(line1);
    }

    @Override
    public int getIconHeight() {
        return getSize().height;
    }

    @Override
    public int getIconWidth() {
        return getSize().width;
    }
}