2013-06-20 58 views
0

我有一个小图像库,可以使用JButtons“next”和“previous”查看。它工作正常。在JPanel上将图像打印到图像上

我想要做的是画一个矩形(只是框架,没有填充)在点击它们时显示的图像。例如,如果我点击点(230,150),我希望我的矩形以其左下角出现在该点上。

这是我的代码,我尝试过很多事情,但没有奏效:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.Rectangle2D; 
import java.awt.Image; 

public class rectOnGallery extends JFrame{ 

private static final long serialVersionUID = 1L; 

private ImageIcon myImage1; 
private ImageIcon myImage2; 
private ImageIcon myImage3; 
private ImageIcon myImage4; 
JPanel ImageGallery = new JPanel(); 
private ImageIcon[] myImages = new ImageIcon[4]; 
private int curImageIndex=0; 

int width; 
int height; 

public rectOnGallery(){ 

    double scale = 0.666667; 
    width = (int) (scale * 612); 
    height = (int) (scale * 792); 

    myImage1 = new ImageIcon(((new ImageIcon("NewSign1.jpg")).getImage()).getScaledInstance(width, height, Image.SCALE_SMOOTH)); 
    myImage2 = new ImageIcon(((new ImageIcon("pdf2.jpg")).getImage()).getScaledInstance(width, height, Image.SCALE_SMOOTH)); 
    myImage3 = new ImageIcon(((new ImageIcon("pdfimg.jpg")).getImage()).getScaledInstance(width, height, Image.SCALE_SMOOTH)); 
    myImage4 = new ImageIcon(((new ImageIcon("images.jpg")).getImage()).getScaledInstance(width, height, Image.SCALE_SMOOTH)); 

    ImageGallery.add(new JLabel (myImage1)); 
    myImages[0]=myImage1; 
    myImages[1]=myImage2; 
    myImages[2]=myImage3; 
    myImages[3]=myImage4; 


    ImageGallery.addMouseListener(new MouseAdapter() { 

     public void mouseClicked(MouseEvent evt) { 
       if (myImages[curImageIndex] != null) { 
       double x = (getWidth() - width)/2; 
       double y = (getHeight() - height)/2; 
       Rectangle2D.Double bounds = new Rectangle2D.Double(x, y, width, height); 
       if (bounds.contains(evt.getPoint())) { 
        System.out.println("You clicked on " + evt.getX() + " x " + evt.getY()); 

       **** HERE GOES SOMETHING THAT WRITES THE FRAME IN THE 
       **** POSITION (ent.getX() , evt.getY()) 

       } 
       } 
     } 
    }); 

    add(ImageGallery, BorderLayout.NORTH); 

    JButton PREVIOUS = new JButton ("Previous"); 
    JButton NEXT = new JButton ("Next"); 

    JPanel buttons = new JPanel(); 
    buttons.setLayout(new GridLayout(1,4)); 
    buttons.add(PREVIOUS); 
    buttons.add(NEXT); 

    add(buttons, BorderLayout.SOUTH); 

    //register listener 
    PreviousButtonListener PreviousButton = new PreviousButtonListener(); 
    NextButtonListener NextButton = new NextButtonListener(); 

    //add listeners to corresponding componenets 
    PREVIOUS.addActionListener(PreviousButton); 
    NEXT.addActionListener(NextButton); 
} 

private class PreviousButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e){ 

     if(curImageIndex>0 && curImageIndex <= 3){    
      ImageGallery.remove(0); 
      curImageIndex -- ; 
      ImageIcon TheImage= myImages[curImageIndex]; 
      ImageGallery.add(new JLabel (TheImage)); 
      ImageGallery.validate(); 
      ImageGallery.repaint(); 
     } 
     else{ 
      ImageGallery.remove(0); 
      ImageGallery.add(new JLabel (myImage1)); 
      curImageIndex=0; 
      ImageGallery.validate(); 
      ImageGallery.repaint(); 
     } 
    } 
} 

private class NextButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e){ 

     if(curImageIndex>=0 && curImageIndex < 3){ 
      ImageGallery.remove(0); 
      curImageIndex ++ ; 
      ImageIcon TheImage= myImages[curImageIndex]; 
      ImageGallery.add(new JLabel (TheImage)); 

      ImageGallery.validate(); 
      ImageGallery.repaint(); 
     } 
     else{ 
      ImageGallery.remove(0); 
      ImageGallery.add(new JLabel (myImage4)); 
      curImageIndex=3; 
      ImageGallery.validate(); 
      ImageGallery.repaint(); 
     } 
    } 
} 

public static void main (String [] args){ 

    provaPosFirma frame = new provaPosFirma(); 

    frame.setSize(500, 600); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
} 
} 

作为替代的事情我也tryed画的其他图像上的一个矩形的形象,但neighter这个工作。

我知道我做错了什么,应该更容易,但我没有通过这个。如果您可以帮助我在显示器上绘制矩形或其他矩形图像,那么无论如何都将欣赏它。

在此先感谢

+2

遵循标准的Java命名约定。变量名不应以大写字符开头。不要继续添加/删除组件。只需使用setIcon(...)方法更改显示的图像即可。 – camickr

+0

@camickr感谢您的提示。我知道setIcon(),但它完全改变了我的图像,不会在前面的 – Igr

回答

3

要做到这一点,你需要创建JPanel的自定义子类,和使用的,与其“ImageGallery。”然后,在该类中,重写paintComponent方法以绘制所需的矩形。所以代码看起来像这样。

class ImageGalleryPanel extends JPanel{ 
    private Point rectPosition = null; 
    private Dimension rectSize; //Set the size of the rect 

    public void setRectPosition(Point p){ 
     rectPosition = p; 
    } 

    private void drawRect(Graphics g){ 
     if(rectPosition != null){ 
      g.drawRect(rectPosition.x, rectPosition.y, rectSize.width, rectSize.height); 
     } 
    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     drawRect(g); 
    } 
} 

用这个类替换你的JPanel“ImageGallery”,并指定你要绘制的矩形的大小。当你有你的意见以绘制矩形,添加这些行 -

ImageGallery.setRectPosition(evt.getPoint()); 
ImageGallery.repaint(); 

我没有测试此代码,因此它可能有一些拼写错误,但这是绘制一个JPanel,当你想的基本理念。覆盖面板的绘画方法。然后,当您想要绘制它时,将其属性更改为它们应该包含的内容并调用repaint()。如果您只是试图直接绘制它,那么重绘方法将自动调用,覆盖您所做的任何更改。

编辑 - 为了避免通过矩形绘制图像,您可以执行一些操作。您可以将paintComponent方法更改为“paint”或“paintChildren”,并将super.paintComponent函数调用更改为匹配。这将是快速和容易的,但被认为是不好的做法。另一种我认为可行的方式是,不是覆盖JPanel的paintComponent,而是覆盖要添加到其中的JLabel。代码将完全相同,您只需将其应用于JLabel即可。

+0

上打印另一个图像。好吧,这一般工作得很好,谢谢,但它会在图像下打印矩形。我想要结束。这是什么原因?我在添加矩形之前显示图像... – Igr

+0

您的意思是图像在屏幕上方?如果将“g.drawRect(...)”行更改为“g.drawRect(rectPosition.x,rectPosition.y - rectSize.height,rectSize.width,rectSize.height)”,应该修复它。还是说,矩形是绘制的,然后图像被绘制,阻碍了一些矩形? – resueman

+0

第二个。矩形在框架上绘制,但在显示的图像下(如果我在矩形的一部分附近添加图像) – Igr