如何定义已经定义的set方法?

时间:2015-12-16 20:43:15

标签: java

我正在努力重构这个继承的项目,并且我在两个类之间遇到了一个错误:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;


public class PicturePuzzlePanel extends JPanel {

    private static final long serialVersionUID = 5106570002791771952L;

    private Game game;

    private boolean dragged;
    private int time;

    public PicturePuzzlePanel() {
        game = new Game();
        time = 0;

        dragged = false;

        setFocusable(true);
        requestFocus();

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if(!dragged) {
                    dragged = true;
                    game.fixOffsets(e.getX(), e.getY());
                }
                game.updateOffsets(e.getX(), e.getY());
                repaint();
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if(dragged) {
                    game.releaseOffsets();
                    dragged = false;
                    repaint();
                    if(game.gameOver()) {
                        JOptionPane.showMessageDialog(PicturePuzzle.frame, "Congratulations! You win... Time taken in seconds : " + time);
                        game = new Game();
                        time = 0;
                        repaint();
                    }
                }
            }
        });

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    PicturePuzzle.frame.dispose();
                }
                if(e.getKeyCode() == KeyEvent.VK_SPACE) {
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setAcceptAllFileFilterUsed(false);
                    fileChooser.setFileFilter(new ImageFilter());
                    int ret = fileChooser.showOpenDialog(PicturePuzzle.frame);
                    if(ret == JFileChooser.APPROVE_OPTION) {
                        PuzzlePic.setPuzzlePicture(fileChooser.getSelectedFile());
                        game = new Game();
                        time = 0;
                        repaint();
                    }
                }
            }
        });

        PicturePuzzle.frame.setVisible(true);

        Thread th = new Thread(new Timer());
        th.start();
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        for(int i = 0; i < 36; i++) {
            g2d.drawImage(game.getImage(i), game.getX(i), game.getY(i), this);
        }
    }

    private class ImageFilter extends FileFilter {
        public boolean accept(File f) {
            if (f.isDirectory())
                return true;
            String s = f.getName();
            s=s.toLowerCase();
            if (s.endsWith(".jpg") || s.endsWith(".png") || s.endsWith(".gif"))
                return true;

            return false;
        }

        public String getDescription() {
            return "Image Files(*.jpg  *.png  *.gif)";
        }
    }

    private class Timer implements Runnable {
        @Override
        public void run() {
            while(PicturePuzzle.frame.isVisible()) {
                try {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
                time++;
            }
        }
    }
}

具体来说,这条线路造成了麻烦,

PuzzlePic.setPuzzlePicture(fileChooser.getSelectedFile());

当我将鼠标悬停在错误消息上时,它显示&#34;方法setPuzzlePicture(File)未定义类型PuzzlePic&#34;:

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class PuzzlePic {
    private static BufferedImage puzzlePic;

    static {
        try {
            puzzlePic = ImageIO.read(PuzzlePic.class.getResourceAsStream("puzzle.jpg"));
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage getPuzzlePicture() {
        return puzzlePic;
    }
    public static void setPuzzlePicture(File file) {
        try {
            puzzlePic = resize(ImageIO.read(file), 512, 512);
        }
        catch(IOException e) {
            try {
                puzzlePic = ImageIO.read(PuzzlePic.class.getResourceAsStream("puzzle.jpg"));
            }
            catch(IOException f) {
                f.printStackTrace();
            }
        }


    }
    private static BufferedImage resize(BufferedImage img, int newW, int newH) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        BufferedImage dimg = new BufferedImage(newW, newH, img.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }



}

在这里您可以看到方法已明确定义:

public static void setPuzzlePicture(File file) {
try {
    puzzlePic = resize(ImageIO.read(file), 512, 512);
}
catch(IOException e) {
    try {
        puzzlePic = ImageIO.read(PuzzlePic.class.getResourceAsStream("puzzle.jpg"));
    }
    catch(IOException f) {
        f.printStackTrace();
    }
}

问题是什么?

1 个答案:

答案 0 :(得分:0)

当我使用eclipse时,我通常会遇到这个问题。 你只需要去Project&gt;清洁

这将删除旧类并构建新类。这应该删除错误。

如果您正面对其他IDE,则应该有类似的功能。