可调整角度

发布于 2024-11-15 04:27:06 字数 1048 浏览 3 评论 0原文

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

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);
    }
}

I have an angle in a frame (I draw it with two lines). I want to make it flexible to frame; I mean, when the user expands the frame angle also become expanded and vice versa. I tried a lot of examples but I could not solve it. Can someone help?

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-11-22 04:27:06

这里是在 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;
    }
}

Here it is done in Swing. Everything is done in the EDT, as is intended with Swing as it is not thread safe. It is double buffered. Why the JLabel/Icon combination? It's just the best way to do it, as far as I have discovered, and I'd struggle to give you a historical/technical explanation of why - that's just the way it seems to have been designed. The other approach is to get involved with BufferStrategy but that starts to get more complicated IMHO.

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;
    }
}
稚气少女 2024-11-22 04:27:06

如果坚持使用 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);
    }
}

If sticking with AWT, I would use a ComponentListener to track the size changes for the Frame and reset the line coordinates accordingly.

You may get away with creating/updating the lines in the Frame.paint() context, but that's just not a very clean implementation, with a lot of implied logic and assumptions and, therefore, probably some issues.

So here's the ComponentListener approach. I had to make a few assumptions about where you wanted your lines to get drawn from/to, as you were not clear on this. (If you can be clearer on this, I can update the example.)

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文