简体   繁体   English

如何在图像中移动矩形?

[英]How do I make a rectangle move in an image?

Basically I have an image loaded, and when I click a portion of the image, a rectangle (with no fill) shows up. 基本上我加载了一个图像,当我点击图像的一部分时,会出现一个矩形(没有填充)。 If I click another part of the image again, that rectangle will show up once more. 如果再次单击图像的另一部分,该矩形将再次显示。 With each click, the same rectangle should appear. 每次单击时,应显示相同的矩形。

So far I have this code, now I don't know how to make the image appear. 到目前为止,我已经有了这段代码,现在我不知道如何使图像显示出来。 My image from my file directory. 我的文件目录中的图像。 I have already made the code to get the image from my file directory. 我已经制作了代码来从我的文件目录中获取图像。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MP2 extends JPanel implements MouseListener{

    JFrame frame;
    JPanel panel;

    int x = 0;
    int y = 0;
    String input;

    public MP2(){

    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        MP2 panel = new MP2();
        panel.addMouseListener(panel);
        frame.add(panel);
        frame.setSize(200,200);
        frame.setVisible(true);

    }

    public void mouseClicked(MouseEvent event) {
        // TODO Auto-generated method stub

        this.x = event.getX();
        this.y = event.getY();
        this.repaint();
        input = JOptionPane.showInputDialog("Something pops out");
        System.out.println(input);

    }

    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        // this.setBackground(Color.white); *Sets the bg color of the panel

        g.setColor(new Color(255,0,0));
        g.drawRect(x, y, 100, 100);
    }
}

You may want to look at drawing the rectangle on The Glass Pane , as shown in GlassPaneDemo . 您可能想看看在Glass Pane上绘制矩形,如GlassPaneDemo所示。 For example, in paintComponent() , replace g.fillOval() with g.drawRect() . 例如,在paintComponent() ,将g.fillOval()替换为g.drawRect()

I don't know how to make the image appear. 我不知道如何使图像出现。

This example shows how to display an image in a JLabel . 示例说明如何在JLabel显示图像。

this.x and this.y refer to the x and y of your JPanel, not the rectangle you want to draw. this.x和this.y是指JPanel的x和y,而不是要绘制的矩形。 You'll need to create two additional fields, rectX and rectY. 您需要创建两个额外的字段,rectX和rectY。 These get set in mouseClicked and used by paintComponent(). 这些在mouseClicked中设置,并由paintComponent()使用。

EDIT 编辑

I'm sorry, my bad. 对不起,我很难过。 I'm now confused. 我现在很困惑。 You do declare an x and y. 你确实声明了一个x和y。 These should still be renamed cause they can be confused with the x and y defined in Component, but they are not the problem. 这些仍然应该被重命名,因为它们可能与Component中定义的x和y混淆,但它们不是问题。 When I run your code and click, the red rectangle appears (along with a dialog). 当我运行您的代码并单击时,将出现红色矩形(以及一个对话框)。 So I'm not sure what is the problem??? 所以我不确定是什么问题???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM