Java 在ActionListener之后缩放图像

Java 在ActionListener之后缩放图像,java,swing,actionlistener,paintcomponent,graphics2d,Java,Swing,Actionlistener,Paintcomponent,Graphics2d,我试图创建一个图像视图程序,但在Java Image的放大和缩小中遇到了一个问题:D 我创建了一个JPanel并使用BuffereImage在我的计算机中显示图像。单击按钮后,它应该是缩放。 但这里的问题是,我在JPanel中重载了方法paintComponent(),以显示我想要的图像。在谷歌搜索之后,我想我应该使用Graphic2D来处理这个问题。跟随岗位,排队 Graphics2D g = resizedImage.createGraphics(); g.drawImage(origina

我试图创建一个图像视图程序,但在Java Image的放大和缩小中遇到了一个问题:D

我创建了一个JPanel并使用BuffereImage在我的计算机中显示图像。单击按钮后,它应该是缩放。 但这里的问题是,我在JPanel中重载了方法paintComponent(),以显示我想要的图像。在谷歌搜索之后,我想我应该使用Graphic2D来处理这个问题。跟随岗位,排队

Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
应放在重载方法paintComponent()中。但是,在我的例子中,我想在单击按钮后缩放图像,那么,我如何访问paintComponent()来进行缩放

public class MiddlePanel extends JPanel {
private BufferedImage img;
private JButton jbtZoom = new JButton("Zoom");

public MiddlePanel(int width){            

    img = ImageIO.read(new FileInputStream(new File("C:\\Picture\\pic1.jpg")));

    this.setPreferredSize(new Dimension(800,460));        
}

public void paintComponent(Graphics g) {
    g.drawImage(img......);
}

public void addComponentActionListener(){
    jbtZoom.addActionListener(new ActionListener{
        public void actionPerformed(){
            //What should I do in here to zoom the image....
        }
    });
}

谢谢你的帮助

您需要这样更改您的设计:

  • 将缩放状态存储在变量中,然后 重写的paintComponent方法应该查看该变量以 决定是否缩放/缩放多少
  • ActionListener将更新变量,然后在上调用repaint() 专家组

您需要像这样更改您的设计:

  • 将缩放状态存储在变量中,然后 重写的paintComponent方法应该查看该变量以 决定是否缩放/缩放多少
  • ActionListener将更新变量,然后在上调用repaint() 专家组

您需要像这样更改您的设计:

  • 将缩放状态存储在变量中,然后 重写的paintComponent方法应该查看该变量以 决定是否缩放/缩放多少
  • ActionListener将更新变量,然后在上调用repaint() 专家组

您需要像这样更改您的设计:

  • 将缩放状态存储在变量中,然后 重写的paintComponent方法应该查看该变量以 决定是否缩放/缩放多少
  • ActionListener将更新变量,然后在上调用repaint() 专家组

public void paintComponent(Graphics g){g.drawImage(Graphics g);
应该是
public void paintComponent(Graphics g){super.paintComponent(g);g.drawImage(img);
@andrewhompson或简单地重写
public void paintComponent(Graphics g)
而不是
public void paintComponent(Graphics g)
@Vallentin不!这是在
JComponent中进行自定义绘制的错误方法
public void paintComponent(Graphics g){g.drawImage(img);
应该是
public void paintComponent(Graphics g g){super.paintComponent(g);g.drawImage(img)…)
@andrewhompson或直接覆盖
公共无效绘制组件(图形g)
而不是
公共无效绘制组件(图形g)
@Vallentin不!在
JComponent
公共无效绘制组件(图形g){g.drawImage(img……)中进行自定义绘制是错误的方法
应该是
public void paintComponent(Graphics g){super.paintComponent(g);g.drawImage(img);
@andrewhompson或简单地覆盖
public void paintComponent(Graphics g)
而不是
public void paintComponent(Graphics g)
@Vallentin不!这是在
JComponent中进行自定义绘制的错误方法
public void paintComponent(Graphics g){g.drawImage(img);
应该是
public void paintComponent(Graphics g g){super.paintComponent(g);g.drawImage(img)…)
@andrewhompson或直接覆盖
public void paint(Graphics g)
而不是
public void paintComponent(Graphics g)
@Vallentin否!在
JComponent
中进行自定义绘制是错误的。