I'm new to Stackoverflow and tried to follow the rules as much as possible. I haven't been able to solve this problem for hours. When I start the program it seldom starts strangely:
properly started: correct image:
not properly started: white screen:
If you see an error in the code and help me fix it I would appreciate it. any correction
public class MainClass extends JPanel implements Runnable, MouseListener {
enum MenuState {
xMenu1, xMenu2;
}
private MenuState menuState = MenuState.xMenu1;
JFrame frame;
private Image bgImage, playButtonImage, backButtonImage;
private Rectangle playButton, backButton;
public MainClass() {
frame = new JFrame("TEST");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.BLACK);
frame.setFocusable(true);
frame.addMouseListener(this);
frame.add(this);
try {
bgImage = ImageIO.read(getClass().getResourceAsStream("/Data/background.png"));
playButtonImage = ImageIO.read(getClass().getResource("/Data/playButton2.png"));
backButtonImage = ImageIO.read(getClass().getResource("/Data/backButton.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
playButton = new Rectangle(900, 400, 214, 78);
backButton = new Rectangle(5, 5, 136, 92);
Thread thread = new Thread(this);
thread.start();
System.out.println("thread started");
}
public static void main(String[] args) {
new MainClass();
}
#Override
public void run() {
while (true) {
System.out.println("run method started");
if (menuState == menuState.xMenu1) {
} else if (menuState == menuState.xMenu2) {
}
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void paint(Graphics g) {
if (menuState == menuState.xMenu1) {
g.drawImage(playButtonImage, playButton.x, playButton.y, this); // THIS IS THE 77th LINE
} else if (menuState == menuState.xMenu2) {
g.drawImage(bgImage, 0, 0, this);
g.drawImage(backButtonImage, backButton.x, backButton.y, this);
}
}
#Override
public void mousePressed(MouseEvent e) {
if (menuState == menuState.xMenu1) {
if (playButton.intersects(e.getX(), e.getY(), 1, 1)) {
menuState = menuState.xMenu2;
}
} else if (menuState == menuState.xMenu2) {
if (backButton.intersects(e.getX(), e.getY(), 1, 1)) {
menuState = menuState.xMenu1;
}
}
}
}
Related
I'm trying to synchronize this code: what I want is that the class Gioca waits until the class Gioco calls the method fine (fine should stop the thread) but as the Gioca class invokes the run method it prints on the console the string "Fine" even thow the class Gioco hasn't called the method fine() yet.
public class Gioca implements Runnable
{
private int vite;
private int recuperi;
public Gioca()
{
vite=3;
recuperi=0;
}
public void gioca()
{
Thread t=new Thread(new Gioco(vite));
try
{
t.start();
t.join();
}
catch (Exception ex) {}
System.out.println("Fine");
}
#Override
public void run()
{
gioca();
}
}
public class Gioco extends Canvas implements ActionListener, KeyListener, Runnable
{
private int direzione;
private Timer timer;
private JFrame f;
private int vite;
private int velocità;
private int spazio;
private Personaggio p;
private int pos;
private LinkedList<Ostacolo> o;
private Random r;
private int po;
private Image imm1=new ImageIcon(this.getClass().getResource("images/sfondo.jpg")).getImage();
private Image imm2=new ImageIcon(this.getClass().getResource("images/cuore.png")).getImage();
public Gioco(int vite)
{
r=new Random();
try
{
File file=new File("images/punteggio.txt");
Scanner scanner=new Scanner(file);
spazio=scanner.nextInt();
}
catch (Exception e) {}
direzione=3;
this.vite=vite;
o=new LinkedList();
for(int i=0; i<20; i++)
o.add(new Ostacolo(Math.abs(400*i)+1000));
p=new Personaggio();
this.velocità=2;
timer=new Timer(10, this);
f=new JFrame("Gioco");
f.setSize(1000, 700);
f.setResizable(false);
f.setLocation(200,200);
f.add(this);
f.addKeyListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(direzione==2)
{
velocità-=2;
if(velocità<2)
velocità=2;
}
if(direzione==1)
p.setY(5);
if(direzione==0)
p.setY(-5);
spazio+=velocità;
if(spazio%1000<10)
velocità++;
pos=(pos+velocità)%4500;
po=-pos;
for(int i=0; i<20; i++)
{
o.get(i).muovi(velocità);
if(o.get(i).getX()<-100)
{
o.remove(i);
o.add(new Ostacolo(i*400));
}
}
verificaCollisioni();
repaint();
}
public void verificaCollisioni()
{
for(int i=0; i<20; i++)
{
if(o.get(i).getX()>300 && o.get(i).getX()<350)
{
int r[]=o.get(i).getDimensioni();
if(r[0]<p.getY() && r[1]>p.getY())
{
}
else
fine();
}
}
}
private void fine()
{
try
{
Thread.sleep(3000);
}
catch(Exception e){}
timer.stop();
try
{
File file=new File("images/punteggio.txt");
file.createNewFile();
FileOutputStream f=new FileOutputStream(file);
f.flush();
String sPunteggio=String.valueOf(spazio);
byte[] scrivi=sPunteggio.getBytes();
f.write(scrivi);
}
catch(Exception e){}
f.dispose();
}
#Override
public void keyPressed(KeyEvent ke)
{
int c=ke.getKeyCode();
if(c == 40)
direzione=1;
if(c == 38)
direzione=0;
if(c==32)
direzione=2;
}
public void paint(Graphics g)
{
Image workspace=createImage(getWidth(),getHeight());
Graphics2D buffer=(Graphics2D) workspace.getGraphics();
buffer.drawImage(imm1, po, 0, this);
buffer.setColor(new Color(242, 54, 33));
buffer.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
buffer.drawString(""+(spazio/100), 10, 20);
buffer.drawImage(imm2, 940, 4, this);
buffer.setColor(new Color(13, 226, 13));
buffer.drawString(""+vite, 920, 20);
buffer.drawImage(p.getImage(), 300, p.getY(), this);
for(int i=0; i<20; i++)
{
Ostacolo tmp=o.get(i);
buffer.drawImage(tmp.getImage(), tmp.getX(),tmp.getY(), this);
}
Graphics2D g2=(Graphics2D)g;
g2.drawImage(workspace, 0, 0, this);
buffer.dispose();
}
public void update(Graphics g)
{
paint(g);
}
public void keyReleased(KeyEvent ke) {direzione=3;}
public void keyTyped(KeyEvent ke) {}
#Override
public void run()
{
f.setVisible(true);
timer.start();
}
}
This code, using the same instructions, works well
public class Campana implements Runnable{
private String suono;
private int volte;
public Campana(String suono,int volte)
{
this.suono =suono;
this.volte=volte;
}
public void run()
{
for(int i=0;i<volte;i++) {
System.out.println((i+1)+" "+suono);
}
}
}
public class Suona {
public static void main(String args[]){
Thread campana1=new Thread(new Campana("din", 5));
Thread campana2=new Thread(new Campana("don", 5));
Thread campana3=new Thread(new Campana("dan", 5));
try {
campana1.start();
campana1.join();
campana2.start();
campana2.join();
campana3.start();
campana3.join();
} catch (InterruptedException ex) {
Logger.getLogger(Suona.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
t.join(); in this case waits for run in Giocoto terminate. That method terminates after
f.setVisible(true);
timer.start();
have completed, which will be very fast since Timer will run actionPerformed a different thread from the one that timer.start(); is called in. It does not wait until the timer has been stopped. You can fix this by introducing some form of synchronization in your run method. I would not recommend a while loop since that will waste resources on running the loop. Instead consider using a CountDownLatch (javadoc link):
Add this to Gioco:
private final CountDownLatch doneSignal = new CountDownLatch(1);
At the end of fine() call doneSignal.countDown(). And finally change your run() method in Gioco to something like this:
#Override
public void run()
{
f.setVisible(true);
timer.start();
try {
doneSignal.await();
} catch (InterruptedException ex) {}//Logg this or something. Shouldn't really ever happen.
}
I would like to make my JToolBar impossible to detach from its container but still let the user drag it to one of the container's sides.
I know about
public void setFloatable( boolean b )
but this won't allow the user to move the JToolBar at all.
Is there any way of doing this without overwriting ToolBarUI?
Also, is there an option to highlight its new position before dropping it?
It's not the most elegant solution, but it works.
public class Example extends JFrame {
BasicToolBarUI ui;
Example() {
JToolBar tb = new JToolBar();
tb.add(new JButton("AAAAA"));
tb.setBackground(Color.GREEN);
ui = (BasicToolBarUI) tb.getUI();
getContentPane().addContainerListener(new Listener());
getContentPane().add(tb, BorderLayout.PAGE_START);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
class Listener implements ContainerListener {
#Override
public void componentAdded(ContainerEvent e) {}
#Override
public void componentRemoved(ContainerEvent e) {
if (ui.isFloating()) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ui.setFloating(false, null);
}
});
}
}
}
public static void main(String[] args) {
new Example();
}
}
Explanation:
Whenever the toolbar is moving to a floating state, it is instructed not do so. The only problem is that you have to wait for the EDT to finish the process for creating the floating window, and only then can you tell it not to float. The result is that you actually see the window created and then hidden.
Note:
I think that overriding the UI for the toolbar is a better solution, though it's possible that with a more intricate approach doing something similar to what I did will also work well.
works for me quite correctly on WinOS, old code from SunForum
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CaptiveToolBar {
private Robot robot;
private JDialog dialog;
private JFrame frame;
public static void main(String[] args) {
//JFrame.setDefaultLookAndFeelDecorated(true);
//JDialog.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CaptiveToolBar().makeUI();
}
});
}
public void makeUI() {
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
final JToolBar toolBar = new JToolBar();
for (int i = 0; i < 3; i++) {
toolBar.add(new JButton("" + i));
}
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.add(toolBar, BorderLayout.NORTH);
final ComponentListener dialogListener = new ComponentAdapter() {
#Override
public void componentMoved(ComponentEvent e) {
dialog = (JDialog) e.getSource();
setLocations(false);
}
};
toolBar.addHierarchyListener(new HierarchyListener() {
#Override
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(toolBar);
if (window instanceof JDialog) {
boolean listenerAdded = false;
for (ComponentListener listener : window.getComponentListeners()) {
if (listener == dialogListener) {
listenerAdded = true;
break;
}
}
if (!listenerAdded) {
window.addComponentListener(dialogListener);
}
}
}
});
frame.addComponentListener(new ComponentAdapter() {
#Override
public void componentMoved(ComponentEvent e) {
if (dialog != null && dialog.isShowing()) {
setLocations(true);
}
}
});
frame.setVisible(true);
}
private void setLocations(boolean moveDialog) {
int dialogX = dialog.getX();
int dialogY = dialog.getY();
int dialogW = dialog.getWidth();
int dialogH = dialog.getHeight();
int frameX = frame.getX();
int frameY = frame.getY();
int frameW = frame.getWidth();
int frameH = frame.getHeight();
boolean needToMove = false;
if (dialogX < frameX) {
dialogX = frameX;
needToMove = true;
}
if (dialogY < frameY) {
dialogY = frameY;
needToMove = true;
}
if (dialogX + dialogW > frameX + frameW) {
dialogX = frameX + frameW - dialogW;
needToMove = true;
}
if (dialogY + dialogH > frameY + frameH) {
dialogY = frameY + frameH - dialogH;
needToMove = true;
}
if (needToMove) {
if (!moveDialog && robot != null) {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
dialog.setLocation(dialogX, dialogY);
}
}
}
Is it possible to disable the drop shadow of a Java AWT application on OS X?
I want to create a transparent window, which works fine, but I cannot get rid of the drop shadow.
If I was using a JFrame this could be done using:
JRootPane root = frame.getRootPane();
root.putClientProperty( "Window.shadow", Boolean.FALSE );
Any similar possibility for an AWT Frame?
I' using the Framework Processing and my code there looks like this:
void setup(){
frame.removeNotify();
frame.setUndecorated(true);
}
Processing itself does the main Frame creation, here is the source.
This is a simple application that uses a transparent window running on Max OS X 10.7.5 under Java 7 (has run under Java 6) which has no problems...
Share some code so we can replicate the issue
Updated from feedback
I have tested this on Mac OS 10.7.5 & 10.8.2, using JDK 1.7.0_07 & 1.6.0_37
Without and with Window.shadow property...
Without going into a lot of tests and without further information, I would suggest you want to make this call as early as you can. If that doesn't work, make it the last call before you make the window visible.
This may be related to how Java/AWT connects to it's native peer, presumably, once the connection is made, you will no longer be able to effect these kinds of properties...
public class TransparentFrame {
public static void main(String[] args) {
new TransparentFrame();
}
public TransparentFrame() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
// Use this to test the transparentancy API
//doTransparentFrame();
doDropShadow();
}
});
}
protected void doDropShadow() {
String version = System.getProperty("java.version");
System.out.println(version);
JFrame frame = new JFrame("Testing");
JRootPane root = frame.getRootPane();
root.putClientProperty("Window.shadow", root);
frame.setUndecorated(true);
frame.setContentPane(new ContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected void doTransparentFrame() {
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setContentPane(new ContentPane());
String version = System.getProperty("java.version");
System.out.println(version);
if (version.startsWith("1.7")) {
frame.setBackground(new Color(0, 0, 0, 0));
} else if (version.startsWith("1.6")) {
if (supportsPerAlphaPixel()) {
setOpaque(frame, false);
} else {
System.out.println("Per Pixel Alphering is not support with Java " + version);
System.exit(1);
}
} else {
System.out.println("Per Pixel Alphering is not support with Java " + version);
System.exit(1);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static boolean supportsPerAlphaPixel() {
boolean support = false;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
}
} catch (Exception exp) {
}
}
public class ContentPane extends JPanel {
public ContentPane() {
setOpaque(false);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
}
public class ImagePane extends JPanel {
private BufferedImage background;
private BufferedImage offImage;
private Ellipse2D offButton;
private boolean mouseIn;
public ImagePane() {
setOpaque(false);
try {
background = ImageIO.read(new File("tamagotchi400.png"));
offImage = ImageIO.read(new File("powerSmall.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
offButton = new Ellipse2D.Float(212, 330, 25, 25);
MouseAdapter handler = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
if (offButton.contains(e.getPoint())) {
Window window = SwingUtilities.getWindowAncestor(ImagePane.this);
if (window != null) {
window.dispose();
}
}
}
}
#Override
public void mouseMoved(MouseEvent e) {
Cursor cursor = Cursor.getDefaultCursor();
if (offButton.contains(e.getPoint())) {
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
}
setCursor(cursor);
}
#Override
public void mouseEntered(MouseEvent e) {
mouseIn = true;
repaint();
}
#Override
public void mouseExited(MouseEvent e) {
mouseIn = false;
repaint();
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
}
#Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(400, 400) : new Dimension(background.getWidth(), background.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
if (mouseIn && offImage != null) {
g2d.drawImage(offImage, (int) offButton.getX(), (int) offButton.getY(), this);
}
g2d.dispose();
}
}
}
}
The code also includes transparency test code to test the transparency API now available in Java 1.7 and Java 1.6_10+. I've used this code successfully in a number of projects, its less cumbersome then the AWT Robot "hack" and provides a live back ground, but that's a choice you need to make.
Updated
Demonstration using java.awt.Frame
public class TestTransparentFrame {
public static void main(String[] args) {
new TestTransparentFrame();
}
public TestTransparentFrame() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exp) {
}
Frame frame = new Frame("Test");
frame.setUndecorated(true);
setOpaque(frame, false);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
frame.add(new ContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ContentPane extends JPanel {
private BufferedImage background;
public ContentPane() {
try {
background = ImageIO.read(new File("duke.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
setOpaque(false);
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.getWindowAncestor(ContentPane.this).dispose();
}
});
setBorder(new LineBorder(Color.RED));
setLayout(new GridBagLayout());
add(close);
}
#Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, this);
}
}
}
public static boolean supportsPerAlphaPixel() {
boolean support = false;
String version = System.getProperty("java.version");
if (version.startsWith("1.6")) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
} else if (version.startsWith("1.7")) {
try {
Class<?> winTransClass = Class.forName("java.awt.GraphicsDevice$WindowTranslucency");
Field field = winTransClass.getField("TRANSLUCENT");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
Method isWindowTranslucencySupported = GraphicsDevice.class.getMethod("isWindowTranslucencySupported", winTransClass);
System.out.println(isWindowTranslucencySupported);
Object value = isWindowTranslucencySupported.invoke(gd, field.get(null));
if (value instanceof Boolean) {
support = ((Boolean) value).booleanValue();
}
} catch (Exception exp) {
}
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
String version = System.getProperty("java.version");
if (version.startsWith("1.6")) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
}
} catch (Exception exp) {
}
} else if (version.startsWith("1.7")) {
Color color = UIManager.getColor("Panel.background");
if (opaque) {
color = new Color(color.getRed(), color.getGreen(), color.getBlue());
} else {
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
}
window.setBackground(color);
}
}
}
Tested on Windows 7 Java 1.6 & 1.7, Mac OS 10.7.5 & 10.8.2, using JDK 1.7.0_07 & 1.6.0_37
It appears that you don't understand the window hierarchy in Java
All "windows" in Java derive from java.awt.Window.
JFrame extends Frame which extends Window.
Using this line, it works:
AWTUtilities.setWindowOpaque(frame, false);
Eclipse prints out a warning on this and I had to change some settings to comile it, so I guess there may be better ways.
I read here, that this is supported since OS X 10.6 (Lion).
It seems that when your frame is not focus, the shadow effect will be triggered. Try adding this code so your frame will disabled being focus.
frame.setFocusableWindowState(false);
I came across a shadow problem with JInternalFrame on OSX today, and I found this solution for another forum (verified on Java 11, Catalina 10.15)
internalFrame.putClientProperty("JInternalFrame.frameType", "normal");
I'm making a screenshot program so when you press the screenshot button, a JMessageDial pops up and prompts you with your image, it the tells you to drag your mouse and make a box around the area that you want to take a screenshot of. I can't seem to find how to get the image inside the box when the user clicks okay.
So what I need help with is getting the image inside of the rectangle the user "draws" with his mouse, and store that in a variable.
Here is the specific code:
public void selectArea(final BufferedImage screen) throws Exception {
final BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
JScrollPane screenScroll = new JScrollPane(screenLabel);
screenScroll.setPreferredSize(new Dimension((int)(screen.getWidth()/2), (int)(screen.getHeight()/2)));
JPanel panel = new JPanel(new BorderLayout());
panel.add(screenScroll, BorderLayout.CENTER);
final JLabel selectionLabel = new JLabel("Draw a rectangle");
panel.add(selectionLabel, BorderLayout.SOUTH);
repaint(screen, screenCopy);
screenLabel.repaint();
screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseMoved(MouseEvent me) {
start = me.getPoint();
repaint(screen, screenCopy);
selectionLabel.setText("Start Point: " + start);
screenLabel.repaint();
}
#Override
public void mouseDragged(MouseEvent me) {
end = me.getPoint();
captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
try {
paintFinalImage(new BufferedImage((int)screenCopy.getGraphics().getClipBounds(captureRect).getWidth()+1,
(int)screenCopy.getGraphics().getClipBounds(captureRect).getHeight()+1, screen.getType()));
} catch (Exception e) {
e.printStackTrace();
}
repaint(screen, screenCopy);
screenLabel.repaint();
selectionLabel.setText("Rectangle: " + captureRect);
}
});
JOptionPane.showMessageDialog(null, panel, "Select your image", JOptionPane.OK_OPTION, new ImageIcon(""));
uploadImage(screenShot);
System.out.println("Final rectangle: " + screenCopy.getGraphics().getClipBounds(captureRect));
}
And this is the full class if you need it:
package com.screencapture;
import java.awt.*;
public class Main implements ActionListener, ItemListener, KeyListener, NativeKeyListener {
private final Image icon = Toolkit.getDefaultToolkit().getImage("./data/icon.gif");
private final TrayIcon trayIcon = new TrayIcon(icon, "Screen Snapper");
private JFrame frame;
private Rectangle captureRect;
private JComboBox<String> imageType;
private boolean hideWhenMinimized = false;
private final String API_KEY = "b84e430b4a65d16a6955358141f21a61";
private static Robot robot;
private BufferedImage screenShot;
private Point end;
private Point start;
public static void main(String[] args) throws Exception {
robot = new Robot();
new Main().start();
}
public void start() throws Exception {
GlobalScreen.getInstance().registerNativeHook();
GlobalScreen.getInstance().addNativeKeyListener(this);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
frame = new JFrame("White power!");
frame.setPreferredSize(new Dimension(250, 250));
frame.getContentPane().setLayout(null);
frame.setIconImage(icon);
frame.addKeyListener(this);
frame.addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent e) {}
#Override
public void windowClosing(WindowEvent e) {}
#Override
public void windowClosed(WindowEvent e) {}
#Override
public void windowIconified(WindowEvent e) {
if (hideWhenMinimized)
frame.setVisible(false);
}
#Override
public void windowDeiconified(WindowEvent e) {}
#Override
public void windowActivated(WindowEvent e) {}
#Override
public void windowDeactivated(WindowEvent e) {}
});
JLabel lblImageType = new JLabel("Image Format:");
lblImageType.setBounds(10, 11, 90, 14);
frame.getContentPane().add(lblImageType);
String[] imageTypes = {"PNG", "JPG"};
imageType = new JComboBox<String>(imageTypes);
imageType.setBounds(106, 8, 51, 20);
frame.getContentPane().add(imageType);
JButton btnTakeScreenShot = new JButton("ScreenShot");
btnTakeScreenShot.setBounds(24, 69, 115, 23);
frame.getContentPane().add(btnTakeScreenShot);
btnTakeScreenShot.addActionListener(this);
setTrayIcon();
frame.pack();
frame.setVisible(true);
}
public void setTrayIcon() throws AWTException {
CheckboxMenuItem startup = new CheckboxMenuItem("Run on start-up");
CheckboxMenuItem hide = new CheckboxMenuItem("Hide when minimized");
String[] popupMenuOptions = {"Open", "Exit"};
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
PopupMenu popupMenu = new PopupMenu();
for (String option : popupMenuOptions) {
if (option == "Exit") {
popupMenu.add(startup);
popupMenu.add(hide);
popupMenu.add(option);
} else {
popupMenu.add(option);
}
}
trayIcon.setPopupMenu(popupMenu);
popupMenu.addActionListener(this);
startup.addItemListener(this);
hide.addItemListener(this);
trayIcon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
}
});
tray.add(trayIcon);
}
}
public void selectArea(final BufferedImage screen) throws Exception {
final BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
JScrollPane screenScroll = new JScrollPane(screenLabel);
screenScroll.setPreferredSize(new Dimension((int)(screen.getWidth()/2), (int)(screen.getHeight()/2)));
JPanel panel = new JPanel(new BorderLayout());
panel.add(screenScroll, BorderLayout.CENTER);
final JLabel selectionLabel = new JLabel("Draw a rectangle");
panel.add(selectionLabel, BorderLayout.SOUTH);
repaint(screen, screenCopy);
screenLabel.repaint();
screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseMoved(MouseEvent me) {
start = me.getPoint();
repaint(screen, screenCopy);
selectionLabel.setText("Start Point: " + start);
screenLabel.repaint();
}
#Override
public void mouseDragged(MouseEvent me) {
end = me.getPoint();
captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
try {
paintFinalImage(new BufferedImage((int)screenCopy.getGraphics().getClipBounds(captureRect).getWidth()+1,
(int)screenCopy.getGraphics().getClipBounds(captureRect).getHeight()+1, screen.getType()));
} catch (Exception e) {
e.printStackTrace();
}
repaint(screen, screenCopy);
screenLabel.repaint();
selectionLabel.setText("Rectangle: " + captureRect);
}
});
JOptionPane.showMessageDialog(null, panel, "Select your image", JOptionPane.OK_OPTION, new ImageIcon(""));
uploadImage(screenShot);
System.out.println("Final rectangle: " + screenCopy.getGraphics().getClipBounds(captureRect));
}
public void paintFinalImage(BufferedImage image) throws Exception {
screenShot = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
Graphics2D g = screenShot.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
}
public void repaint(BufferedImage orig, BufferedImage copy) {
Graphics2D g = copy.createGraphics();
g.drawImage(orig, 0, 0, null);
if (captureRect != null) {
g.setColor(Color.BLACK);
g.draw(captureRect);
}
g.dispose();
}
public void uploadImage(BufferedImage image) throws Exception {
String IMGUR_POST_URI = "http://api.imgur.com/2/upload.xml";
String IMGUR_API_KEY = API_KEY;
String readLine = null;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, imageType.getSelectedItem().toString(), outputStream);
URL url = new URL(IMGUR_POST_URI);
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(outputStream.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8");
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(data);
wr.flush();
InputStream inputStream;
if (((HttpURLConnection) urlConnection).getResponseCode() == 400) {
inputStream = ((HttpURLConnection) urlConnection).getErrorStream();
} else {
inputStream = urlConnection.getInputStream();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
readLine = line;
}
wr.close();
reader.close();
} catch(Exception e){
e.printStackTrace();
}
String URL = readLine.substring(readLine.indexOf("<original>") + 10, readLine.indexOf("</original>"));
System.out.println(URL);
Toolkit.getDefaultToolkit().beep();
StringSelection stringSelection = new StringSelection(URL);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
System.out.println(command);
if (command.equalsIgnoreCase("open")) {
frame.setVisible(true);
} else if (command.equalsIgnoreCase("exit")) {
System.exit(-1);
} else if (command.equalsIgnoreCase("screenshot")) {
try {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
selectArea(screen);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
#Override
public void itemStateChanged(ItemEvent e) {
System.out.println(e.getItem() + ", " +e.getStateChange());
String itemChanged = e.getItem().toString();
if (itemChanged.equalsIgnoreCase("run on start-up")) {
//TODO
} else if (itemChanged.equalsIgnoreCase("hide when minimized")) {
if (e.getStateChange() == ItemEvent.DESELECTED) {
hideWhenMinimized = false;
} else if (e.getStateChange() == ItemEvent.SELECTED) {
hideWhenMinimized = true;
}
}
}
#Override
public void keyTyped(KeyEvent e) {
System.out.println("KeyTyped: "+e.getKeyCode());
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void nativeKeyPressed(NativeKeyEvent e) {
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("KeyReleased: "+e.getKeyCode());
System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == 154) {
try {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
selectArea(screen);
frame.setVisible(true);
} catch(Exception e1) {
e1.printStackTrace();
}
}
}
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
}
I recommend that you simplify your problem to solve it. Create a small compilable and runnable program that doesn't have all the baggage of your current code, but whose only goal is to display an image, and let the user select a sub-portion of that image. Then if your attempt fails, you can post your attempt here, and we can actually both run it, and understand it, and hopefully then be able to fix it.
For example, here's a small bit of compilable and runnable code that uses a MouseListener to select a small section of an image. Perhaps you can get some ideas from it:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class ImagePlay extends JPanel {
public static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/" +
"commons/thumb/3/39/European_Common_Frog_Rana_temporaria.jpg/" +
"800px-European_Common_Frog_Rana_temporaria.jpg";
private static final Color RECT_COLOR = new Color(180, 180, 255);
private BufferedImage img = null;
Point p1 = null;
Point p2 = null;
public ImagePlay() {
URL imgUrl;
try {
imgUrl = new URL(IMAGE_PATH);
img = ImageIO.read(imgUrl );
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon) {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
myLabelPaint(g);
}
};
setLayout(new BorderLayout());
add(new JScrollPane(label));
MouseAdapter mAdapter = new MyMouseAdapter();
label.addMouseListener(mAdapter);
label.addMouseMotionListener(mAdapter);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void myLabelPaint(Graphics g) {
if (p1 != null && p2 != null) {
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int width = Math.abs(p1.x - p2.x);
int height = Math.abs(p1.y - p2.y);
g.setXORMode(Color.DARK_GRAY);
g.setColor(RECT_COLOR);
g.drawRect(x, y, width, height);
}
}
private class MyMouseAdapter extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
p2 = null;
repaint();
}
#Override
public void mouseDragged(MouseEvent e) {
p2 = e.getPoint();
repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
p2 = e.getPoint();
repaint();
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int width = Math.abs(p1.x - p2.x);
int height = Math.abs(p1.y - p2.y);
BufferedImage smlImg = img.getSubimage(x, y, width, height);
ImageIcon icon = new ImageIcon(smlImg);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(ImagePlay.this, label, "Selected Image",
JOptionPane.PLAIN_MESSAGE);
}
}
private static void createAndShowGui() {
ImagePlay mainPanel = new ImagePlay();
JFrame frame = new JFrame("ImagePlay");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
It uses a publicly available image obtained online, a MouseListener that's added to a JLabel, and the JLabel has its paintComponent(...) method overridden so as to show the guide lines from the MouseListener/Adapter. It creates the sub image via BufferedImage's getSubimage(...) method, and then it displays it in a JOptionPane, but it would be trivial to save the image if desired.
I'm trying to capture the screen without including my application's window. To do this I first call setVisible(false), then I call the createScreenCapture method, and finally I call setVisible(true). This isn't working however and I'm still getting my applications window in the screen capture. If I add a call to sleep this seems to resolve the issue, but I know this is bad practice. What is the right way to do this?
Code:
setVisible(false);
BufferedImage screen = robot.createScreenCapture(rectScreenSize);
setVisible(true);
Have you tried to use SwingUtilities.invokeLater() and run the capture inside of the runnable passed as an argument? My guess is that the repaint performed to remove your application is performed right after the end of the current event in the AWT-EventQueue and thus invoking the call immediately still captures your window. Invoking the createCapture in a delayed event through invokeLater should fix this.
you have to delay this action by implements Swing Timer, for example
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class CaptureScreen implements ActionListener {
private JFrame f = new JFrame("Screen Capture");
private JPanel pane = new JPanel();
private JButton capture = new JButton("Capture");
private JDialog d = new JDialog();
private JScrollPane scrollPane = new JScrollPane();
private JLabel l = new JLabel();
private Point location;
private Timer timer1;
public CaptureScreen() {
capture.setActionCommand("CaptureScreen");
capture.setFocusPainted(false);
capture.addActionListener(this);
capture.setPreferredSize(new Dimension(300, 50));
pane.add(capture);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(pane);
f.setLocation(100, 100);
f.pack();
f.setVisible(true);
createPicContainer();
startTimer();
}
private void createPicContainer() {
l.setPreferredSize(new Dimension(700, 500));
scrollPane = new JScrollPane(l,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBackground(Color.white);
scrollPane.getViewport().setBackground(Color.white);
d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d.add(scrollPane);
d.pack();
d.setVisible(false);
d.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
f.setVisible(true);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});
}
private void startTimer() {
timer1 = new Timer(1000, new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
capture.doClick();
f.setVisible(false);
}
});
}
});
timer1.setDelay(500);
timer1.setRepeats(false);
timer1.start();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("CaptureScreen")) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
Robot r;
BufferedImage bI;
try {
r = new Robot(); // creates robot not sure exactly how it works
Thread.sleep(1000); // waits 1 second before capture
bI = r.createScreenCapture(new Rectangle(dim)); // tells robot to capture the screen
showPic(bI);
saveImage(bI);
} catch (AWTException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
}
private void saveImage(BufferedImage bI) {
try {
ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
private void showPic(BufferedImage bI) {
ImageIcon pic = new ImageIcon(bI);
l.setIcon(pic);
l.revalidate();
l.repaint();
d.setVisible(false);
//location = f.getLocationOnScreen();
//int x = location.x;
//int y = location.y;
//d.setLocation(x, y + f.getHeight());
d.setLocation(150, 150);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
d.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CaptureScreen cs = new CaptureScreen();
}
});
}
}