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
Related
I'm trying to get a starter project for a template running over for JavaFX, the assignment is to get a hello world program up and running using JavaFX to get the program running.
(Some Background context: We're using IntelliJ for our class assignment, but recently we found out that it seems(?) support for JavaFx has been discontinued within InteliJ, regardless the same assignment remains so we installed a third party library following the steps over from a guide about getting the library, ( How to get JavaFX and Java 11 working in IntelliJ IDEA ), the instructions helped to get the program running and compiled and I used the paths out, but when I ran the program, I got a blank white screen, and I have no clue if this is normal or not.)
The code is just the default JavaFX template from Intllij, I've tried installing the SDKS and JDKS (which seemed to help the program compile), but when running, I just get a blank white screen.
Here is a screenshot of the white screen problem https://i.imgur.com/7ZYju3M.jpg
And the code below; it's just a IntelliJ default startup, but I don't know why it doesn't work.
package sample;
// Original Imports found in testTemplate
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
public class Controller {
}
We expected the results to say hello world. I think so anyways, but I actually have no clue what it's supposed to look like as it's my first time, I think it's supposed to say hello world. (Later on, we're supposed to get the GUI configured, but for right now, I just want to figure out what's going wrong with the program.)
package javaapp;
import javax.swing.JApplet;
import java.awt.Graphics;
public class JavaApp extends JApplet
{
public void paint(Graphics canvas)
{
canvas.drawOval(100, 50, 200, 200);
canvas.fillOval(155, 100, 10, 20);
canvas.fillOval(230, 100, 10, 20);
canvas.drawArc(150, 160, 100, 50, 180, 180);
}
}
I tried to run this code which draws a smiley face on a canvas. I ran this code also on Eclipse, where it compiled fine. But on NetBeans, it keeps saying it can't find the main class. Is NetBeans recommended for a beginner like me or should I look for another IDE that better suits for a beginner? I am more fond with the NetBeans because the interface seems more friendly.
In netbeans, right click on the project and chose properties. Under
Application > Webstart, there is an option Applet class. specify the
full path to your main class (i.e with package names).
Right click the project node and choose 'run' from the list. Don't right click the file (run file) as this doesn't work in NetBeans.
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);
}}
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?
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.