3

im wondering of anyone can help me use the rotate method, i looked through previous questions and didnt find what i was looking for..

Code has been compacted so someelements might be missing.. screenshot below.

im currently adding and subtracting 10 from the line variables to rotate can anyone help me use the actual rotate method,,

Thanks in Advance..

public class Display extends JFrame{

private int Lx1 = CANVAS_WIDTH / 2;         private int Mx1 = CANVAS_WIDTH / 2;
private int Ly1 = CANVAS_HEIGHT / 2;        private int My1 = CANVAS_HEIGHT / 2;
private int Lx2 = CANVAS_WIDTH / 2;         private int Mx2 = CANVAS_WIDTH / 2;
private int Ly2 = CANVAS_HEIGHT / 2;        private int My2 = CANVAS_HEIGHT;



public Display() {
    canvas = new DrawCanvas();
    canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
    canvas.setLayout(new GridLayout(8, 4, 80, 10));
    canvas.setComponentOrientation(ComponentOrientation.UNKNOWN);
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());

Altitude = new JTextField("Altitude", 5);               Altitude.setHorizontalAlignment(JTextField.CENTER);
Altitude.setEditable(false);                            Altitude.setOpaque(false);
Altitude.setFont(Altitude.getFont().deriveFont(25f));           Altitude.setForeground(Color.WHITE);

TASpeed = new JTextField("TAs", 5);                         TASpeed.setHorizontalAlignment(JTextField.CENTER);
TASpeed.setEditable(false);                                 TASpeed.setOpaque(false);
TASpeed.setFont(TASpeed.getFont().deriveFont(25f));             TASpeed.setForeground(Color.WHITE);

TargetAlt = new JTextField("Taralt", 5);                    TargetAlt.setHorizontalAlignment(JTextField.CENTER);
TargetAlt.setEditable(false);                               TargetAlt.setOpaque(false);
TargetAlt.setFont(TargetAlt.getFont().deriveFont(25f));         TargetAlt.setForeground(Color.WHITE);

TargetSpeed = new JTextField("TArsp", 5);                   TargetSpeed.setHorizontalAlignment(JTextField.CENTER);
TargetSpeed.setEditable(false);                             TargetSpeed.setOpaque(false);
TargetSpeed.setFont(TASpeed.getFont().deriveFont(25f));         TargetSpeed.setForeground(Color.WHITE);

BaroCorrectUp =  new JTextField("BaroCorU", 5);             BaroCorrectUp.setHorizontalAlignment(JTextField.CENTER);
BaroCorrectUp.setEditable(false);                           BaroCorrectUp.setOpaque(false);
BaroCorrectUp.setFont(BaroCorrectUp.getFont().deriveFont(25f));     BaroCorrectUp.setForeground(Color.WHITE);

BaroCorrectDown =  new JTextField("BaroCorD", 5);           BaroCorrectDown.setHorizontalAlignment(JTextField.CENTER);
BaroCorrectDown.setEditable(false);                         BaroCorrectDown.setOpaque(false);
BaroCorrectDown.setFont(BaroCorrectUp.getFont().deriveFont(25f));   BaroCorrectDown.setForeground(Color.WHITE);

WindDir = new JTextField("Wind dir", 5);                    WindDir.setHorizontalAlignment(JTextField.CENTER);
WindDir.setEditable(false);                                 WindDir.setOpaque(false);
WindDir.setFont(WindDir.getFont().deriveFont(25f));             WindDir.setForeground(Color.WHITE);

EquivSpeed = new JTextField("EquivSpe", 5);             EquivSpeed.setHorizontalAlignment(JTextField.CENTER);



addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            switch(evt.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                Rx1 -= 10;      Ry1 += 10;          Rx2 += 10;      Ry2 -= 10;
                x1 -=10;        x2 +=10;            Mx1 += 10;      Mx2 -= 10;      

                repaint();
                break;
            case KeyEvent.VK_RIGHT:
                Rx1 -= 10;      Ry1 -= 10;          Rx2 += 10;      Ry2 += 10;
                x1 += 10;       x2 += 10;           Mx1 -= 10;      Mx2 += 10;      


                repaint();
                break;
            case KeyEvent.VK_DOWN:
                y1 -= 10;       y2 -= 10;           Ly1 -= 10;      Ly2 -= 10;
                Sy1 += 10;      Sy2 -= 10;          Ry1 +=10;       Ry2 += 10;
                repaint();
                break;
            case KeyEvent.VK_UP:
                y1 += 10;       y2 += 10;           Ly1 += 10;      Ly2 += 10;
                Sy1 -= 10;      Sy2 += 10;          Ry1 -= 10;      Ry2 -= 10;
                repaint();
                break;
            case KeyEvent.VK_M:
                System.exit(0);
                break;
            case KeyEvent.VK_4:
                countAd++;
                BaroCorrectDown.setText(countAd + "");
                repaint();
                break;
            case KeyEvent.VK_3:
                countAu++;
                BaroCorrectUp.setText(countAu + "");
                repaint();
                break;
            }
        }
    });
    cp.add(canvas);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(CANVAS_WIDTH / 3, CANVAS_HEIGHT / 4, CANVAS_WIDTH, CANVAS_HEIGHT);
    setTitle("FLIGHT DISPLAY");                         pack();
    setVisible(true);                                   requestFocus();
}

class DrawCanvas extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        setBackground(CANVAS_BACKGROUND);
        g.setColor(GROUND_COLOR);
        //Draw ground Color
        g.drawRect(Sx1 - Sx1,Sy1 /2, CANVAS_WIDTH, Sy2 /2);
        g.fillRect(Sx1 - Sx1, Sy1 /2, CANVAS_WIDTH, Sy2 /2);

        g.setColor(LINE_COLOR);
        //Draw line centre horizontal
        g.drawLine(Rx1, Ry1 /2, Rx2, Ry2 /2);

        g.drawOval(x1 -15, y1 -15, 30, 30);


        //Draw line dim

        g.setColor(Color.YELLOW);
        g.fillArc(CANVAS_WIDTH /2 -150, 10, 300, 170, 30, 120);
        g.setColor(LINE_COLOR);

        //Draw polyline centre plane
        g.setColor(Color.YELLOW);
        g.drawPolyline(xs, ys, 10);
        g.fillPolygon(new Polygon(
                 new int[]{470, 350, 230, 350, 470},
                 new int[]{260, 205, 260, 225, 260},5));

        g.setColor(Color.CYAN);
        g.setColor(Color.BLACK);
        g.drawArc(CANVAS_WIDTH / 2 -150, 10, 300, 170,  30, 120);
        g.drawArc(CANVAS_WIDTH / 2 -150, 11, 300, 170,  30, 120);
        g.setColor(CANVAS_BACKGROUND);
        g.fillRect(0, 60, CANVAS_WIDTH, 50);
        Color DarkTRANS = new Color(0.0f, 0.0f, 0.0f, 1.0f);
        g.setColor(DarkTRANS);
        g.fillRect(0, 400, CANVAS_WIDTH, 50);
        g.fillRect(0, 345, 115, 55);
        g.fillRect(585, 345, 115, 55);
        g.setColor(LINE_COLOR);
        g.drawLine(Lx1 -50, Ly1 -120, Lx2 +50, Ly2 -120);
        g.drawLine(Lx1 -25, Ly1 -140, Lx2 +25, Ly2 -140);
        g.drawLine(Lx1 -75, Ly1 -160, Lx2 +75, Ly2 -160);
        g.drawLine(Lx1 -25, Ly1 +20, Lx2 +25, Ly2 +20);
        g.drawLine(Lx1 -50, Ly1 +40, Lx2 +50, Ly2 +40);
        g.drawLine(Lx1 -25, Ly1 +60, Lx2 +25, Ly2 +60);
        g.drawLine(Lx1 -75, Ly1 +80, Lx2 +75, Ly2 +80);
        g.drawLine(Lx1 -25, Ly1 +100, Lx2 +25, Ly2 +100);
        g.drawLine(Lx1 -50, Ly1 +120, Lx2 +50, Ly2 +120);
        //Draw line dim
        g.drawLine(Lx1 -25, Ly1 -20, Lx2 +25, Ly2 -20);
        g.drawLine(Lx1 -50, Ly1 -40, Lx2 +50, Ly2 -40);
        g.drawLine(Lx1 -25, Ly1 -60, Lx2 +25, Ly2 -60);
        g.drawLine(Lx1 -75, Ly1 -80, Lx2 +75, Ly2 -80);
        g.drawLine(Lx1 -25, Ly1 -100, Lx2 +25, Ly2 -100);

        //bottom triangles
        g.setColor(DarkTRANS);
        g.fillPolygon(new Polygon(
                 new int[]{115, 115, 200},
                 new int[]{400, 345, 400},3));
        g.fillPolygon(new Polygon(
                 new int[]{585, 585, 500},
                 new int[]{400, 345, 400},3));
        //top button triangle/shapes
        g.fillPolygon(new Polygon(
                new int[]{0, 200, 115, 0,},
                new int[]{0, 0, 50, 50},4));
        g.fillPolygon(new Polygon(
                new int[]{500, 700, 700, 585,},
                new int[]{0, 0, 50, 50},4));
        //middle button shapes
        g.fillPolygon(new Polygon(
                new int[]{0, 100, 100, 117, 117, 130, 117, 117, 100, 100, 0},
                new int[]{174, 174, 159, 159, 187, 197, 207, 235, 235, 220, 220},11));
                //right hand shape
        g.fillPolygon(new Polygon(
                new int[]{700, 600, 600, 583, 583, 570, 583, 583, 600, 600, 700},
                new int[]{174, 174, 159, 159, 187, 197, 207, 235, 235, 220, 220},11));
        Color TRANS = new Color(0.0f, 0.0f, 0.0f, 0.35f);
        g.setColor(TRANS);
        g.fillRect(0, 0, 115, CANVAS_HEIGHT);
        g.fillRect(585, 0, 115, CANVAS_HEIGHT);

        g.setColor(LINE_COLOR);
        g.drawLine(Mx1+20, My1 + My1, Mx2, My2-440);
        g.drawLine(Mx1-20, My1 + My1, Mx2, My2-440);
        g.drawLine(Mx1, My1 + My1, Mx2, My2-440);

        g.drawString("10", Lx1 -90, Ly1 -75);
        g.drawString("10", Lx1 +77, Ly1 -75);
        g.drawString("10", Lx1 -90, Ly1 +85);
        g.drawString("10", Lx1 +77, Ly1 +85);
        g.drawString("20", Lx1 +77, Ly1 -155);
        g.drawString("20", Lx1 -90, Ly1 -155);


        g.drawString("Press alt + 3(U), 4(D)\n" , 582, 305);
        g.drawString("To Initialize" , 595, 315);
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Display();
        }
    });
}

} enter image description here

Delish
  • 77
  • 1
  • 12
  • 3
    And for god sake, please follow Java naming conventions (variables and methods start with a lower-case letter and only a single instruction per line). Your code is a nightmare to read for others. – Guillaume Polet Feb 21 '13 at 17:50
  • 2
    See this [Line Transformation](http://www.java2s.com/Code/Java/2D-Graphics-GUI/Linetransformationrotationshearscale.htm) example. – David Kroukamp Feb 21 '13 at 18:28
  • 1
    [`LinePanel`](http://stackoverflow.com/a/5797965/230513) shows how to _translate_ a line using key bindings; it looks like you want to _translate_ just one end. – trashgod Feb 21 '13 at 18:32
  • thaat line transformation example was brilliant except for one thing it moves everythin on the screen when i only what to rotate a line – Delish Feb 22 '13 at 10:26
  • 1
    @BenJi: I've elaborated below. Don't forget the `@` address in comments; I almost missed this. :-) – trashgod Feb 22 '13 at 15:38
  • @trashgod thanks man glad you didnt miss it cus your last comment solved my problem for me as i think this will to thanks!!!! – Delish Feb 22 '13 at 15:48

1 Answers1

4

This variation of LinePanel may suggest an approach.

image

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * @see https://stackoverflow.com/a/15027637/230513
 * @see https://stackoverflow.com/questions/6991648
 * @see https://stackoverflow.com/questions/6887296
 * @see https://stackoverflow.com/a/5797965/230513
 */
public class LinePanel extends JPanel {

    private Point p1 = new Point(320, 100);
    private Point p2 = new Point(320, 380);
    private boolean drawing;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(640, 480);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.blue);
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(8,
            BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
        g.drawLine(p1.x, p1.y, p2.x, p2.y);
    }

    private class ControlPanel extends JPanel {

        private static final int DELTA = 10;

        public ControlPanel() {
            this.add(new MoveButton("\u21BA", KeyEvent.VK_LEFT, DELTA, 0));
            this.add(new MoveButton("\u21BB", KeyEvent.VK_RIGHT, -DELTA, 0));
        }

        private class MoveButton extends JButton {

            KeyStroke k;
            int dx, dy;

            public MoveButton(String name, int code, final int dx, final int dy) {
                super(name);
                this.k = KeyStroke.getKeyStroke(code, 0);
                this.dx = dx;
                this.dy = dy;
                this.setAction(new AbstractAction(this.getText()) {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        LinePanel.this.p1.translate(-dx, 0);
                        LinePanel.this.p2.translate(dx, 0);
                        LinePanel.this.repaint();
                    }
                });
                ControlPanel.this.getInputMap(
                    WHEN_IN_FOCUSED_WINDOW).put(k, k.toString());
                ControlPanel.this.getActionMap().put(k.toString(), new AbstractAction() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        MoveButton.this.doClick();
                    }
                });
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("LinePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.add(new ControlPanel(), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LinePanel().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045