我有一个小图片库,可以使用 JButtons“下一个”和“上一个”查看。它工作正常。
我想要做的是在单击它们时显示的图像上绘制一个矩形(只是框架,没有填充)。例如,如果我单击点 (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);
}
}
作为替代方法,我还尝试在另一张图像上绘制矩形图像,但效果更佳。
我知道我做错了什么,这应该更容易出现,但我没有解决这个问题。如果您能帮我在显示器上绘制矩形或其他矩形图像,无论如何都会感激不尽。
提前致谢