I have the following program to test GlassPane, but it doesn't work with JDIC's WebBrowser. Does anyone know what I did wrong and how to make it work?
import org.jdesktop.jdic.browser.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Test_Panel extends JPanel
{
static WebBrowser webBrowser=new WebBrowser();
static int W=802,H=702;
Test_Panel()
{
setPreferredSize(new Dimension(W,H));
setLayout(new BorderLayout());
webBrowser.setPreferredSize(new Dimension(W,H));
// add("Center",webBrowser);
try { webBrowser.setURL(new URL("http://www.yahoo.com")); }
catch (MalformedURLException e) { e.printStackTrace(); }
}
static void Create_And_Show_GUI()
{
JFrame frame=new JFrame("Test");
frame.add(new Test_Panel());
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
new My_GlassPane(frame,W,H);
frame.pack();
frame.setBounds(0,0,W,H);
frame.setVisible(true);
}
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } }); }
}
class My_GlassPane extends JComponent
{
JFrame f;
int W,H,Edge,Size;
public My_GlassPane(JFrame f,int W,int H)
{
this.f=f;
this.W=W;
this.H=H;
Edge=W/100;
Size=W/5;
f.setGlassPane(this);
f.getGlassPane().setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(W/6,H*18/120,W*2/3,H*2/3);
g.setColor(Color.white);
g.setFont(new Font("Times New Roman",0,Size));
g.drawString("Test",W/3,H*68/120);
}
}
If you uncomment add("Center",webBrowser); you will see what I mean-- the GlassPane won't show up. Why not?
You need to have "jdic.jar" and "IeEmbed.exe" to make it work. The version I have is 0.9.1.0 and you can get them here.
As I've read, WebBrowser is an AWT component while GlassPane is a Swing component. There is a common problem mixing heavyweight and lightweight components. I don't think there is a workaround on what you're trying to do.
More information on this subject can be found in this discussion.
Related
I'm trying to add a line into my program, it runs however displays nothing, how do I fix this?
I've watched tutorials and I've come up with the following code, but it doesn't display anything. How do I fix this?
public void paint(Graphics g)
{
g.drawLine(0, 0, 100, 100);
}
Here is my full program:
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
public class GuiLine {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiLine window = new GuiLine();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiLine() {
initialize();
}
public void paint(Graphics g)
{
g.drawLine(0, 0, 100, 100);
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Your class GuiLine has the method paint(Graphics g), but it will never be called since the class isn't a component (nor is it added to the frame, so it wouldn't be visible).
You can make the class extend JPanel and in your initialize method call frame.add(this);. Then you can continue reading some more tutorials.
I just started Java AWT programming.I can't change background color of my frame.!Here is my code..and below that error..Plz tell me why I'm facing this error and how to get rid of that..
Thanks in advance!
import java.awt.*;
import java.awt.event.*;
class F1 extends Frame
{
public void paint(Graphics g)
{
g.drawString("Hi",200,300);
}
public static void main(String args[])
{
F1 f = new F1();
f.setVisible(true);
f.setSize(1500,1500);
f.setBackground(Color.BLUE);
f.setTitle("First fRAME");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent x)
{
System.exit(0);
}
});
}
}
It works for me. Are you sure to have imported all required packages?
import java.awt.Color;
Try with this code, which is the simplest you can do to check if the problem is due to set background color or is due to something other:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test
{
public static void main(String[] args)
{
Frame frame = new Frame("Title");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
#Override public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setBackground(Color.BLUE);
frame.setVisible(true);
}
}
I'm trying to display a string when a button is pressed, but it does not work. I do not know what the problem is. I get no error, but that does not bother me. I'm missing something fundamental, I suppose. Please help!!
//I'm trying to draw a string in the frame when a button is pressed, but it won't work..
//Can't figure out what the problem is.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class AppletTwo extends JFrame implements ActionListener
{
JFrame frameOne;
JButton btnOne;
AppletTwo()
{
frameOne = new JFrame("frameOne");
frameOne.setSize(320,240);
frameOne.setLayout(new FlowLayout(FlowLayout.LEFT));
frameOne.setVisible(true);
frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOne = new JButton("Print");
btnOne.addActionListener(this);
frameOne.add(btnOne);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnOne)
{
repaint();
}
}
public void paint(Graphics g)
{
g.drawString("Never Works",150,150);
}
public static void main(String[] args)
{
AppletTwo frame1 = new AppletTwo();
}
}
" I'm missing something fundamental, I suppose. "
Yes, you are:
Main problem:
Your class is JFrame which is the component for which you are overriding the paint method. But you create another instance of a JFrame, which is the one you setVisible to. Keep in mind, you haven't drawn anything to this frame. So you are seeing the new instance of frame, not the class frame for which you are painting (and for which you never set visible).
Other problems:
You should always call super.paint[Component] after a paint[Component] override
#Override
public void paint(Graphics g) {
super.paint(g);
}
Don't paint on top level container like JFrame. Instead paint on a JPanel or JComponent and override is paintComponent method and call super.paintComponent, then add that component to the frame. See Performing Custom Painting
Swing apps should be run on the event dispatch thread (EDT). You can do so by wrapping your main code in a SwingUtilities.invokeLater(...). See Initial Threads
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
AppletTwo frame1 = new AppletTwo();
}
});
}
Generally, you always want to set the frame visible after adding your components.
Other notes:
See Extends JFrame vs. creating it inside the the program
UPDATE
Example with all the above mentioned points.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SimpleDrawing {
public SimpleDrawing() {
final DrawingPanel panel = new DrawingPanel();
final JTextField field = new JTextField(15);
JButton button = new JButton("Change name");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
String someString = field.getText();
if (!someString.isEmpty()) {
panel.setString(someString);
}
}
});
JPanel bottomPanel = new JPanel();
bottomPanel.add(field);
bottomPanel.add(button);
JFrame frame = new JFrame();
frame.add(panel);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private String someString = "Stackoverflow";
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(someString, 75, 75);
}
#Override
public Dimension getPreferredSize() {
return new Dimension (300, 100);
}
public void setString(String someString) {
this.someString = someString;
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
new SimpleDrawing();
}
});
}
}
According to the event delegation model i have taken one handler class.
package simple;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonHandler implements ActionListener {
MainFrame frame;
public ButtonHandler(MainFrame frame) {
this.frame = frame;
}
#Override
public void actionPerformed(ActionEvent e) {
MyThread thread = new MyThread(frame);
Thread mthread = new Thread(thread);
mthread.start();
}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
One MainFrame Class for frame creation
package simple;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class MainFrame extends JFrame {
JButton btnStart = new JButton("Start");
int xPos1,xPos2;
public MainFrame()
{
setSize(700, 600);
setLayout(null);
setVisible(true);
xPos1=10;
xPos2=600;
btnStart.setBounds(590, 30, 100, 30);
add(btnStart);
btnStart.addActionListener(new ButtonHandler(this));
paint(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void paint(Graphics g) {
g.drawString("Seed", xPos1, 50);
g.drawString("Infotech", xPos2, 550);
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
one Thread Class which implements runnable interface
package simple;
public class MyThread implements Runnable {
MainFrame frame;
public MyThread(MainFrame frame) {
super();
this.frame = frame;
}
#Override
public void run() {
while(true)
{
frame.repaint();
try {
frame.xPos1++;
frame.xPos2--;
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
//////////////////////////////////////////////////////////////
and one class for just main method
package simple;
public class Test {
public static void main(String[] args) {
new MainFrame();
}
}
Can anyone tell what is the problem in that I am new to the java programming concepts.
Overriding paint of a top level container (JFrame)
Not calling super.paint(g);
Calling paint(null)
setLayout(null)
Star by taking a look at Performing Custom Painting and Laying out components in a container
Instead of using a Thread, you may find a javax.swing.Timer more useful. Take a look at Concurrency in Swing for more details
I want to make the JFrame transparent, but the image on top of it to be non-transparent. This is what I have now:
Does anyone know a way to make only the JFrame transparent?
Here's my code:
import javax.swing.*;
import java.awt.*;
import com.sun.awt.AWTUtilities;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class SplashDemo extends JFrame
{
public SplashDemo()
{
setUndecorated(true);
setSize(200, 200);
add(new JLabel(new ImageIcon("puppy2.png")));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setOpacity(0.85f);
}
public static void main(String[] args)
{
new SplashDemo();
}
}
Basically, you need to make a transparent window and a translucent content pane. This will mean anything added to the content pane will continue to be rendered without additional alphering...
public class TranscluentWindow {
public static void main(String[] args) {
new TranscluentWindow();
}
public TranscluentWindow() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JWindow frame = new JWindow();
frame.setAlwaysOnTop(true);
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
}
}
});
frame.setBackground(new Color(0,0,0,0));
frame.setContentPane(new TranslucentPane());
frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/Puppy.png")))));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class TranslucentPane extends JPanel {
public TranslucentPane() {
setOpaque(false);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.85f));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
}