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?
Related
I'm learning java by watching tutorials, I'm a beginner. Currently, I'm trying to follow a tutorial (https://www.youtube.com/watch?v=_SqnzvJuKiA) to make the snake game. Around minute 11, the man makes a jpg display on the window. I believe I followed all of his steps but can't seem to make it work. Iv'e attached following image of my code and output
and paste "my" code.In the public void paint line netbeans tells me to "add #override annotation", which he didn't do, and "multiple annotations here [2] click to cycle".
In the output on the top white rectangle should appear the jpg.
Does someone see my mistake?
package snake;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class GP extends JPanel{
private ImageIcon titleImage;
public GP() {
}
public void paint(Graphics g)
{
// Titulo
g.setColor(Color.white);
g.drawRect(24, 10, 851, 55);
titleImage = new ImageIcon("snaketitle.jpg");
//ImageIcon icon = new ImageIcon("androidBook.jpg");
titleImage.paintIcon(this, g, 25, 11);
//Area Juego
g.setColor(Color.WHITE);
g.drawRect(24,74,851,577);
g.setColor(Color.black);
g.fillRect(25,75,850,575);
}
First of all i think your Question is understandable, what you are facing is that the program is not able to locate your image "androidBook.jpg".
By the way,
The ImageIcon class has several Constructors
Try using
ImageIcon(URL location)
//Creates an ImageIcon from the specified URL.
and paste the URL pointing to the location of "androidBook.jpg".
Also you can follow the below link for more insight...
https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html
I'm trying to use an image that I created in photoshop in a applet. Every time I try to load the image it doesn't show up, but if I use an image from the internet it works fine. I've tried using both png and jpg neither work.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Test extends Applet{
final int WIDTH = 1000, HEIGHT = 400, MIN = 1;
private Image img;
public void init() {
img = getImage(getDocumentBase(), "image.jpg");
setSize(WIDTH, HEIGHT);
}
public void paint(Graphics g) {
g.drawImage(img, 20, 20, null);
}
}
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);
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="" />
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.