[英]Print rects onto an image on a JPanel
我有一个小的图片库,可以使用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);
}
}
作为替代,我也尝试将矩形的图像绘制在其他图像上,但加倍处理。
我知道我在做错事,应该使appers容易些,但我没有解决这个问题。 如果您可以帮助我在显示屏上绘制rect或其他矩形图像,无论如何将不胜感激。
提前致谢
为此,您需要创建一个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()。 如果您只是尝试直接在其上绘制,则repaint方法将被自动调用,并覆盖您所做的任何更改。
编辑-为了避免图像在矩形上绘制,您可以做一些事情。 您可以将paintComponent方法更改为“ paint”或“ paintChildren”,并将super.paintComponent函数调用更改为匹配。 这将是快速且容易的,但是被认为是不好的做法。 我认为可行的另一种方法是,不要覆盖JPanel的paintComponent,而是覆盖要添加到它的JLabel。 代码完全相同,您只需将其应用于JLabel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.