1

I am currently having lots of difficulty in one of my assignments. The task for this assignment is to create an aviation radar, VOR, with java's GUI features. When the user presses either the left/right arrow on their keyboard, the circular radar should rotate, and a needle in the middle of the radar should move left/right. I am very close to finishing, but I am stuck on one part. I drew an image of the radar, and I attempted to draw a line on top of it. However, I was only successful when I was able to make all JPanels transparent. This causes a problem because it is difficult to see what I drew on top of it. So, my question is, how would I draw it on top of the radar image without making everything transparent? Below is my code

public class finalVORGUI extends JPanel{
    private JPanel rotationPanel;
    private JPanel needle;
    private JPanel attributes;
    private int degrees;
    private String CurrentRadial;
    private int x;
    private int y1;
    private int y2;
    final int WIDTH = 600;
    final int HEIGHT = 600;
    private ImageIcon radar = new ImageIcon("image/vor1.png");

    /**
     * The constructor for the class
     * It's going to set the dimension of the program to 600x600, the 
     * background is going to be white (in order to blend in with the
     * vor image), and it is going to add in the VOR radar and a radial
     * indicator that will let the user know which radial he/she is on
     */
    public finalVORGUI(){
        JLayeredPane lp = new JLayeredPane();
        lp.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.white);
        lp.setLayout(null);
        lp.setFocusable(true);
        lp.addKeyListener(new KeyboardListener());  
        rotationPanel = new JPanel();
        rotationPanel = new TurningCanvas();
        needle = new JPanel(); 
        needle = new DrawNeedle(); 
        attributes = new JPanel();
        attributes = new DrawAttributes();
        lp.add(rotationPanel, Integer.valueOf(1));
        lp.add(needle, Integer.valueOf(2));
        lp.add(attributes, Integer.valueOf(3));
        needle.setBounds(100,0, needle.getPreferredSize().width, needle.getPreferredSize().height);
        rotationPanel.setBounds(100, 100, rotationPanel.getPreferredSize().width, rotationPanel.getPreferredSize().height);
        attributes.setBounds(100, 100, rotationPanel.getPreferredSize().width, rotationPanel.getPreferredSize().height);
        add(lp);
        degrees = 360; //to edit: this is going to be the radial the radar is currently facing
        x = 172; //x is the location of the needle
        y1 = 155;
        y2 = 330;
        CurrentRadial = "Radial: " + degrees; //A string that is always going to be above the radar. it's going to let the user know the current radial
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString(CurrentRadial, 250, 100);
    }

    public class DrawAttributes extends JPanel{

        public DrawAttributes(){
            setOpaque(false);
            add(new Attributes());
        }

        public class Attributes extends JPanel{
            int w = 500;
            int h = 400;

            public Attributes(){
                setPreferredSize(new Dimension(w,h));
                setBackground(Color.white);
            }

            public void paintComponent(Graphics g){  
                Graphics2D g2 = (Graphics2D) g;   
                g2.drawString("To",300,400);
                g2.setComposite(AlphaComposite.getInstance(
                        AlphaComposite.SRC_OVER, 0.3f));
                g2.setStroke(new BasicStroke(3));
                super.paintComponent (g);  
                g2.dispose();  
            }
        }
    }

    public class DrawNeedle extends JPanel{ //todo: make sure this works and adds correctly to the LayeredPane

        public DrawNeedle(){
            setOpaque(false);
            add(new Needle());
        }

        public class Needle extends JPanel{
            int w = 500;
            int h = 400;

            public Needle(){
                setPreferredSize(new Dimension(w,h));
                setBackground(Color.white);
            }

            private void doDrawing(Graphics g){
                Graphics2D g4 = (Graphics2D) g;
                g4.drawString("TO", 190, 200);
                g4.drawString("FROM",190, 300);
            }

            public void paintComponent(Graphics g){  
                Graphics2D g2 = (Graphics2D) g;  
                RenderingHints hints = new RenderingHints(null);  
                hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
                hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
                hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);  
                g2.setRenderingHints(hints);  
                g2.setComposite(AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, 0.3f));
                g2.setStroke(new BasicStroke(3));
                doDrawing(g);
                g2.drawLine(x,y1,x,y2);
                super.paintComponent (g);  
                g2.dispose();  
            } 
        }
    }

    public class TurningCanvas extends JPanel{

        public TurningCanvas(){
            setOpaque(false);
            add(new TurningImage());
        }

        public class TurningImage extends JPanel{
            int w = radar.getIconWidth()- 20;
            int h = radar.getIconHeight() -20;

            public TurningImage(){
                setPreferredSize(new Dimension(w,h));
                setBackground(Color.white);
            }



            public void paintComponent(Graphics g){  
                super.paintComponent (g);  
                Graphics2D g2 = (Graphics2D) g;  
                RenderingHints hints = new RenderingHints(null);  
                hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
                hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
                hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);  
                g2.setRenderingHints(hints);  
                g2.rotate (Math.toRadians(degrees),w/2, h/2);  
                g2.drawImage(radar.getImage(), 0, 0, this);
                //g2.drawLine(171,0,x,300);
                g2.dispose();  
            } 

        }
    }

    /**
     * This is the keyboard listener that this program will be using
     * Depending on what the user wishes, or what type of keyboard they have,
     * they will be able to find their desired radial by using the arrow keys
     * or the "a" and "d" buttons. Left arrow to minus radial, right arrow to plus radial, etc etc...
     *
     */
    public class KeyboardListener implements KeyListener{  
       public void keyPressed (KeyEvent event) {  
           if(event.getKeyCode() == KeyEvent.VK_LEFT){  
               degrees--;
               x--;
               if(degrees <= 0){
                   degrees = 360;
               }
               if(x <= 89){
                   x = 89;
               }
               CurrentRadial = "Radial: " + degrees;
               repaint();  
           }  
           if(event.getKeyCode() == KeyEvent.VK_RIGHT){  
               degrees++;
               x++;
               if(degrees >= 360){
                   degrees = 1;
               }
               if(x >= 250){
                   x = 250;
               }
               CurrentRadial = "Radial: " + degrees;
               repaint();  
           }  
       }  
       public void keyTyped (KeyEvent event) {}  
       public void keyReleased (KeyEvent event) {}  
    } 

    /**
     * The main method of this class
     * This is going to make a new JFrame, which will hold the new
     * VOR radar
     * @param args
     */
    public static void main(String[] args){  
        finalVORGUI test = new finalVORGUI();
        JFrame frame = new JFrame("VOR Radar");  
        frame.setContentPane(test);  
        frame.pack();  
        frame.setVisible(true);
    }  

}
7
  • Are you not allowed to draw in the same panel? May 3, 2014 at 12:18
  • I'm sorry, I forgot to go over that part as well. It would be much easier to draw in the same panel, the KeyBoardListener makes the image of the radar rotate. The line I want to draw is not supposed to rotate, only shift to the sides. Thats why I could not draw them in the same panel May 3, 2014 at 12:29
  • Sorry, but your statement above makes no sense. Those requirements do not stop you from drawing both items on the same JPanel. Period. May 3, 2014 at 12:42
  • @HovercraftFullOfEels I know I could definitely draw them on the same panel, but I don't know how to prevent the line from rotating whenever I want to rotate the image of the radar May 3, 2014 at 12:54
  • You could create a copy of the Graphics2D object, transform the copy, draw the rotated image, dispose of the copy, and draw the non-rotated image with the original Graphics object. May 3, 2014 at 13:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.