如何将一个类的ChangeListener引用到另一个类?

时间:2014-02-17 12:52:37

标签: java swing class applet

我丢弃所有这些代码并完全不同的一个>>> 不需要回答任何问题

我从教授那里得到了一份功课。他告诉我们使用4个类制作applet绘图程序。 主要; LightSourcePanel; DrawingPanel; PalettePanel;

(基本代码没有给出;只有课程)

当我制作圆形线时,程序将采用鼠标输入并绘制圆圈。并且圆圈将具有取决于lightSource的“发光效果”(使用JSlider作为光)。当JSlider滑动时,圆形发光效果会实时变化。

我在将LightSource事件侦听器引用到Drawing时遇到问题,因此它更改了DrawingPanel内的'int light'。我不知道为什么JColorChooser中的引用工作,而这个没有。

(它给了我java“非静态方法无法从静态引用”,我无法将其更改为静态,因为我需要调用repaint()方法)

这是我的第三课程,教授只是在一堂课中教我们基本的动作听众。所以我不知道我在做什么。如果可能的话,请详细解释我的错误。

主要代码:

public class HW3_to_4 extends Applet {

    public void init (){            
        Dimension d = this.getSize();
        setLayout(new BorderLayout());
        JPanel Pale = new PalettePanel();
        Pale.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        JPanel Light =  new LightSourcePanel();
        Light.setBorder(BorderFactory.createLineBorder(Color.black, 5));
        JPanel Draw = new DrawingPanel();
        Cursor c = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
        Draw.setCursor(c);
        Draw.setBorder(BorderFactory.createLineBorder(Color.green, 3));

        add(Pale, BorderLayout.SOUTH );
        add(Light,BorderLayout.NORTH);
        add(Draw,BorderLayout.CENTER);
    }    
}

PalettePanel代码:

class PalettePanel extends JPanel implements ChangeListener {

    JColorChooser j;

    public PalettePanel ()  {
        j = new JColorChooser ();
        j.setPreviewPanel(new JPanel());
        j.getSelectionModel().addChangeListener(this);
        this.add(j);
    }

    public void stateChanged (ChangeEvent e){
        Color a = j.getColor();
        DrawingPanel.changecolor (a);
    }
}

DrawingPanel代码&内部的子类(DrawingCanvas和Polyline) 我从YAIP的示例页面中删除了它。 :

    public class DrawingPanel extends JPanel {

        private List<PolyLine> lines = new ArrayList<PolyLine>();
        private static PolyLine currentline;

            private  int maxX,maxY,difX, difY,minX,minY;
            private static  int lightSource = 0;
           public static final int CANVAS_WIDTH = 2000;
           public static final int CANVAS_HEIGHT = 800;
           public static int[][] circle = new int[1000][4];
           public static int check = 0;
           public static Color c = Color.RED;

        public DrawingPanel(){

            DrawingCanvas canvas = new DrawingCanvas();
            Dimension d = new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT);
            canvas.setPreferredSize(d);
            canvas.addMouseListener(new MouseAdapter(){

                public void mousePressed(MouseEvent e){

                    currentline = new PolyLine();
                    currentline.line_color = c;
                    lines.add(currentline);
                    currentline.add(e.getX(), e.getY());

                }
            });
            canvas.addMouseMotionListener(new MouseMotionAdapter(){

                public void mouseDragged(MouseEvent e){
                    currentline.add(e.getX(), e.getY());
                    repaint();
                }
            });
            canvas.addMouseListener(new MouseAdapter(){

                public void mouseReleased(MouseEvent e){

                    for(int i = 0; i<currentline.xList.size()-1; i++){
                        if(maxX<currentline.xList.get(i)){
                            maxX = currentline.xList.get(i);
                        }
                    }
                    for(int i = 0; i<currentline.yList.size()-1; i++){
                        if(maxY<currentline.yList.get(i)){
                            maxY = currentline.yList.get(i);
                        }
                    }
                    minX = maxX;
                    for(int i = 0; i<currentline.xList.size()-1; i++){
                        if(minX>currentline.xList.get(i)){
                            minX = currentline.xList.get(i);
                        }
                    }
                    minY = maxY;
                    for(int i = 0; i<currentline.yList.size()-1; i++){
                        if(minY>currentline.yList.get(i)){
                            minY = currentline.yList.get(i);
                        }
                    }
                    difX = maxX - minX;
                    difY = maxY - minY;


                        currentline.addcircle(minX,minY, difX, difY);

                            circle[check][0] = minX;
                            circle[check][1] = minY;
                            circle[check][2] = difX;
                            circle[check][3] = difY;
                            check++;

                    repaint();

                    maxX = 0; difX = 0;
                    maxY = 0; difY = 0;

                }
            });

            this.add(canvas);


        }

        public static void changecolor(Color b){
            c = b;
        }

        public void lightChange (int light){
            lightSource = light;
            RE();


        }
        public void RE (){

            for (int x = 0; x< check ; x++) currentline.addcircle(circle[x][0],circle[x][1], circle[x][2],circle[x][3],lightSource);
            repaint();
        }





private class DrawingCanvas extends JPanel{

    public void paint(Graphics g){

        for(PolyLine line : lines){
            g.setColor(line.line_color);
            line.draw(g);
        }
    }
}

static class PolyLine{

    public Color line_color = Color.RED;
    private List <Integer> xList;
    private List <Integer> yList;
    boolean drawcircle = false;
    int minx, miny, difx, dify, light;

     public PolyLine() {

          xList = new ArrayList<Integer>();
          yList = new ArrayList<Integer>();
       }

    public void add(int x, int y){
        xList.add(x);
        yList.add(y);
    }

    public void addcircle(int x, int y, int difx, int dify){
        this.minx = x; this.miny = y; this.difx = difx; this.dify = dify;
        drawcircle = true;
    }
    public void addcircle(int x, int y, int difx, int dify, int light){
        this.minx = x; this.miny = y; this.difx = difx; this.dify = dify; this.light = light;
        drawcircle = true;
    }
    public void draw(Graphics g){

        if(drawcircle){

            g.fillOval(minx, miny, difx, dify);
            g.setColor(Color.WHITE);
            g.fillOval((minx+difx/4)+light, miny+dify/4, difx/4, dify/4);
            g.setColor(line_color);

        }else{

            for(int i = 0; i<xList.size()-1; i++){
                g.drawLine((int)xList.get(i), (int)yList.get(i), (int)xList.get(i + 1),
                           (int)yList.get(i + 1));
            }
        }
    }}}
  

&GT;

Here is the problem.

LightSourcePanel Code:



class LightSourcePanel extends JPanel implements ChangeListener {

       static JSlider j;
    public LightSourcePanel (){


        j = new JSlider(0,180,90);
        j.setMajorTickSpacing(45);
        j.setMinorTickSpacing(5);
        j.setPaintLabels(true);
        j.setPaintTicks (true);     
        j.setPreferredSize(new Dimension (1500,50));
        j.addChangeListener(this);
        this.add(j);

    }


    @Override

    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        lightChange(j.getValue()); // problem <<<<<< FAIL
    }}

1 个答案:

答案 0 :(得分:0)

首先,在DrawingPanel中更改changecolor(Color)方法以删除static关键字。然后你需要一个DrawingColor实例来使用该类。我会将它的实例存储在一个字段中,就像存储JColorChooser一样。