I'm trying to make a program that will work and function like simple horse races, but I can't set different speeds. As far as I can see, see that when the timer gets different values, for example: I have two pictures, one has a timer 40 and the other 80, they move at the same speed, but a picture that has 80 goes chopping, or rather has fewer clocks, and this one I have 40 goes more smooth.
public class AnimatedThing {
public AnimatedThing() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Trke");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 600));
frame.setLayout(new BorderLayout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new GridLayout(4, 1));
frame.setBackground(Color.white);
enemyRed enemyPink = new enemyPink(); // sub-panel 1
frame.add(enemyPink);
frame.setLocationRelativeTo(null);
}
});
}
public class enemyPink extends JPanel {
private BufferedImage enemyPink;
public enemyPink() {
try {
enemyPink = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEB\\images\\enemy - pink.png"));
Timer timer2 = new Timer(20, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyPink.getWidth() > getWidth()) {
} else {
repaint();
}
}
});
timer2.setRepeats(true);
timer2.setCoalesce(true);
timer2.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return enemyPink == null ? super.getPreferredSize() : new Dimension(enemyPink.getWidth() * 4, enemyPink.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyPink, xPos, 0, this);
}
}
It is best to read the standard documentation a bit and look at some tutorial on how to do it. The code is very confusing and I can't solve it.
I will post a link: https://youtu.be/Kmgo00avvEw
Related
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;
}
}
}
}
Trying to work on a networking project, where two or more users need to be able to paint in the same JFrame.
How would this be approached most realisticly, connecting the two PCs using their IPs, then create a real-time scenario for them to work in, OR, some kind of package system, where the users send what they've drawne recently, and then the other user can see what have just been send?
EDIT: Added my drawing class for context
public class CanvasFrame extends JPanel {
ArrayList<Point> location = new ArrayList<Point>();
JTextArea consoleOutput = new JTextArea(1,20);
public void addComponentToPane(Container pane) {
consoleOutput.setEditable(false);
}
public CanvasFrame() {
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
location.clear();
location.add(e.getPoint());
}
});
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
location.add(e.getPoint());
repaint();
}
});
setPreferredSize(new Dimension(800, 500));
setBackground(Color.WHITE);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(location.isEmpty()){
return;
}
Point p = location.get(0);
for (int i = 1; i < location.size(); i++) {
Point q = location.get(i);
g.drawLine(p.x, p.y, q.x, q.y);
p = q;
}
}
public static void main(String[] args) throws Exception {
InetAddress SERVERIP = InetAddress.getLocalHost();
Runnable runnable = new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Drawing with friends");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CanvasFrame(), BorderLayout.CENTER);
JTextArea IPadress = new JTextArea(1,20);
IPadress.setEditable(false);
IPadress.append("DEVICE IP: " + SERVERIP.getHostAddress());
frame.add(IPadress, BorderLayout.SOUTH);
frame.setSize(new Dimension(800,600));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
}
I am having two classes, a main class and class which extends JPanel and implements Runnable. I am trying to create two threads for the same instance of the JPanel class in an actionListener, but i just don't know where to create the JPanel1 object...
//Edit: Button1 is the start of the application .After that , button 2 will appear with a quick animation of labels and when clicked it(button2) will start the same animation too. How can i do whenever one of these buttons is clicked to launch the run method ?
public void run() {
if(isTom){
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.removeAll();
panel.add(tomLabel1);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel2);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel3);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel4);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel5);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.removeAll();
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
public Game(){
JFrame frame = new JFrame();
Panel1 key = new Panel1();
key.addKeyListener(key);
frame.add(key);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public class Panel1 extends JPanel implements KeyListener,Runnable{
JButton button1 = new JButton("BUTTON1");
JButton button2 = new JButton("BUTTON2");
add(button1);add(button2);
Thread t = new Thread(this); // This works, but i need it inside the actionListener.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button1");
Thread x = new Thread(j);//'j' is an JPanel1 object. I need something like this i guess
x.setName("Thread x");});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button2");
Thread y = new Thread(j);
y.setName("Thread y");
});
public void run(){
System.out.println(Thread.currentThread().getName());
}
First, Swing is NOT thread safe! This means that you should NEVER create or modify the UI from outside of context of the Event Dispatching Thread!
Second, Swing is a single threaded environment, this means that you should never block or execute long running code from within the context of the Event Dispatching Thread, this will cause the UI to freeze until the block is removed.
Your concept is correct, you implementation is wrong, you should use a Swing Timer instead.
Instead of removing and adding labels, use a single label and change it's properties (text/icon, what ever)
See Concurrency in Swing and How to use Swing Timers for more details
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
try {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
private JButton button1;
private JButton button2;
private SplashScreen splashScreen;
public TestPane() throws IOException {
button1 = new JButton("Button One");
button2 = new JButton("Button Two");
JPanel buttons = new JPanel();
buttons.add(button1);
buttons.add(button2);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});
splashScreen = new SplashScreen();
setLayout(new BorderLayout());
add(splashScreen);
add(buttons, BorderLayout.SOUTH);
}
}
public static class SplashScreen extends JPanel {
protected static final int IMAGE_COUNT = 4;
private JLabel label;
private Timer timer;
private int delta;
private int count;
private Icon[] icons;
private Dimension preferredSize;
public SplashScreen() throws IOException {
String path = "/images/splash";
String ext = ".png";
icons = new Icon[IMAGE_COUNT];
int maxWidth = 0;
int maxHeight = 0;
for (int index = 0; index < IMAGE_COUNT; index++) {
String name = path + (index + 1) + ext;
System.out.println(name);
icons[index] = new ImageIcon(ImageIO.read(getClass().getResource(name)));
maxWidth = Math.max(maxWidth, icons[index].getIconWidth());
maxHeight = Math.max(maxHeight, icons[index].getIconHeight());
}
preferredSize = new Dimension(maxWidth, maxHeight);
timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (count >= IMAGE_COUNT) {
count = IMAGE_COUNT - 2;
delta = -1;
} else if (count < 0) {
((Timer)e.getSource()).stop();
} else {
label.setIcon(icons[count]);
count += delta;
}
}
});
setLayout(new BorderLayout());
label = new JLabel();
add(label);
}
#Override
public Dimension getPreferredSize() {
return preferredSize;
}
public void run() {
if (!timer.isRunning()) {
delta = 1;
count = 0;
timer.start();
}
}
}
}
Create a new instance of a Thread with your Panel1 class instance:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button1");
Thread x = new Thread(Panel1.this);
x.start();
x.setName("Thread x");});
Repeat for the other button with a new Thread object:
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button2");
Thread y = new Thread(Panel1.this);
y.start();
y.setName("Thread y"); });
Panel1.this is referring to the instance of the Panel1 class that is currently running, making sure that your Threads are executing run() of that instance.
one of my menus is simply an image, and a button to go 'back.'
The problem is, the back button always loads before the image, even though it is after in the code.
Is there any way to fix this? I'm brand new to programming, so the best way I could think of would be to pause the method somehow, say for 3 seconds, while the picture loads, THEN load the back button.
I have tried using thread.sleep(3000) but if I do that, the image doesn't load at all! Maybe I am using it wrong?
public void render(Graphics g){
if(Instscrn==true){
g.drawImage(InstructionScreen, 0, 0, Resources.sp);
if(instclose){
g.setFont(new Font("Arial", Font.PLAIN, 30));
g.setColor(Color.BLACK);
g.drawString("Back", 672, 552);
}
With thread.sleep
public void render(Graphics g){
if(Instscrn==true){
g.drawImage(InstructionScreen, 0, 0, Resources.sp);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(instclose){
g.setFont(new Font("Arial", Font.PLAIN, 30));
g.setColor(Color.BLACK);
g.drawString("Back", 672, 552);
}
Thanks for your help!
You can load image on render(Graphics g) without pausing any method:
boolean inited = false;
Image i;
public void render(Graphics g){
if(!inited) {
//here, init your image, for example: download files from web or load resources
try {
i = ImageIO.read(new File(""));//loading image
} catch (IOException e) {
e.printStackTrace();//error
}
inited=true;
}
g.drawImage...
}
You can't pause a method, but you can pause a thread (with method):
public static void main(String[] args) throws InterruptedException {
final Thread pauseable = new Thread("Pauseable method/thread") {
//Pauseable method
public void run() {
for(int i=0; i<20; i++) {
System.out.println("Working");
sleepMethod();
}
}
//Sleep for 1 seconds
public void sleepMethod() {
try {
sleep(1000);
} catch(Throwable t){}
}
};
pauseable.start();
JFrame f = new JFrame();
JButton pause = new JButton("Pause");
JButton start = new JButton("Resume");
pause.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
try {
pauseable.suspend();
} catch(Throwable t) {
t.printStackTrace();
}
}});
start.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
try {
pauseable.resume();
} catch(Throwable t) {
t.printStackTrace();
}
}});
//STYLE OF BUTTONS ETC.:
JPanel p = new JPanel(new GridLayout(1,2));
p.add(pause);
p.add(start);
f.getContentPane().add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
pauseable.join(); //If you want wait to end of running method
System.out.println("Method pauseable.run() was ended!");
}
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");