当前位置: 首页 > 知识库问答 >
问题:

getGraphics()返回空值

翟奕
2023-03-14

我的JPanel预览在drawToScreen方法中调用getGraphics()时返回null。测试类确实扩展了JPanel,因为它被保存在选项卡窗格中。该类还实现了RunnablekeystenermouseStener

系统的日志。出来println

javax。摆动JPanel[,1721491280x720,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=null]

下面是现有代码的示例。运行它是行不通的,因为它使用外部方法来完成自己,但希望能找到答案。

public Test() {

    setBackground(Color.DARK_GRAY);
    setLayout(null);

    preview = new JPanel();
    preview.setBounds(172, 149, 1280, 720);
    add(preview);
}

public void addNotify() {
    preview.addNotify();
    if(thread == null) {
        thread = new Thread(this);
        preview.addKeyListener(this);
        preview.addMouseListener(this);
        thread.start();
    }
}

private void init() {
    image = new BufferedImage(WIDTH, HEIGHT,
            BufferedImage.TYPE_INT_ARGB);
    g = (Graphics2D) image.getGraphics();
    running = true;
}

private long redraw() {

    long t = System.currentTimeMillis();

    if(onTab) {

        if(!FileManager.isSleeping())
            update();

        if(!pause) {
            draw();
            drawToScreen();
        }

    }

    return System.currentTimeMillis() - t;
}

public void run() {

    init();

    while(running) {

        long durationMs = redraw();

        try {
            Thread.sleep(Math.max(0, FPS - durationMs));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

private void update () {
    reupdateImages();
}

private void draw() {
    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    for(int i = 0; i < layers.length; i++) {
        if(layers[i] != null) {
            g.drawImage(
                    layers[i].getImage(), 
                    layers[i].getX(), 
                    layers[i].getY(), 
                    layers[i].getWidth(), 
                    layers[i].getHeight(), 
                    null
            );
            g.setColor(Color.red);
            for(int j = 0; j < layers.length; j++) {
                if(layers[j].isSelected()) 
                    g.drawRect(
                            layers[j].getX(),
                            layers[j].getY(),
                            layers[j].getWidth(),
                            layers[j].getHeight()
                    );
            }
        }
    }
}

private void drawToScreen() {
    System.out.println(preview);
    System.out.println(preview.getGraphics());
    Graphics g2 = preview.getGraphics();
    g2.drawImage(image, 0, 0, 
            WIDTH, HEIGHT, 
            null);
    panelImage = image;
    g2.dispose();
}

至于代码的其余部分,根据markspace的要求,我添加了其余部分,只是为了在提供的示例之外揭穿。

@SuppressWarnings("serial")
public class ThumbnailEditor extends JPanel implements Runnable, KeyListener, MouseListener {

private JComboBox layerBox;
private int pos = 0;

private JFileChooser jfc;

// automated gui, I'm lazy and its dynamic
private int numberOfLayers = 24;        // even numbers only guys and keep above 8

private JPanel preview;

private JSpinner localWidth;
private JSpinner localHeight;
private JSpinner posx;
private JSpinner posy;
private boolean ignore = false;

private JTextField width;
private JTextField height;

private JButton remove;
private JButton add;
private JButton select;
private JButton edit;

// drawing stoof
public int WIDTH = 1280;
public int HEIGHT = 720;

private Thread thread;
private boolean running;
private int FPS = 60;

private File location;
private BufferedImage panelImage;
private BufferedImage image;
private Graphics2D g;
private boolean pause = false;
private boolean onTab = false;

// when it gets too big, annoying issues start to happen with the text. This automatically fixes it
private static int[] overrideSizes = {
        8,
        9,
        10,
        11,
        12,
        14,
        16,
        18,
        20,
        22,
        24,
        26,
        28,
        36,
        48,
        72
};

// adjust this, low the more sensitive the changing of the font is.
private static int sensitivity = 4;

// layer stoof
// layer 0 --> at 0. layer 1 --> at 1
private static ThumbnailObject[] layers;
public static TextEditor[] te;

public ThumbnailEditor() {

    setBackground(Color.DARK_GRAY);
    setLayout(null);

    preview = new JPanel();
    preview.setBounds(172, 149, 1280, 720);
    add(preview);


    layerBox = new JComboBox();
    layerBox.setBackground(Color.DARK_GRAY);
    layerBox.setFont(new Font("Arial Black", Font.BOLD, 14));
    layerBox.setBounds(10, 11, 100, 40);
    add(layerBox);

    add = new JButton("Add");
    add.setBackground(Color.DARK_GRAY);
    add.setFont(new Font("Arial Black", Font.BOLD, 14));
    add.setBounds(120, 11, 100, 40);
    add(add);

    select = new JButton("Select");
    select.setBackground(Color.DARK_GRAY);
    select.setFont(new Font("Arial Black", Font.BOLD, 14));
    select.setBounds(230, 11, 100, 40);
    add(select);

    edit = new JButton("Edit");
    edit.setBackground(Color.DARK_GRAY);
    edit.setFont(new Font("Arial Black", Font.BOLD, 14));
    edit.setBounds(340, 11, 100, 40);
    add(edit);

    remove = new JButton("Remove");
    remove.setBackground(Color.DARK_GRAY);
    remove.setFont(new Font("Arial Black", Font.BOLD, 14));
    remove.setBounds(450, 11, 100, 40);
    add(remove);

    JButton generate = new JButton("Generate Test Image");
    generate.setBackground(Color.DARK_GRAY);
    generate.setFont(new Font("Arial Black", Font.BOLD, 14));
    generate.setBounds(1376, 11, 239, 40);
    add(generate);

    JButton deselect = new JButton("Deselect All Layers");
    deselect.setBackground(Color.DARK_GRAY);
    deselect.setFont(new Font("Arial Black", Font.BOLD, 14));
    deselect.setBounds(120, 62, 210, 40);
    add(deselect);

    JLabel widthLabel = new JLabel("Width");
    widthLabel.setHorizontalAlignment(SwingConstants.CENTER);
    widthLabel.setForeground(Color.WHITE);
    widthLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
    widthLabel.setBounds(10, 149, 70, 40);
    add(widthLabel);

    JLabel heightLabel = new JLabel("Height");
    heightLabel.setHorizontalAlignment(SwingConstants.CENTER);
    heightLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
    heightLabel.setForeground(Color.WHITE);
    heightLabel.setBounds(90, 149, 70, 40);
    add(heightLabel);

    localWidth = new JSpinner();
    localWidth.setFont(new Font("Arial Black", Font.BOLD, 14));
    localWidth.setBounds(10, 184, 70, 20);
    add(localWidth);

    localHeight = new JSpinner();
    localHeight.setFont(new Font("Arial Black", Font.BOLD, 14));
    localHeight.setBounds(90, 184, 70, 20);
    add(localHeight);

    JLabel xSizeLabel = new JLabel("X");
    xSizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    xSizeLabel.setForeground(Color.WHITE);
    xSizeLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
    xSizeLabel.setBounds(10, 215, 70, 40);
    add(xSizeLabel);

    JLabel ySizeLabel = new JLabel("Y");
    ySizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    ySizeLabel.setForeground(Color.WHITE);
    ySizeLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
    ySizeLabel.setBounds(90, 215, 70, 40);
    add(ySizeLabel);

    posx = new JSpinner();
    posx.setFont(new Font("Arial Black", Font.BOLD, 14));
    posx.setBounds(10, 248, 70, 20);
    add(posx);

    posy = new JSpinner();
    posy.setFont(new Font("Arial Black", Font.BOLD, 14));
    posy.setBounds(92, 248, 70, 20);
    add(posy);

    pause = true;
    layers = new ThumbnailObject[numberOfLayers];
    te = new TextEditor[numberOfLayers];

    for(int i = 0; i < layers.length; i++) {
        layers[i] = new ThumbnailObject();
    }

    for(int i = 0; i < numberOfLayers; i++) {
        layerBox.addItem("Layer [" + i + "]");
    }

    remove.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            pos = layerBox.getSelectedIndex();
            layers[pos].reset();
            add.setToolTipText("");
            if(te[pos] != null) {
                te[pos].getFrame().dispose();
                te[pos] = null;
            }
        }

    });
    select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            pos = layerBox.getSelectedIndex();
            if(layers[pos].getImage() != null) {
                for(int i = 0; i < layers.length; i++) {
                    layers[i].setSelected(false);
                }
                layers[pos].setSelected(true);
                localWidth.setValue(layers[pos].getWidth());
                localHeight.setValue(layers[pos].getHeight());
                posx.setValue(layers[pos].getX());
                posy.setValue(layers[pos].getY());
            }           
        }           
    });
    edit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pos = layerBox.getSelectedIndex();
            if(layers[pos].getImage() != null) {
                if(te[pos] != null) {
                    te[pos].getFrame().setVisible(true);
                }
            }
        }
    });
    add.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            pos = layerBox.getSelectedIndex();
            jfc = new JFileChooser();
            jfc.setCurrentDirectory(new java.io.File("user.home"));
            jfc.setDialogTitle("Select Layer 0 Image File");
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (jfc.showOpenDialog(add) == JFileChooser.APPROVE_OPTION) {
                try {
                    layers[pos].setFile(jfc.getSelectedFile());
                    if(!layers[pos].isReversed())
                        if(layers[pos].getFile().getName().contains(".txt")) {
                            if(te[pos] == null)
                                te[pos] = new TextEditor(pos);
                            else
                                te[pos].getFrame().setVisible(true);
                            // load defaults, this will be overriden when saved
                            layers[pos].setFont(te[pos].getFont());
                            layers[pos].setAlignment(te[pos].getAlignment());
                            layers[pos].setSize(te[pos].getSize());
                            layers[pos].setColor(te[pos].getColor()[0], te[pos].getColor()[1], te[pos].getColor()[2]);
                            layers[pos].setBold(te[pos].isBold());
                            layers[pos].setItalic(te[pos].isItalic());
                            layers[pos].setAdjusted(te[pos].isAdjusted());
                            layers[pos].setWidth(te[pos].getWidth());
                            layers[pos].setHeight(te[pos].getHeight());
                            layers[pos].setImage(convertTextToImage(layers[pos].getFile(), pos));
                        } else 
                            if(layers[pos].getFile().getName().contains("png") ||
                                    layers[pos].getFile().getName().contains("jpg") ||
                                    layers[pos].getFile().getName().contains("jpeg") ||
                                    layers[pos].getFile().getName().contains("bmp")){
                            layers[pos].setImage(ImageIO.read(layers[pos].getFile()));
                            layers[pos].setX(0);
                            layers[pos].setY(0);
                            layers[pos].setWidth((int) (layers[pos].getImage().getWidth()));
                            layers[pos].setHeight((int) (layers[pos].getImage().getHeight()));
                        } else {
                            JOptionPane.showMessageDialog(null, "txt, png, jpg, and bmp files only!");
                            layers[pos].reset();
                            add.setToolTipText("");
                            if(te[pos] != null) {
                                te[pos].getFrame().dispose();
                                te[pos] = null;
                            }
                            return;
                        }
                    else {
                        reverseImage(pos);
                    }
                    edit.setEnabled(true);
                    add.setToolTipText(jfc.getSelectedFile().getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    pause = false;

    deselect.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            for(int i = 0; i < numberOfLayers; i++) {
                layers[i].setSelected(false);
            }
        }

    });

    generate.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                generatePanel(null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    });

    posx.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent arg0) {
            for(int i = 0; i < layers.length; i++) {
                if(layers[i].isSelected()) {
                    layers[i].setX((int) posx.getValue());
                }
            }
        }

    });

    posy.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            for(int i = 0; i < layers.length; i++) {
                if(layers[i].isSelected()) {
                    layers[i].setY((int) posy.getValue());
                }
            }
        }

    });

    localWidth.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            for(int i = 0; i < layers.length; i++) {
                if(layers[i].isSelected())
                    layers[i].setWidth((int) localWidth.getValue());
            }
        }
    });

    localHeight.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            if(!ignore) {
                for(int i = 0; i < layers.length; i++) {
                    if(layers[i].isSelected())
                        layers[i].setHeight((int) localHeight.getValue());
                }
            } else {
                ignore = false;
            }
        }
    });

}

public void addNotify() {
    preview.addNotify();
    preview.requestFocus();
    preview.setFocusable(true);
    preview.setVisible(true);
    if(thread == null) {
        thread = new Thread(this);
        preview.addKeyListener(this);
        preview.addMouseListener(this);
        thread.start();
    }
}

private void init() {
    image = new BufferedImage(WIDTH, HEIGHT,
            BufferedImage.TYPE_INT_ARGB);
    g = (Graphics2D) image.getGraphics();
    running = true;
}

private long redraw() {

    long t = System.currentTimeMillis();

    if(onTab) {

        if(!FileManager.isSleeping())
            update();

        if(!pause) {
            draw();
            //drawToScreen();
        }

    }

    return System.currentTimeMillis() - t;
}

public void run() {

    init();

    while(running) {

        long durationMs = redraw();

        try {
            Thread.sleep(Math.max(0, FPS - durationMs));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

private void update () {
    reupdateImages();
}

private void draw() {
    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    for(int i = 0; i < layers.length; i++) {
        if(layers[i] != null) {
            g.drawImage(
                    layers[i].getImage(), 
                    layers[i].getX(), 
                    layers[i].getY(), 
                    layers[i].getWidth(), 
                    layers[i].getHeight(), 
                    null
            );
            g.setColor(Color.red);
            for(int j = 0; j < layers.length; j++) {
                if(layers[j].isSelected()) 
                    g.drawRect(
                            layers[j].getX(),
                            layers[j].getY(),
                            layers[j].getWidth(),
                            layers[j].getHeight()
                    );
            }
        }
    }
}

private void drawToScreen() {
    System.out.println(preview);
    System.out.println(preview.getGraphics());
    Graphics g2 = preview.getGraphics();
    g2.drawImage(image, 0, 0, 
            WIDTH, HEIGHT, 
            null);
    panelImage = image;
    g2.dispose();
}

public BufferedImage generateThumbnail() {
    // so draw doesnt interfere
    if(!pause) pause = true;
    for(int i = 0; i < numberOfLayers; i++) {
        layers[i].setSelected(false);
    }
    draw();
    drawToScreen();
    int genwidth = Integer.parseInt(width.getText());
    int genheight = Integer.parseInt(height.getText());
    // manipulate the width and height to specs

    BufferedImage resized = new BufferedImage(genwidth, genheight, BufferedImage.TYPE_INT_ARGB);
    Graphics g = resized.createGraphics();
    g.drawImage(panelImage, 0, 0, genwidth, genheight, null);
    g.dispose();
    pause = false;
    return resized;
}

public void deselect() {
    for(int i = 0; i < numberOfLayers; i++) {
        layers[i].setSelected(false);
    }
}

private void generatePanel(String n) throws IOException {
    if(!pause) pause = true;
    for(int i = 0; i < numberOfLayers; i++) {
        layers[i].setSelected(false);
    }
    draw();
    drawToScreen();
    String user = System.getProperty("user.name");
    String location = FileManager.getMediaDirectory().replaceAll("/", "\\\\") + "\\"; 

    int genwidth = WIDTH;
    int genheight = HEIGHT;
    // manipulate the width and height to specs

    BufferedImage resized = new BufferedImage(genwidth, genheight, BufferedImage.TYPE_INT_ARGB);
    Graphics g = resized.createGraphics();
    g.drawImage(panelImage, 0, 0, genwidth, genheight, null);
    g.dispose();

    //print
    File outputfile = null;
    if(n == null || n == "")
        outputfile = new File(location + "\\test.png");
    else
        outputfile = new File(n);
    pause = false;
    ImageIO.write(resized, "png", outputfile);

}

public static void reupdateImagesOverride() {
    /* basically, this will update any changes thus
     * if a char change happened, it will change ONLY if you are
     * using root as your media center
     */ 
    for(int i = 0; i < layers.length; i++) {
        if(layers[i] != null && layers[i].getFile() != null && layers[i].getImage() != null) {
            try {
                if(!layers[i].isReversed()) {
                    if(layers[i].getFile().getName().contains(".txt")) {
                        layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
                        layers[i].setWidth(layers[i].getImage().getWidth());
                        layers[i].setHeight(layers[i].getImage().getHeight());
                    } else
                        layers[i].setImage(ImageIO.read(layers[i].getFile()));
                } else {
                    reverseImage(i);
                }
                try{
                    layers[i].collectTimeStamp();
                } catch (Exception e2) {

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

private void reupdateImages() {
    /* basically, this will update any changes thus
     * if a char change happened, it will change ONLY if you are
     * using root as your media center
     */ 
    for(int i = 0; i < layers.length; i++) {
        if(layers[i] != null && layers[i].getFile() != null && layers[i].getImage() != null) {
            if(layers[i].getFile().lastModified() != layers[i].getTimeStamp()) {
                try {
                    if(!layers[i].isReversed()) {
                        if(layers[i].getFile().getName().contains(".txt")) {
                            layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
                            layers[i].setWidth(layers[i].getImage().getWidth());
                            layers[i].setHeight(layers[i].getImage().getHeight());
                        } else
                            layers[i].setImage(ImageIO.read(layers[i].getFile()));
                    } else {
                        reverseImage(i);
                    }
                    try{
                        layers[i].collectTimeStamp();
                    } catch (Exception e2) {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

// really simple
public static BufferedImage convertTextToImage(File f, int i) {
    try {
        int tmpy = 0;
        int type = Font.PLAIN;
        if(layers[i].isBold()) type = Font.BOLD;
        if(layers[i].isItalic()) type = type | Font.ITALIC;
        layers[i].setFont(te[i].getFont());
        layers[i].setAlignment(te[i].getAlignment());
        layers[i].setSize(te[i].getSize());
        layers[i].setColor(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]);
        layers[i].setBold(te[i].isBold());
        layers[i].setItalic(te[i].isItalic());
        layers[i].setAdjusted(te[i].isAdjusted());
        layers[i].setWidth(te[i].getWidth());
        layers[i].setHeight(te[i].getHeight());

        // grab width of longest line, if it's multi-line
        BufferedImage tmp = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
        BufferedImage actual;
        BufferedImage ghetto;
        Graphics2D gx = tmp.createGraphics();
        gx.setColor(new Color(layers[i].getColor()[0], layers[i].getColor()[1], layers[i].getColor()[2]));
        gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
        String line = null;
        BufferedReader reader = new BufferedReader(new FileReader(f));
        int longest = 0;
        while((line = reader.readLine()) != null) {
            if(gx.getFontMetrics().stringWidth(line) > longest) {
                longest = gx.getFontMetrics().stringWidth(line);
            }
        }

        // check if the image/text is longer then designated width
        if(longest > layers[i].getWidth()) {
            if(layers[i].isAdjusted()) {
                int tmpnum = longest - layers[i].getWidth();
                int reduce = 0;
                while(tmpnum > sensitivity) {
                    tmpnum %= sensitivity;
                    reduce++;
                }
                for(int z = 0; z < overrideSizes.length; z++) {
                    if(overrideSizes[z] > layers[i].getSize()) {
                        if(z - reduce >= 0) {
                            layers[i].setSize(overrideSizes[z - reduce]);
                            if(layers[i].getAlignment().equals("right") || layers[i].getAlignment().equals("center")) {
                                if(z - (reduce + 1) >= 0) layers[i].setSize(overrideSizes[z - (reduce + 1)]);
                            }
                        } else {
                            layers[i].setSize(overrideSizes[0]);
                        }
                        break;
                    }
                }
            }
            // draw to image, ignore controllers wish of width, we will do that later
            actual = new BufferedImage(longest, layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
            gx.dispose();
            tmp = null;
            gx = actual.createGraphics();
            gx.setColor(new Color(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]));
            gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
            reader.close();
            reader = new BufferedReader(new FileReader(f));
            while((line = reader.readLine()) != null) {
                gx.drawString(line,0, (tmpy += gx.getFontMetrics().getHeight()));
            }
            reader.close();
            // now lets resize this
            ghetto = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2x = ghetto.createGraphics();
            g2x.drawImage(actual, 0, 0, layers[i].getWidth(), layers[i].getHeight(), null);
            g2x.dispose();
            return ghetto;
        } else {
            actual = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
            gx.dispose();
            tmp = null;
            gx = actual.createGraphics();
            gx.setColor(new Color(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]));
            gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
            reader.close();
            reader = new BufferedReader(new FileReader(f));
            while((line = reader.readLine()) != null) {
                if(layers[i].getAlignment().equals("left"))
                    gx.drawString(line,0, (tmpy += gx.getFontMetrics().getHeight()));
                else
                if(layers[i].getAlignment().equals("right"))
                    gx.drawString(line, layers[i].getWidth() - gx.getFontMetrics().stringWidth(line), (tmpy += gx.getFontMetrics().getHeight()));
                else
                if(layers[i].getAlignment().equals("center"))
                    gx.drawString(line, (layers[i].getWidth() / 2) - (gx.getFontMetrics().stringWidth(line) / 2), (tmpy += gx.getFontMetrics().getHeight()));
            }
            reader.close();
            return actual;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

private static void reverseImage(int i) {
    BufferedImage tmp = new BufferedImage(
            layers[i].getImage().getWidth(),
            layers[i].getImage().getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics gx = tmp.createGraphics();
    try {
        if(layers[i].getFile().getName().contains(".txt")) {
            layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
        } else
            layers[i].setImage(ImageIO.read(layers[i].getFile()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    gx.drawImage(
            layers[i].getImage(), 
            layers[i].getImage().getWidth(), 
            0, 
            -layers[i].getImage().getWidth(), 
            layers[i].getImage().getHeight(), 
            null);
    gx.dispose();
    layers[i].setImage(tmp);
}

public void mouseClicked(MouseEvent arg0) {
}

public void mouseEntered(MouseEvent arg0) {
}

public void mouseExited(MouseEvent arg0) {
}

public void mousePressed(MouseEvent arg0) {
    // button is being held, scanning layers that match
    if(arg0.getButton() == MouseEvent.BUTTON3) {
        for(int i = 0; i < layers.length; i++) {
            if(layers[i].getFile() != null && layers[i].getImage() != null && layers[i].isSelected()) {
                if(layers[i].isReversed()) {
                    layers[i].setReversed(false);
                    reupdateImagesOverride();
                    return;
                } else {
                    layers[i].setReversed(true);
                    reupdateImagesOverride();
                    return;
                }
            }
        }
    }
    if(arg0.getButton() == MouseEvent.BUTTON1) {
        for(int i = 0; i < layers.length; i++) {
            if(layers[i].isSelected()) {
                layers[i].setX(arg0.getX());
                layers[i].setY(arg0.getY());
                posx.setValue(arg0.getX());
                posy.setValue(arg0.getY());
            }
        }
    }
}

public void mouseReleased(MouseEvent arg0) {
}

public void keyPressed(KeyEvent arg0) {
}

public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}

public void setOnTab(boolean b) { 
    onTab = b;
}

}

共有1个答案

锺威
2023-03-14

别忘了通知!

一个JPanel只有一个Graphics实例,并且只有当它被添加到父容器中时。

Graphics实例是在组件期间构建的。addNotify()但是您覆盖了它,阻止了它的创建。

也许试着打电话给super。addNotify()是一种快速解决方法。

 类似资料:
  • 问题内容: 我正在尝试渲染自定义的Swing组件,在该组件中扩展了JComponent类。 为了简化组件要求,让我们总结一下我的组件,以使其需要渲染一些字符串,每个字符串都有自己的字体。 我需要将组件的大小精确调整为渲染字符串的宽度和高度的总和。 为了确定此大小,我使用FontMetrics计算每个字符串的大小。有了这些信息,我可以弄清楚我的组件的大小,并适当地调整它的大小。 问题是,当我访问ge

  • 我无法理解为什么findFragmentByTag()在我的代码中返回null。我已经在片段onCreate()上设置了RetainInstance(true),并且正在使用getSupportFragmentManager()。findFragmentByTag(),因为它是SherlockFragmentActivity。 这是我的活动代码: } 这是我的片段代码:

  • 下面是我的控制器代码:- 以下是我的服务代码:- 下面是我的存储库代码:-

  • 问题内容: 通常,当我调用getGraphics()时,即使我设置了xxx.getGraphics();它也返回null。xxx可见(如Google搜索所示…) 但这是行不通的,这使我感到沮丧,因为它在C-Sharp中很容易实现。 有谁知道这样做的更好方法,而不是使用getGraphics()? 问题答案: 您通常不希望在Java Swing组件上使用getGraphics,因为如果尚未渲染该组件

  • 当我在android Studio中集成OpenCV进行眼睛检测时。集成后消息打印Opencv安装成功消息生成,但出现错误。 无法从加载器加载detection_based_trackerdalvik.system.PathClassLoader[DexPathList[[zip file"/data/app/com.example.admin.lanskart-2.apk],nativeLibr

  • 这一行是: 我在和上尝试了和,但仍然给出相同的错误