Why there is no thing appear in my applet ?
I started to learn java using eclipse before two days, but when I come to image part there is no thing appear in my applet
My code is as follows:
import java.awt.*;
import javax.swing.*;
public class CityScape extends JApplet {
setSize(800,500);
super.paint(g);
Image img=getImage(getCodeBase(),"aaa.png");
g.drawImage(img,0,0,this);
}}
Related
I am not sure how to fully express this but, I have probably gone through 10 pages of Google links on this topic and not one of them has helped me solve my issue.
I thought it would be simple enough, I was just trying to add an image to the paint function of my java applet, like any other shape, but this has turned out to be a nightmare for me.
The problem is that every time I try to run the drawImage function it keeps saying Access Denied ("java.io.FilePermission" "Image.jpg" "read"). Yet, none of the tutorials mention this at all, all they ever say is that I should do the following:
import java.applet.*;
import java.awt.*;
Image img;
//These would go in the paint function
img=getImage(getDocumentBase(),"/Image.jpg"); //I have tried without the slash too
g.drawImage(img,20,20,this);
This is all they do and it works for them, but it just won't work for me. Other methods are far too complex for the sake of just adding an image, and even when I go through the toil of doing those it keeps giving me the "Access Denied" message. There's also the method of "signing" it, but I really don't think that's going to help given all that I have tried, so I am afraid it might just be another wasted endeavor. None of the tutorials even tell you to have your applet signed.
I have the image in the "build" (also called bin) folder together with the classes.
The program seemed to run when I included the entire file path, but even then the image did not display. That is not to mention I can't really include the complete path from my own computer because then it wouldn't work when I actually send it to another person.
Please, I just want to know why it doesn't work for me yet seems to work perfectly for others. That, and if there's a way around this.
This is an example of what I am doing:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),"/Image.jpg");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(img,20,20,this);
}
}
This is my HTML file:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="400" height="500">
</applet>
</body>
</html>
According JApplet java docs method getImage(URL url, String name) should have two parameter: URL-link to picture and String name.
Is method getDocumentBase() returnig an URL-link?
You must start by understanding that an Applet, unless signed, may not read from the file system. It must use either classpath resources or things fetched from the same place it was fetched from. You have to decide which of these applies to you. If the image is a fixed image, you can put it in your classpath as a resource, and use Class.getResourceAsStream. If it's a different image every time, you'll have to use HTTP.
Try this one if the image in the "build" (also called bin) folder together with the classes.
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import javax.swing.JApplet;
public class JavaProject extends JApplet {
Image img;
public void init() {
img = getImage(getDocumentBase(), "images/222.png");
// Please ensure that 222.png is placed under bin/images folder directly
}
#Override
public void paint(Graphics g) {
update(g);
}
#Override
public void update(Graphics g) {
g.drawImage(img, 20, 20, this);
}
}
Try with HTTP URL first
URL myURL = new URL("https://www.gravatar.com/avatar/a94613cea642e6e9e2105867bc2e103e?s=32&d=identicon&r=PG&f=1");
img = getImage(myURL);
If you are using Eclipse under Windows then have a look at below screenshot:
Please have a look at below post to get some understanding about it.
java.io.FilePermission exception - Read from jar file?
ANSWERED / CLOSED
to make things simple, I've just pulled code directly from the java applet tutorial.
The code is:
private void createGUI() {
JLabel label = new JLabel(
"You are successfully running a Swing applet!");
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
getContentPane().add(label, BorderLayout.CENTER);
}
The only thing is, getContentPane() isn't recognized even though I have the following imports
import java.applet.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;
could someone please tell me what I have to do to get Eclipse Kepler to recognize getContentPane()?
Thanks
I'm following along with Stanford's CS106a class and trying to do the assigments. I had difficulty running the sample code from the book but somehow managed to run them with the ACM package. Right now I'm trying to do the assignments and run my own code. I've created a "project" and a .java file in that project. I don't know how to run it though. I keep getting the following:
Error: Could not find or load main class Pyramid.
I think it is because the program isn't accessing the ACM package. Below is the code although I think it would happen with any code I write. Any help would be appreciated.
Thanks so much.
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class GRectExample extends GraphicsProgram {
public void run() {
GRect rect = new GRect(100, 50, 125, 60);
rect.setFilled(true);
rect.setColor(Color.RED);
add(rect);
}
}
Create a main method inside GRectExample class, for examle
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class GRectExample extends GraphicsProgram {
public void run() {
GRect rect = new GRect(100, 50, 125, 60);
rect.setFilled(true);
rect.setColor(Color.RED);
add(rect);
}
public static void main(String args[])
{
new GRectExample().run();
}
}
Looks like you have to tell Eclipse where to locate the ACM package, most of the times it can't assume the exact location.
Right click on your project folder and select Properties.
Select the Java Build Path option and click on the "Add External JARs" and that will include it into your project...
Not too familiar with Eclipse, but here's a suggestion:
Right - Click on the Project folder
click Properties at the bottom
click Run/Debug Settings
Make sure your Launching class is the list. Click on it, make sure it's the Main class
Make sure you use the fully qualified name i.e. mypackage.MyClass
Also try clicking all of them in the list. And make sure only the one you want to be the launching class has the Main Class field filled in.
I am trying to make a form page in a java applet. Now after making textbox and submit button, how can I validate this text-box? Anyone if can tell me if I can link JS code to applet and if yes, then how?
package com.tcs.applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JButton;
public class MyApplet extends Applet {
TextField inputLine = new TextField(15);
private JButton button = new JButton("Submit");
public MyApplet() {
add(inputLine);
add(button);
}
}
Package netscape.javascript provides Java code(your applet code) the ability to access the JavaScript engine in the web browser.
for more info you can check following
Netscape Doc
On the action performed of the JButton do your validation.
If there is wrong input in textfield then throw a acception.
I'm trying to design an applet using the designer delivered with Intellij IDEA. I have no idea where I can find an option to make one. Solutions have been posted on the net, but seem to be outdated at the moment.
Create this class in your application
import java.applet.Applet;
import java.awt.*;
public class Main extends Applet {
public void paint(Graphics g) {
g.drawString("SALAM Applet", 20, 20);
g.drawString("سلام اپلت", 20, 40);
}
}
Right click on your applet class and select create 'Main'.
Click OK.
Run
for more information visit this link