Java 如何将同一类中的二维图形和图像分别绘制到同一个JPanel
我有一个方法(drawImages),它将BuffereImage的arraylist绘制到扩展的JPanel。但是,只有在调用另一个方法(drawPerfectRect)时,才会在paintComponent方法中调用它 此方法(drawPerfectRect)负责使用传递给它的坐标,使用用户单击的点绘制矩形 基本上,我面临的两个问题是:Java 如何将同一类中的二维图形和图像分别绘制到同一个JPanel,java,swing,graphics2d,Java,Swing,Graphics2d,我有一个方法(drawImages),它将BuffereImage的arraylist绘制到扩展的JPanel。但是,只有在调用另一个方法(drawPerfectRect)时,才会在paintComponent方法中调用它 此方法(drawPerfectRect)负责使用传递给它的坐标,使用用户单击的点绘制矩形 基本上,我面临的两个问题是: 它不会绘制BuffereImage的arraylist,直到用户单击并拖动以创建一个矩形(希望在单击selectDirectory(JButton)后在处绘
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import com.sun.tools.internal.ws.wsdl.document.Import;
import net.coobird.thumbnailator.*;
import com.mortennobel.imagescaling.*;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.Timer;
public class ImportWorkspace extends JPanel{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x, y, x2, y2;
ArrayList<BufferedImage> thumbnailList = new ArrayList<BufferedImage>();
int ContentPanelWidth;
private boolean addThumbnails = false;
public ImportWorkspace(){
x = y = x2 = y2 = 0;
setLayout(null);
setBackground(Color.decode("#2a2e37"));
setBounds(85, 0, screenSize.width-85, screenSize.height);
//System.out.println("W: " + super.getWidth() + ", H: " + super.getHeight());
ContentPanelWidth = getWidth();
System.out.println(ContentPanelWidth);
JLabel dataIcon = new JLabel(new ImageIcon(new ImageIcon ("folder.png").getImage().getScaledInstance(256,256, Image.SCALE_DEFAULT)));
dataIcon.setBounds((getWidth()/2)-128, (getHeight()/2)-200, 256, 256);
add(dataIcon);
JLabel askImport = new JLabel("No Data Files have been selected: To begin importing data please select a directory.");
askImport.setFont(new Font("Helvetica", Font.PLAIN, 20));
askImport.setForeground(Color.white);
askImport.setBounds((getWidth()/2)-375, (getHeight()/2)+50, 750, 100);
add(askImport);
JButton selectDirectory = new JButton("Select Directory");
selectDirectory.setBounds((getWidth()/2)-75, (getHeight()/2)+150, 150, 50); //+half of width or height
add(selectDirectory);
selectDirectory.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeAll();
revalidate();
repaint();
setLayout(new FlowLayout(FlowLayout.LEFT));
ImportingImages getImages = new ImportingImages();
getImages.importFiles();
File curDir = new File("");
File[] files = curDir.listFiles();
long noFiles = curDir.length();
for (File f : files) {
String fileName = f.getName();
String hiddenFile = ".DS_Store";
if (fileName.equals(hiddenFile)){
//System.out.println("Do nothing");
} else {
String thumbnailPath = curDir + "/" + f.getName();
try {
BufferedImage thumbnailIcon = ImageIO.read(new File(thumbnailPath));
thumbnailList.add(thumbnailIcon);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
//DisplayImages(thumbnailList, ContentPanelWidth);
}
});
}
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x2 = (x);
y2 = (y);
}
public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
int px = Math.min(x,x2);
int py = Math.min(y,y2);
int pw = Math.abs(x-x2);
int ph = Math.abs(y-y2);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int alpha = 100; // 127 50% transparent
Color cyanTransparent = new Color(0,206,209, alpha);
g2.setColor(cyanTransparent);
g2.fillRoundRect(px, py, pw, ph, 5, 5);
g2.setColor(Color.cyan);
g2.setStroke(new BasicStroke(1));
g2.drawRoundRect(px, py, pw, ph, 5, 5);
}
public void drawImages(Graphics g){
int xSpacing = 10;
int ySpacing = 20;
int noImages = 0;
for (BufferedImage thumbnail : thumbnailList){
g.drawImage(thumbnail, xSpacing, ySpacing,null );
if ((xSpacing+100) > (ContentPanelWidth-100)){
ySpacing = ySpacing + 77;
xSpacing = 10;
} else{
xSpacing = xSpacing + 110;
}
noImages = noImages + 1;
//System.out.println(noImages);
}
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
revalidate();
repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
revalidate();
repaint();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
System.out.println(addThumbnails);
drawImages(g2);
drawPerfectRect(g2, x, y, x2, y2);
}
}
导入javax.imageio.imageio;
导入javax.swing.*;
导入javax.swing.border.border;
导入java.awt.*;
导入java.awt.event.*;
导入com.sun.tools.internal.ws.wsdl.document.import;
导入net.coobird.thumbnailator.*;
导入com.mortennobel.imagescaling.*;
导入java.awt.Graphics2D;
导入java.awt.Graphics;
导入java.awt.geom.RoundRectangle2D;
导入java.io.IOException;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.nio.Buffer;
导入java.util.ArrayList;
导入java.util.Timer;
公共类ImportWorkspace扩展了JPanel{
维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int x,y,x2,y2;
ArrayList thumbnailList=新建ArrayList();
int-ContentPanelWidth;
私有布尔addThumbnails=false;
公共ImportWorkspace(){
x=y=x2=y2=0;
setLayout(空);
挫折背景(彩色解码(#2a2e37”);
立根(85,0,屏幕大小.宽度-85,屏幕大小.高度);
//System.out.println(“W:+super.getWidth()+”,H:+super.getHeight());
ContentPanelWidth=getWidth();
System.out.println(ContentPanelWidth);
JLabel dataIcon=new JLabel(新图像图标(新图像图标(“folder.png”).getImage().getScaledInstance(256256,Image.SCALE_默认值));
setBounds((getWidth()/2)-128,(getHeight()/2)-20025256);
添加(数据图标);
JLabel askImport=new JLabel(“未选择任何数据文件:若要开始导入数据,请选择一个目录”);
setFont(新字体(“Helvetica”,Font.PLAIN,20));
askImport.setForeground(颜色:白色);
askImport.setBounds((getWidth()/2)-375,(getHeight()/2)+50750100);
添加(askImport);
JButton selectDirectory=新JButton(“选择目录”);
选择directory.setBounds((getWidth()/2)-75,(getHeight()/2)+150、150、50);///+宽度或高度的一半
添加(选择目录);
selectDirectory.addActionListener(新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
removeAll();
重新验证();
重新油漆();
setLayout(新的FlowLayout(FlowLayout.LEFT));
ImportingImages getImages=新建ImportingImages();
getImages.importFiles();
文件curDir=新文件(“”);
File[]files=curDir.listFiles();
long noFiles=curDir.length();
用于(文件f:文件){
字符串文件名=f.getName();
字符串hiddenFile=“.DS_Store”;
if(fileName.equals(hiddenFile)){
//System.out.println(“不做任何事”);
}否则{
字符串thumbnailPath=curDir+“/”+f.getName();
试一试{
BuffereImage thumbnailIcon=ImageIO.read(新文件(thumbnailPath));
缩略图列表。添加(缩略图图标);
}捕获(IOEX异常){
例如printStackTrace();
}
}
}
MyMouseListener侦听器=新建MyMouseListener();
addMouseListener(监听器);
addMouseMotionListener(listener);
//显示图像(缩略图列表、ContentPanelWidth);
}
});
}
公共无效设置起点(整数x,整数y){
这个.x=x;
这个。y=y;
}
公共void setEndPoint(int x,int y){
x2=(x);
y2=(y);
}
公共void drawPerfectRect(图形g、整数x、整数y、整数x2、整数y2){
int px=数学最小值(x,x2);
int py=数学最小值(y,y2);
int pw=数学绝对值(x-x2);
int ph=数学绝对值(y-y2);
图形2d g2=(图形2d)g;
g2.setRenderingHint(renderingHits.KEY\u ANTIALIASING,renderingHits.VALUE\u ANTIALIAS\u ON);
int alpha=100;//127 50%透明
颜色青色透明=新颜色(0206209,alpha);
g2.setColor(青色透明);
g2.fillRoundRect(px,py,pw,ph,5,5);
g2.setColor(颜色为青色);
g2.设定行程(新基本行程(1));
g2.drawRoundRect(px,py,pw,ph,5,5);
}
公共图像(图形g){
int xSpacing=10;
int=20;
int noImages=0;
用于(BuffereImage缩略图:缩略图列表){
g、 drawImage(缩略图、XSpace、YSpace、null);
如果((xspace+100)>(ContentPanelWidth-100)){
YSPACE=YSPACE+77;
xSpacing=10;
}否则{
xspacking=xspacking+110;
}
noImages=noImages+1;
//系统输出打印项次(noImages);
}
}
类MyMouseListener扩展了MouseAdapter{
公共无效鼠标按下(MouseEvent e){
设置起点(e.getX(),e.getY());
}
公共无效鼠标标记(鼠标事件e){
setEndPoint(e.getX(),e.getY());
public ImportWorkspace(){
x = y = x2 = y2 = 0;
setLayout(null);
setBackground(Color.decode("#2a2e37"));
setBounds(85, 0, screenSize.width-85, screenSize.height);
//System.out.println("W: " + super.getWidth() + ", H: " + super.getHeight());
ContentPanelWidth = getWidth();
System.out.println(ContentPanelWidth);
JLabel dataIcon = new JLabel(new ImageIcon(new ImageIcon ("folder.png").getImage().getScaledInstance(256,256, Image.SCALE_DEFAULT)));
dataIcon.setBounds((getWidth()/2)-128, (getHeight()/2)-200, 256, 256);
add(dataIcon);
JLabel askImport = new JLabel("No Data Files have been selected: To begin importing data please select a directory.");
askImport.setFont(new Font("Helvetica", Font.PLAIN, 20));
askImport.setForeground(Color.white);
askImport.setBounds((getWidth()/2)-375, (getHeight()/2)+50, 750, 100);
add(askImport);
JButton selectDirectory = new JButton("Select Directory");
selectDirectory.setBounds((getWidth()/2)-75, (getHeight()/2)+150, 150, 50); //+half of width or height
add(selectDirectory);
selectDirectory.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeAll();
revalidate();
//repaint(); //Removed this line. It is not necessary in this place. Will be re-added AFTER the `for` loop below...
thumbnailList.clear(); //Added this line.
setLayout(new FlowLayout(FlowLayout.LEFT));
ImportingImages getImages = new ImportingImages();
getImages.importFiles();
File curDir = new File("");
File[] files = curDir.listFiles();
long noFiles = curDir.length();
for (File f : files) {
String fileName = f.getName();
String hiddenFile = ".DS_Store";
if (fileName.equals(hiddenFile)){
//System.out.println("Do nothing");
} else {
String thumbnailPath = curDir + "/" + f.getName();
try {
BufferedImage thumbnailIcon = ImageIO.read(new File(thumbnailPath));
thumbnailList.add(thumbnailIcon);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
repaint(); //Added this line! Now the newly loaded images shall be painted.
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
//DisplayImages(thumbnailList, ContentPanelWidth);
}
});
}