我正在尝试使用以下代码将Icon(javax.swing.Icon)转换为Image(java.awt.Image):
private Image iconToImage(Icon icon)
{
if(icon instanceof ImageIcon)
{
return ((ImageIcon) icon).getImage();
}
else
{
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
icon.paintIcon(null, image.getGraphics(), 0, 0);
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,该paintIcon函数抛出一个NullPointerException上image.getGraphics().
对于记录,该icon值是默认CheckBox图标(通过获取UIManager.getIcon("CheckBox.icon"))
以下是抛出异常的详细信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sun.java.swing.plaf.windows.WindowsIconFactory$CheckBoxIcon.paintIcon(WindowsIconFactory.java:306)
at utils.WarningRenderer.iconToImage(WarningRenderer.java:50)
at utils.WarningRenderer.<init>(WarningRenderer.java:38)
at deliveryexpress.DeliveryExpressView.setWarnings(DeliveryExpressView.java:278)
at deliveryexpress.DeliveryExpressView.updateLists(DeliveryExpressView.java:218)
at deliveryexpress.DeliveryExpressView.access$1100(DeliveryExpressView.java:47)
at deliveryexpress.DeliveryExpressView$5.addCheck(DeliveryExpressView.java:183)
at org.japura.gui.model.DefaultListCheckModel.fireCheckListModelListeners(Unknown Source)
at org.japura.gui.model.DefaultListCheckModel.fireAddCheckListModelListeners(Unknown Source)
at org.japura.gui.model.DefaultListCheckModel.addCheck(Unknown Source)
at org.japura.gui.CheckList$1.mouseClicked(Unknown …Run Code Online (Sandbox Code Playgroud) 我想知道在Swing中是否有某种方法可以将ImageIcon转换为灰度,如下所示:
component.setIcon(greyed(imageIcon));
Run Code Online (Sandbox Code Playgroud) 在Java中是否有一种快速的方法来替换某些颜色的位图中的所有实例与其他颜色?
我正在使用的图像是一个非常大的5616 x 2160 24位非透明无索引位图,尽管此位图的像素值会有所不同.
这是我目前正在使用的代码,但它太慢了:http: //pastebin.com/UjgwgB0V
public class DisplayImage extends JFrame {
public DisplayImage(boolean resize, boolean mapCountries) throws IOException {
super("Province Map");
File mapProvinceFile = new File("map\\provinces.bmp");
BufferedImage mapProvinceImage = ImageIO.read(mapProvinceFile);
byte[] pixels = (byte[])mapProvinceImage.getRaster().getDataElements(0, 0, mapProvinceImage.getWidth(), mapProvinceImage.getHeight(), null);
if (mapCountries) {
for (int i = 0; i < Victoria2Stats.provinceDefinitionArray.size(); i++) {
for (int p = 0; p < pixels.length-3; p = p + 3) {
if ((byte)Victoria2Stats.provinceDefinitionArray.get(i).rgb[0] == pixels[p]) {
if ((byte)Victoria2Stats.provinceDefinitionArray.get(i).rgb[1] == pixels[p+1]) {
if …Run Code Online (Sandbox Code Playgroud) 在(嵌入式)Ubuntu 12.04系统上,我们有一个简单的Java程序,它在窗口上显示一些图形模式,每秒钟更新一次.我们用它来监控系统上运行的一些进程.问题在于,虽然它是活动的而不是最小化的,但只要窗口更新,它就会窃取焦点.这使得无法使用开放式终端窗口.
运行应用程序表单命令行或从Eclipse IDE时,行为是相同的.
在NetBeans IDE下运行时,Windows 7上不会出现同样的问题.
我们如何防止Java应用程序将重点放在Ubuntu机器上?
更新1:发现这个问题似乎很难解决同样的问题: 如何停止/解决Java应用程序窃取Linux窗口管理器的焦点.阅读它,我了解到问题是使用JFrame作为容器,这是我们使用的.他们的解决方案是使用JWindow容器而不是JFrame容器.然而,寻找差异,JWindow是"赤裸裸",并没有表现得像一个"真正的"窗口,因为没有装饰.有没有办法在 JFrame中使用JWindow ,从而消除焦点窃取?
更新2:尝试在PC上的Ubuntu虚拟机上运行此程序会产生同样的错误行为.这表明Windows 7和Linux的Java运行时存在差异,并且该问题并非特定于嵌入式Linux.
更新3:这是一个SSCCE:
//package SSCCE;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class MonitorSSCCE extends JFrame{
public static void main(String[] args)
{
// Set the frame
JFrame ecoreFrame = new JFrame("SSCCE");
ecoreFrame.setSize(120, 120);
ecoreFrame.setVisible(true);
// Refresh frame every 200 msec
while (true) {
GRFX grfx = new GRFX();
ecoreFrame.add(grfx);
ecoreFrame.setVisible(true);
grfx = null;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
} …Run Code Online (Sandbox Code Playgroud) 我正在为一个项目做一台老虎机.我无法让我JButton从我的生成新的随机数ArrayList.我可以在程序启动和actionlistener设置时随机化数字,但它不能满足我的需要.它仅用于测试目的.
我Actionlistener在一个不同的Java文件中.一切正常,我只是无法弄清楚如何在占位符产生新的随机量plc1,plc2以及plc3按钮被按下时.
我刚刚开始在3周前开始编码.请不要讨厌,这是我的第一个项目.
package GGCGuiLotto;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Random;
public class GGCGuiLotto {
public static void main(String[] args) {
//Arraylist of images
ImageIcon pic0 = new ImageIcon("pics/pic1.png");
ImageIcon pic1 = new ImageIcon("pics/pic2.png");
ImageIcon pic2 = new ImageIcon("pics/pic3.png");
ImageIcon …Run Code Online (Sandbox Code Playgroud) 我需要根据 nio ShortBuffer 中的数据创建灰度图像。我有一个函数,可以将 ShortBuffer 中的数据映射到无符号字节,但采用 int 形式(很容易更改)。我发现的方法使用 RGB 加透明度颜色模型,看起来效率很低。我无法了解如何应用 TYPE_BYTE_GRAY 并修改代码。我是 Java 新手。这是我的代码:
public void paintComponent(Graphics g) {
final BufferedImage image;
int[] iArray = {0, 0, 0, 255}; // pixel
image = (BufferedImage) createImage(WIDTH, HEIGHT);
WritableRaster raster = image.getRaster();
sBuf.rewind(); // nio ShortBuffer
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0; col < WIDTH; col++) {
int v = stats.mapPix(sBuf.get()); // map short to byte
iArray[0] = v; // RGBT
iArray[1] = …Run Code Online (Sandbox Code Playgroud) 嗨,我正在研究一个项目(Java记忆游戏),首先我想了解摇摆计时器是如何工作的.首先,我的主要课程实现ActionListener和ItemListener.我使用计时器,actionPerformed(ActionEvent e)如果用户选择不同图片的两张卡然后我用timer.start()它给他几秒钟看图片,然后他们将再次关闭.但如果用户选择两张不同的图片,他们会突然关闭,所以我看不到第二张图片.我读了一些关于摇摆计时器的教程,但我想我的理解是错误的.通过我创建我的SSCCE的方式,如果你能帮助我,我将不胜感激.谢谢anwyway ...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.*;
public class ConcentrationGame4 extends JFrame implements ActionListener, ItemListener{
private static final long serialVersionUID = 1L;
private int buttoncounter=0;
private int counter = 0;
private JFrame …Run Code Online (Sandbox Code Playgroud) 我已经实现了这个算法,使用鼠标点击创建一个形状,然后你可以使用边界填充算法填充形状....只有部分形状填充,然后我得到这个错误:
线程"AWT-EventQueue-0"中的异常java.lang.StackOverflowError java.util.HashMap.getEntry(未知来源)java.util.HashMap.get(未知来源)at sun.awt.AppContext.get(未知来源)来自位于javax.swing.RepaintManager的一个javax.swing.RepaintManager.getDelegate(未知来源)的com.sun.java.swing.SwingUtilities3.getDelegateRepaintManager(未知来源)位于javax.swing.JComponent.repaint的javax.swing.RepaintManager.addDirtyRegion(未知来源)来自java.awt.Component.repaint的未知来源(未知来源)
有什么想法错了吗?这是我使用的边界填充算法....
public void BoundaryFill(int x, int y, Color bColor, Color fColor){
int current = bI.getRGB(x, y);
if((current != bColor.getRGB()) && (current != fColor.getRGB())){
//bI.setRGB(x, y, fColor.getRGB());
bI.setRGB(x, y, fColor.getRGB());
repaint();
BoundaryFill(x+1, y, bColor, fColor);
BoundaryFill(x-1, y, bColor, fColor);
BoundaryFill(x, y-1, bColor, fColor);
BoundaryFill(x, y+1, bColor, fColor);
}
else
return;
}
Run Code Online (Sandbox Code Playgroud)
请注意,x和y参数是单击鼠标并进行填充的坐标....
我用java创建了一个滑动拼图程序.然后,我继续实现滑动拼图求解程序.问题是当我点击"播放结果"按钮时,程序将自动将有效方块滑动到目标状态,如n-puzzle-solver.appspot.com.我使用JButtons创建了这个游戏,其中包含这样的二维数组(btnTiles [0] [0]).我可以用多线程实现吗?如果可以的话,请教我一个例子,或者给我一个自动移动JButton的多线程链接.