gpt4 book ai didi

java - 将 Graphics2D 转换为 ImageIcon

转载 作者:行者123 更新时间:2023-11-29 03:10:33 24 4
gpt4 key购买 nike

根据先前提出的问题 Java Swing selecting a panel ,我正在尝试将示例中显示的图标更改为小型化的 Graphics2D 对象。

我的程序将有几个面板,每个面板中都有一个 Graphics2D 绘图,单击面板时,附近会出现一个放大的 View (也如其他问题答案中的示例所示。

我现在的问题是从每个面板中的 Graphics2D 绘图生成一个小图标。目前,我在创建和使用图标时遇到问题。

public class BiomorphPanel extends JPanel{

PointCalc pc = new PointCalc();
private JLabel label = new JLabel();
private ImageIcon icon;

public JLabel getLabel() {
return label;
}

public BiomorphPanel(){
this.add(label);
}

private void doDrawing(Graphics g){

Graphics2D g2d = (Graphics2D) g;
//g2d.scale(0.1, 0.1);
BufferedImage img = new BufferedImage(31, 31, BufferedImage.TYPE_INT_RGB);
Graphics2D g2dIcon = (Graphics2D) img.getGraphics();

drawAll(g2d);
drawAll(g2dIcon);

g2dIcon.setColor(Color.BLACK);
g2dIcon.fillRect(0, 0, 31, 31);
g2dIcon.drawImage(img, null, 0, 0);
icon = new ImageIcon(img);
//icon = new ImageIcon(gra);
}

@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//scaling and translating to fit into mini panels in GUI
// g2.scale(0.15, 0.15);
//g2.translate(-100.0, 0.0);
super.paintComponent(g2);
doDrawing(g2);
}

public ImageIcon getIcon(){
return icon;
}

public void drawAll(Graphics2D g2d){
//draw head
Polygon hPoints = pc.getHeadPoints();
g2d.drawPolygon(hPoints);

//draw eyes
Pair<Polygon, Polygon> ePoints = pc.getAllEyePoints();
g2d.drawPolygon(ePoints.x);
g2d.drawPolygon(ePoints.y);

//draw eyebrows
Pair<HashMap<Integer, Pair<Integer, Integer>>, HashMap<Integer, Pair<Integer, Integer>>> ebPoints = pc.getEyebrowPoints();
g2d.drawLine(ebPoints.x.get(0).x, ebPoints.x.get(0).y, ebPoints.x.get(1).x, ebPoints.x.get(1).y);
g2d.drawLine(ebPoints.y.get(0).x, ebPoints.y.get(0).y, ebPoints.y.get(1).x, ebPoints.y.get(1).y);

//draw nose
HashMap<Integer, Pair<Integer, Integer>> nPoints = pc.getNosePoints();
g2d.drawLine(nPoints.get(0).x, nPoints.get(0).y, nPoints.get(1).x, nPoints.get(1).y);
g2d.drawLine(nPoints.get(1).x, nPoints.get(1).y, nPoints.get(2).x, nPoints.get(2).y);

//draw mouth
Pair<HashMap<Integer, Pair<Integer, Integer>>, HashMap<Integer, Pair<Integer, Integer>>> mPoints = pc.getMouthPoints();
g2d.drawLine(mPoints.x.get(0).x, mPoints.x.get(0).y, mPoints.x.get(1).x, mPoints.x.get(1).y);
g2d.drawLine(mPoints.x.get(1).x, mPoints.x.get(1).y, mPoints.x.get(2).x, mPoints.x.get(2).y);

g2d.drawLine(mPoints.y.get(0).x, mPoints.y.get(0).y, mPoints.y.get(1).x, mPoints.y.get(1).y);
g2d.drawLine(mPoints.y.get(1).x, mPoints.y.get(1).y, mPoints.y.get(2).x, mPoints.y.get(2).y);

}

}

上面是我尝试创建 ImageIcon 的地方。

public class ListDisplayPanel extends Component{

//private static final Icon icon = UIManager.getIcon("html.pendingImage");

private ListPanel listPanel = new ListPanel();
private BiomorphPanel displayPanel1 = new BiomorphPanel();
private BiomorphPanel displayPanel2 = new BiomorphPanel();
private Icon icon = displayPanel1.getIcon();
private JPanel dpHold = new JPanel();

private class ListPanel extends JPanel {

private static final int N = 5;
private DefaultListModel dlm = new DefaultListModel();
private JList list = new JList(dlm);

public ListPanel() {
super(new GridLayout());
for (int i = 0; i < N * N; i++) {
String name = "Cell-" + String.format("%02d", i);
dlm.addElement(name);
}
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(N);
list.setCellRenderer(new ListRenderer());
list.addListSelectionListener(new SelectionHandler());
this.add(list);
}

private class ListRenderer extends DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
label.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
label.setIcon(icon);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
return label;
}
}

private class SelectionHandler implements ListSelectionListener {

@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
displayPanel1.getLabel().setText((String) dlm.getElementAt(e.getLastIndex()));
displayPanel2.getLabel().setText((String) dlm.getElementAt(e.getLastIndex()));
}
}
}
}

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane jsp = new JSplitPane();
jsp.setLeftComponent(new JScrollPane(listPanel));
JSplitPane jsp2 = new JSplitPane();
jsp2.setTopComponent(displayPanel1);
jsp2.setBottomComponent(displayPanel2);
jsp.setRightComponent(jsp2);
f.add(jsp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
/*EventQueue.invokeLater(() ->*/ {
new ListDisplayPanel().display();
};
}
}

这是我尝试使用创建的图标的地方。当我运行类(class)时,菜单中的每个“单元格”都没有图标出现。图标创建不正确还是使用不正确?我说不出来。

谢谢

最佳答案

关键是创建一个实现 Icon 的类界面。覆盖图标的 paintIcon()方法,它接收合适的图形上下文,并在那里进行渲染。使用 Component 参数检索属性,使用 xy 参数利用布局几何。查看完整示例here ;可以找到更多 here .

image

关于java - 将 Graphics2D 转换为 ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29610195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com