Java 不使用AlphaComposite在图像顶部绘制线条

Java 不使用AlphaComposite在图像顶部绘制线条,java,swing,paintcomponent,drawimage,Java,Swing,Paintcomponent,Drawimage,我目前在一项作业中遇到很多困难。本任务的任务是创建一个具有java GUI功能的航空雷达VOR。当用户按下其键盘上的左/右箭头时,圆形雷达应该旋转,而雷达中间的一根针应该向左/向右移动。 我很快就要完成了,但我被困在了一部分。我画了一幅雷达图像,试图在上面画一条线。然而,只有当我能够使所有JPanel透明时,我才成功。这导致了一个问题,因为很难看到我在上面画了什么。 所以,我的问题是,我如何在雷达图像上绘制它,而不使所有内容都透明?下面是我的代码 public class finalVORGUI

我目前在一项作业中遇到很多困难。本任务的任务是创建一个具有java GUI功能的航空雷达VOR。当用户按下其键盘上的左/右箭头时,圆形雷达应该旋转,而雷达中间的一根针应该向左/向右移动。 我很快就要完成了,但我被困在了一部分。我画了一幅雷达图像,试图在上面画一条线。然而,只有当我能够使所有JPanel透明时,我才成功。这导致了一个问题,因为很难看到我在上面画了什么。 所以,我的问题是,我如何在雷达图像上绘制它,而不使所有内容都透明?下面是我的代码

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

}
公共类finalVORGUI扩展了JPanel{
私人JPanel轮换小组;
私用JPanel针;
私有JPanel属性;
私立国际学位;
私有字符串;
私人INTX;
私人int y1;
私人互联网y2;
最终整数宽度=600;
最终内部高度=600;
私有ImageIcon雷达=新ImageIcon(“image/vor1.png”);
/**
*类的构造函数
*它将把程序的尺寸设置为600x600
*背景将为白色(以便与背景融为一体)
*vor图像),它将添加vor雷达和一个辐射式雷达
*指示灯,用于让用户知道他/她正在使用哪个放射线
*/
公共财政组织(){
JLayeredPane lp=新的JLayeredPane();
lp.setPreferredSize(新尺寸(宽度、高度));
挫折地面(颜色:白色);
lp.setLayout(空);
lp.setFocusable(真);
lp.addKeyListener(新的KeyboardListener());
rotationPanel=新的JPanel();
旋转面板=新的旋转画布();
针=新的JPanel();
针=新的牵引针();
attributes=newjpanel();
属性=新属性();
lp.add(rotationPanel,Integer.valueOf(1));
lp.add(指针,整数值(2));
lp.add(属性,Integer.valueOf(3));
针.立根(100,0,针.getPreferredSize().宽度,针.getPreferredSize().高度);
rotationPanel.setBounds(100100,rotationPanel.getPreferredSize().width,rotationPanel.getPreferredSize().height);
attributes.setBounds(100100,rotationPanel.getPreferredSize().width,rotationPanel.getPreferredSize().height);
添加(lp);
度=360;//编辑:这将是雷达当前面对的径向
x=172;//x是针的位置
y1=155;
y2=330;
CurrentRadial=“Radial:+度;//一个总是在雷达上方的字符串。它将让用户知道当前的径向值
}
公共组件(图形g){
超级组件(g);
g、 抽绳(径向、250、100);
}
公共类DrawAttributes扩展了JPanel{
公共属性(){
设置不透明(假);
添加(新属性());
}
公共类属性扩展了JPanel{
int w=500;
int h=400;
公共属性(){
设置首选尺寸(新尺寸(w,h));
挫折地面(颜色:白色);
}
公共组件(图形g){
图形2d g2=(图形2d)g;
g2.抽绳(“至”,300400);
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_超过0.3f);
g2.设定行程(新基本行程(3));
超级组分(g);
g2.dispose();
}
}
}
公共类DrawNeedle扩展了JPanel{//todo:请确保此操作有效并正确添加到LayeredPane
公共提款针(){
设置不透明(假);
添加(新针());
}
公共级JPanel{
int w=500;
int h=400;
公共针头(){
设置首选尺寸(新尺寸(w,h));
挫折地面(颜色:白色);
}
私人空间绘制(图g){
图形2d g4=(图形2d)g;
g4.抽绳(“TO”,190200);
g4.抽绳(“从”,190,300);
}
公共组件(图形g){
图形2d g2=(图形2d)g;
RenderingHints=newrenderinghints(null);
put(renderinghits.KEY\u抗锯齿,renderinghits.VALUE\u抗锯齿开);
put(RenderingHints.KEY\u插值,RenderingHints.VALUE\u插值双三次);
put(RenderingHints.KEY\u RENDERING,RenderingHints.VALUE\u RENDER\u QUALITY);
g2.设置渲染提示(提示);
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_超过0.3f);
g2.设定行程(新基本行程(3));
doDrawing(g);
g2.拉线(x,y1,x,y2);
超级组分(g);
g2.dispose();
} 
}
}
公共类TurningCanvas扩展JPanel{
公共TurningCanvas(){
设置不透明(假);
添加(新TurningImage());
}
公共类TurningImage扩展了JPanel{
int w=radar.getIconWidth()-20;
int h=radar.getIconHeight()-20;
公众形象{
设置首选尺寸(新尺寸(w,h));
挫折地面(颜色:白色);
}
公共组件(图形g){
超级组分(g);
图形2d g2=(图形2d)g;
RenderingHints=newrenderinghints(null);