Applet Shows Blank Screen in Browser - java

I have an applet that is packaged into a jar which although it runs (tested with print statements showing in console) it displays only a blank screen.
Here is the applet code:
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class Test extends JApplet {
JLayeredPane frame = new JLayeredPane();
JButton button = new JButton("Test");
JLabel backgroundLabel;
public void init() {
button.setBounds(10, 10, 100, 40);
backgroundLabel = new JLabel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(
new Point(0, 0),
new Color(90, 207, 233),
new Point(0, getHeight()),
Color.white));
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
};
backgroundLabel.setBounds(0, 0, getWidth(), getHeight());
frame.add(backgroundLabel, new Integer(0));
frame.add(button, new Integer(1));
add(frame);
}
}
And this is the html code:
<applet
id="clientApplet"
codebase="test"
code="Test.class"
archive="test.jar"
width="820" height="600">
Your browser does not support the <code>applet</code> tag.
</applet>
The applet works fine in Eclipse and no errors show up in the console. Can anyone please say what the problem might be?

It seems that an update has stopped the <applet> tag from functioning. I replaced it with <embed> and all is fine now.
<embed id="test"
type="application/x-java-applet;version=1.6"
width="256" height="256"
archive="test.jar"
code="Test.class"
codebase="test"
pluginspage="http://java.com/download/"
myParam="" />

Related

JFrame Graphics2D.drawline() x-axis origin problem in Java

I created a JFrame. And when I draw a line with x=0, y=0 starting point in the x and y axis with Graphics2D.drawline(), it does not start from the x=0 axis of the JFrame. What should I do? enter image description here
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class MyGraphics extends JFrame{
MyGraphics(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public void paint(Graphics g){
Graphics2D g2D = (Graphics2D)g;
g2D.setPaint(Color.blue);
g2D.drawLine(0, 0, 500, 500);
g2D.setStroke(new BasicStroke(5));
}
}
It starts from (0,0) but it is covered by top bar of window frame.
To see this, put
this.setUndecorated(true);
as the first line in constructor. It will show a window without top bar.

How to change AWT Label Font

I am trying to define the Font for an AWT Label.
While I can manage to use fonts for a Java2D graphics environment, same font seems not to work with AWT Labels. I would like to understand if there is any limitation on font usage I am not aware about it for AWT Labels, or if I am simply not using the right syntax/procedure.
this is my code, it basically adds a Label and text to the graphics context:
package com.company.test;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
public class TestTest extends Frame {
Label myLabel = new Label();
Font myFont = new Font("Roboto Condensed Light", Font.PLAIN, 12);
Graphics2D g2d;
public TestTest() {
setSize(500,200);
setLocation(10,10);
setUndecorated(false);
myLabel.setBackground(Color.red);
myLabel.setFont(myFont);
myLabel.setText("ROBOTO CONDENSED, THIS DOES NOT WORK!");
add(myLabel, BorderLayout.SOUTH);
setVisible(true);
}
public void paint(Graphics g) {
g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(0.0f));
g2d.setFont(myFont);
g2d.setColor(Color.BLACK);
g2d.drawString("ROBOTO CONDENSED THIS WORKS!",50, 50);
}
public static void main( String[] args ) {
TestTest window = new TestTest();
}
}
This is the result:
While font is properly defined (as it is used in the Text element in the Graphics component), it is not being applied to the AWT Label component.
Any tip is welcomed.
Note: please do not suggest to use SWING or JavaFX, I am well aware that they are the recommended way of using widgets. Question is specifically related to AWT Label widget.
Ok you have to create your own label; the idea is to get to the graphics of things. I have added my inline class but you can create a proper class; then you have to pass the string or other parameters to that class:
class TestTest extends Frame {
Font myFont = new Font("Rockwell Nova", Font.PLAIN, 12);
Graphics2D g2d;
public TestTest() {
Label myLabel = new Label() {
public void paint(Graphics g) {
g.setFont(myFont);
g.drawString("ROBOTO CONDENSED, THIS DOES NOT WORK!", 0, 20);
}
};
setSize(500,200);
setLocation(10,10);
setUndecorated(false);
myLabel.setBackground(Color.red);
add(myLabel, BorderLayout.SOUTH);
setVisible(true);
}
public void paint(Graphics g) {
g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(0.0f));
g2d.setFont(myFont);
g2d.setColor(Color.BLACK);
g2d.drawString("ROBOTO CONDENSED THIS WORKS!",50, 50);
}
}

Unable to see image in browser for my applet

I have created a simple Applet program that displays an image, this is my program:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.BLACK);
g.fillRect(0, 0, d.width, d.height); // paint background
g.setFont(new Font("San-serif", Font.BOLD, 24));
g.setColor(new Color(255, 215,0));
g.drawString("Hello, world!", 60, 40);
// g.drawImage(getImage(getCodeBase(), "Rabbit.jpg"), 20, 60, this);
//g.drawImage(getImage(getCodeBase(), getClass().getResource("/Rabbit.jpg").getFile()), 20, 60, this);
ImageIcon image2 = new ImageIcon(getClass().getResource("/Rabbit.jpg"));
g.drawImage(image2.getImage(), 20, 60, this);
}
}
Now, in eclipse I copied the Rabbit.jpg image to bin directory and when I run the applet it is working fine and I can see the image.
Now if I place the class file in a directory on my machine and also the image Rabbit.jpg in same path of my class file and then I created the below html file:
<applet code="HelloWorldApplet.class" width="350" height="350">
Java applet
</applet>
If I run the command appletviewer sample.html then I can see the applet is loaded along with the image. Now if I open the sample.html file in browser then the browser is not displaying the image, it just shows the text "Hello, world!"
Can you please tell me how I can see the image also as embedded in my applet?

Graphics2d methods produce no output

I've been trying to get the Graphics2d object to work without success. I've searched for an answer on both the Oracle tutorial site and Stackoverflow without finding an answer.
The problem I have is that when I call the methods lineTo, fill, and drawRect, I get a blank grey square in my window, instead of the shapes that I want.
package main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphicsTesting extends JPanel {
private static final long serialVersionUID = 6096199371167913312L;
static BufferedImage buffImag = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
static Graphics2D graff = buffImag.createGraphics();
Point2D.Double point = new Point2D.Double(10, 10);
static Graphics gra = buffImag.createGraphics();
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 4);
gp.moveTo(30, 55);
gp.lineTo(168, 384);
gp.lineTo(462, 81);
gp.lineTo(321, 423);
gp.lineTo(269, 243);
g2.setColor(new Color(112, 150, 134));
g2.fill(gp);
g2.setColor(new Color(56, 112, 232));
g2.draw(gp);
g2.setColor(new Color(152, 1, 210));
g2.drawRect(25, 152, 380, 405);
g2.drawImage(buffImag, 0, 0, 500, 0, 0, 500, 500, 500, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
GraphicsTesting gT = new GraphicsTesting();
frame.setContentPane(gT);
gT.paint(gra);
}
}
Your code seems fine and I tried to run it on my machine. It produces what you can see on the following screen shot. I think this is what you expect to get, right? Your problem might be coming from a faulty Java installation or an os-related issue. Which virtual machine are you using and on which operating system?
As a side note, your code is not complete though, as the following import is missing
import java.awt.geom.Point2D;
There are several issues with your code, but the main one causing your problem is that you are making the frame visible before adding the panel to it.
Move your setVisible(true) line to here:
frame.setContentPane(gT);
frame.setVisible(true);
gT.paint(gra);

Java Swing transparency drawing issues

Edit:
I submitted a bug for the below (it may take a a few days to become approved though):
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7043319
Some more details:
It works with Windows Sun JDK 1.6 versions 13 and 17
It fails on Ubuntu 11.04 x64 with both OpenJDK 1.6.0_22 and Sun JDK 1.6.0_24
What I want is to make a background image panel with additional panels on top of it (with additional components - e.g. JButtons, custom shapes, etc. - in them) and draw all that correctly. I'm using JLayeredPane for that purpose in my app, but for the sake of an example the below code should suffice. I'm open to suggestions about how to do what I want regardless of the below problem.
I'm running into the issue that the painting is behaving really weird. It doesn't repaint fully (e.g. only the top part above the image), it repaints in - from what I've noticed increasingly spaced - steps (e.g. 1st paint, 3rd paint, 9th paint, 21st paint, 64th paint, etc.). My guess is that I'm going too much into implementation here - is there anything obviously wrong with the below?
On a separate note, there are three commented lines below. Interestingly enough, uncommenting any of them and commenting the following line solves the problem. The images are with the following attributes (and it seems it doesn't matter which image - just the size):
cat.jpg JPEG 640x533 640x533+0+0 8-bit DirectClass 110KB 0.000u 0:00.000
cat-small.jpg JPEG 200x167 200x167+0+0 8-bit DirectClass 7.99KB 0.000u 0:00.000
Here's the Java code I'm having issues with:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class SwingDrawingPrb {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame frame = new JFrame("SwingDrawingPrb");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
frame.setLocation(550, 50);
frame.setSize(1000, 800);
frame.setVisible(true);
// ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat-small.jpg"));
ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat.jpg"));
final JPanel imagePanel = new JPanel() {
// Color trans = new Color(255, 0, 0, 255);
Color trans = new Color(255, 0, 0, 64);
protected void paintComponent(Graphics g) {
System.out.println("painting");
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(trans);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.blue);
g.drawLine(0, 0, 1000, 1000);
}
};
imagePanel.setBounds(0, 0, image.getIconWidth() + 200, image.getIconHeight() + 200);
imagePanel.setLayout(null);
// JLabel imageLabel = new JLabel("Hello, world!");
JLabel imageLabel = new JLabel(image);
imageLabel.setBounds(100, 100, image.getIconWidth(), image.getIconHeight());
imageLabel.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved");
imagePanel.repaint();
}
});
imagePanel.add(imageLabel);
contentPane.add(imagePanel);
}
}
You need to add:
imagePanel.setOpaque(false);
See Backgrounds With Transparency for more information.

Categories