package org.kodejava.example.applet;
import java.applet.Applet;
import java.awt.*;
public class AppletGetImage extends Applet {
private Image logo;
#Override
public void init() {
logo = getImage(getDocumentBase(), "/images/logo.png");
}
#Override
public void paint(Graphics g) {
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.drawImage(logo, 10, 10, this);
}
}
Questions:
compalsary package declare in all java program?
private Image logo; for which purpose this variable can be use in applet class?
public void init() for which purpose this method can be use in applet class?
compalsary package declare in all java program?
No. See here for more: Java Package
private Image logo; for which purpose this variable can be use in
applet class?
See here: Declaring Member Variables in Java
public void init() for which purpose this method can be use in applet
class?
See here: Life Cycle of an Applet and Applet init() method
Related
Recently i decided to start learning how to make 2D games With JAVA ( eclipse ) so i found a tutorial online that shows how to make superMari game with java, i wrote the same code he wrote and i followed step by step what he did, which wasn't a big thing to talk about, unfortunately he's code shows, after excuting, a window with two images in it while mine shows just the window with no images, i ensure you that i imported the two images and put them in one package to avoid all kind of problems but it still shows nothing.
my code has two classes, "main" and "Scene", here it is, hopefully someone will find a solution for me, thank you guys!
Main.java :
package AiMEUR.AMiN.jeu;
import javax.swing.JFrame;
public class Main {
public static Scene scene;
public static void main(String[] args) {
JFrame fenetre = new JFrame("Naruto in mario World!!");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(700, 360);
fenetre.setLocationRelativeTo(null);
fenetre.setResizable(false);
fenetre.setAlwaysOnTop(true);
scene = new Scene();
fenetre.setContentPane(scene);
fenetre.setVisible(true);
}
}
Scene.java :
package AiMEUR.AMiN.jeu;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Scene extends JPanel{
private ImageIcon icoFond;
private Image imgFond1;
private ImageIcon icoMario;
private Image imgMario;
private int xFond1;
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
public void paintCompenent(Graphics g){
super.paintComponent(g);
Graphics g2 = (Graphics2D)g;
g2.drawImage(this.imgFond1, this.xFond1, 0, null);
g2.drawImage(imgMario, 300, 245, null);
}
}
You have not named the paintComponent method correctly, and therefore it is not being overridden.
The correct name is paintComponent not paintCompenent:
public class Example extends JPanel {
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
}
}
You can determine the loading status of an ImageIcon by doing something like this:
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
int status = icoFond.getImageLoadStatus();
switch (status) {
case (MediaTracker.COMPLETE): {
System.out.println("icoFond image has successfully loaded");
}
case (MediaTracker.ERRORED): {
System.out.println("The icoFond image didn't load successfully");
// probably because the image isn't actually at "/Images/fond.gif"
}
}
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
I'm trying to make a drawing program that will draw circles onto a canvas using the mouseDragged() method from MouseMotionListener. Inside my init() method, I put in this.addMouseMotionListener(this) and got this error message:
Cannot find symbol - method addMouseMotionListener(Canvas).
I am trying to make it so that every time the mouse is dragged, the Brush (which is just a circle), will draw onto the DrawingBoard which has a Canvas on it
Here is the code for the DrawingBoard:
import java.awt.*;
import java.awt.geom.*;
import java.awt.PointerInfo;
import java.awt.MouseInfo;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class DrawingBoard implements MouseMotionListener
{
private Canvas myCanvas;
private Brush myBrush;
private PointerInfo a = MouseInfo.getPointerInfo();
private Point p = a.getLocation();
private int x = (int)p.getX();
private int y = (int)p.getY();
private Brush b1 = new Brush(x, y, 10, Color.red, myCanvas);
// instance variables - replace the example below with your own
public void init() {
this.addMotionListener(this);
}
public DrawingBoard(int canvasSizeX, int canvasSizeY)
{
myCanvas = new Canvas("Drawing Board", canvasSizeX, canvasSizeY);
myCanvas.setVisible(true);
myCanvas.setForegroundColor(Color.lightGray);
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
b1.draw();
}
The code for the Canvas can be found here:
http://pastebin.com/RzMpkKhy
Your DrawingBoard needs to extend some kind of AWT/Swing-Component like Frame, JFRame, Panel etc, that contains the method addMouseMotionListener(). Because your not doing this, the compiler assumes that addMouseMotionListener() is a method you defined somewhere in your class. But he can't find it (because it doesn't exist) so it throws an error. Try adding this method to the Canvas code:
public void addMouseMotionListener(MouseMotionListener ml){
canvas.addMouseMotionListener(ml);
}
and put
myCanvas.addMouseMotionListener(this);
into your init() method.
Because the Canvas you are using is a custom class (there is another Canvas in the java.awt package), you have to alter its code to support user input (it doesn't look like it was designed to do this).
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
public class MonoplyDriver extends JApplet {
boolean isFirst=true;
Player John = new Player(1500,"Circle","John");
Board board = new Board();
Image imgBoard;
public void init()
{
//imgBoard = new ImageIcon("res/board.png").getImage();
imgBoard = getImage(getDocumentBase(),"res/board.png");
setSize(750,750);
System.out.println(getDocumentBase());
}
public void paint(Graphics g)
{
//super.paint(g);
if(isFirst)
{
isFirst=false;
}
g.drawImage(imgBoard, 0, 0, this);
}
}
From the sounds of it, the image is not being found because it is a internal resource.
You could try something like...
imgBoard = ImageIO.read(getClass().getResource("res/board.png"));
This will throw an IOException if the image cannot be loaded for some reason, which is more useful than what you're getting right now
As an aside. You should avoid painting directly to top level containers, but instead using something that extends from JComponent and override it's paintComponent method
Take a look at Performing Custom Painting and Reading/Loading an Image for more details
I found this code online and it works for my application. I'm trying to trace through and understand it more thoroughly. I don't find any documentation that explains the use of the "add(new Surface());" statement. I understand what it does, but here is what I don't understand:
How does the add() method work without a "SomeObject." in front of it. It seems to assume the add() method is for the object (SampleAddMethod) which contains it. I can't find any documentation on how or when this is valid.
Why does "super.add(new Surface()); work, but ""SampleAddMethod.add(new Surface());" fail? It seems the add() method is inherited from Component and SampleAddMethod is a JFrame which is a Component.
Am I correct that (new Surface()) creates an "anonymous class"?
(Sample code below)
package testMainMethod;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class Surface extends JPanel {
private void doDrawing(Graphics g, int x) {
double xd = (double) x;
Graphics2D g2d = (Graphics2D) g;
// Code to draw line image goes here
}
#Override
public void paintComponent(Graphics g) {
for (int i = 0; i < 512; i++) {
// super.paintComponent(g); // this erases each line
doDrawing(g, i);
}
}
}
public class SampleAddMethod extends JFrame {
public SampleAddMethod() {
initUI();
}
private void initUI() {
setTitle("Lines");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new Surface());
setSize(650, 350);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SampleAddMethod lines = new SampleAddMethod();
lines.setVisible(true);
}
});
}
}
Why does "super.add(new Surface()); work, but ""SampleAddMethod.add(new Surface());" fail?
Because add(Component) is an instance method on Container, basically - and SampleAddMethod is a subclass of Container, indirectly. So the add call in initUI is implicitly this.add(new Surface()). You can't call it as SampleAddMethod.add because that would only work if it were a static method.
Am I correct that (new Surface()) creates an "anonymous class"?
No. It's just calling a constructor. The code is equivalent to:
Surface surface = new Surface();
add(surface);
The only anonymous type in the code you've shown us is in main, where it creates a new anonymous class implementing Runnable.
I have an image and I want to display it in the applet, The problem is the image wont display. Is there something wrong with my code?
Thanks...
Here's my code :
import java.applet.Applet;
import java.awt.*;
public class LastAirBender extends Applet
{
Image aang;
public void init()
{
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
public void paint(Graphics g)
{
g.drawImage(aang, 100, 100, this);
}
}
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
I suspect you are doing something wrong, and that should be just plain:
aang = getImage(getDocumentBase(), "images.jpg");
What is the content of HTML/applet element? What is the name of the image? Is the image in the same directory as the HTML?
Update 1
The 2nd (changed) line of code will try to load the images.jpg file in the same directory as the HTML.
Of course, you might need to add a MediaTracker to track the loading of the image, since the Applet.getImage() method returns immediately (now), but loads asynchronously (later).
Update 2
Try this exact experiment:
Save this source as ${path.to.current.code.and.image}/FirstAirBender.java .
/*
<applet class='FirstAirBender' width=400 height=400>
</applet>
*/
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class FirstAirBender extends JApplet {
Image aang;
public void init() {
try {
URL pic = new URL(getDocumentBase(), "images.jpg");
aang = ImageIO.read(pic);
} catch(Exception e) {
// tell us if anything goes wrong!
e.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
if (aang!=null) {
g.drawImage(aang, 100, 100, this);
}
}
}
Then go to the prompt and compile the code then call applet viewer using the source name as argument.
C:\Path>javac FirstAirBender.java
C:\Path>appletviewer FirstAirBender.java
C:\Path>
You should see your image in the applet, painted at 100x100 from the top-left.
1) we living .. in 21century, then please JApplet instead of Applet
import java.awt.*;
import javax.swing.JApplet;
public class LastAirBender extends JApplet {
private static final long serialVersionUID = 1L;
private Image aang;
#Override
public void init() {
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
#Override
public void paint(Graphics g) {
g.drawImage(aang, 100, 100, this);
}
}
2) for Icon/ImageIcon would be better to look for JLabel
3) please what's getImage(getDocumentBase(), getParameter("images.jpg"));
there I'll be awaiting something like as
URL imageURL = this.getClass().getResource("images.jpg");
Image image = Toolkit.getDefaultToolkit().createImage(imageURL);
Image scaled = image.getScaledInstance(100, 150, Image.SCALE_SMOOTH);
JLabel label = new JLabel(new ImageIcon(scaled));
Well , above answers are correct. This is the code I used to display image. Hope it helps:
/*
<applet code = "DisplayImage.class" width = 500 height = 300>
</applet>
*/
import java.applet.Applet;
import java.awt.*;
public class DisplayImage extends Applet
{
Image img1;
public void init(){
img1 = getImage(getCodeBase(),"Nature.jpg" );
}
public void paint(Graphics g){
g.drawImage(img1, 0,0,500,300,this);
}
}
In above code, we create an image class object and get image from location specified by codebase. Then plot the image using drawImage method. Those who are interested in knowing value of getCodeBase() and getDocumentBase() methods can add following code in paint method. They are actually location of src folder in your project folder:-
String msg;
URL url=getDocumentBase();
msg="Document Base "+url.toString();
g.drawString(msg,10,20);
url=getCodeBase();
msg="Code Base "+url.toString();
g.drawString(msg,10,40);
One more point to note:- Make sure images and classes don't have same name in src folder. This was preventing my image to be displayed.