开发者

resizable angle

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, 2开发者_StackOverflow社区00, 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);
    }
}


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜