I'm having trouble displaying an image in an applet in eclipse. I'm wondering if there is any alternate way of doing this. I also want to know if there is a reliable online applet tester that displays an applet I made on the web. I'm following the oracle tutorials but they don't work. Here is my code:
Displaying class:
import java.applet.Applet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class usb extends Applet{
static BufferedImage background;
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void init() {
// TODO Auto-generated method stub
try {
URL url = new URL(getCodeBase(), "resources/usb_homescreen.png");
background = ImageIO.read(url);
} catch (IOException e) {
}
setSize(1000,500);
add(new goat());
}
}
Canvas class:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class goat extends Canvas {
/**
*
*/
private static final long serialVersionUID = 1L;
goat() {
setSize(1000,500);
setBackground(Color.white);
}
#Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(usb.background, 0, 0, null);
}
}
Any ideas about what's wrong?
Applets are executed in a special environment. When using them as plain objects, this environment is missing, i.e. the init method will never get called and the method getCodeBase() will not work. If you want to test an Applet there’s the program appletviewer.exe shipped with the JDK. It requires a HTML page containing an <applet> or <object> tag.
But there is also a way to instantiate an Applet within a stand-alone java application which will generate the required context. Given an Applet class “MyApplet” the necessary code looks like:
MyApplet applet=(MyApplet)java.beans.Beans.instantiate(
MyApplet.class.getClassLoader(), MyApplet.class.getName());
System.out.println(applet.getCodeBase());//prove that the context is now there
// now you can use applet like a normal AWT/Swing component
Related
Everything works in the program when the libraries with import javax.swing.* and import java.awt.*,but it doesn't work when something from these libraries is typed that doesn't offer awt and swing options in drop-down menu.
Example:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame{
public static MainFrame instance = null;
private MainFrame() {
inicijalizacija();
}
private void inicijalizacija() {
Toolkit kt=Toolkit.getDefaultToolkit();
Dimension velicina=kt.getScreenSize();
int visina=velicina.height;
int sirina=velicina.width;
setSize(visina/2,sirina/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("GeRuDok");
}
public static MainFrame getInstance() {
if(instance==null) {
instance=new MainFrame();
}
return instance;
}
}
e.g Toolkit cannot be released here as awt, but offers it as com.sun.javafx.tk and like others(Dimension,getScreenSize...)
I thought it had something to do with the content assistant, but it doesn't work either.
It may have something to do with the Java version 1.8 that the project is in, but I really have no idea how to fix it.
I'd appreciate it.
I'm just starting out in java and trying to use some example code I found online to get started, but for some reason, I am unable to compile this code. I on Ubuntu 16.04 and I have the "default-jdk" installed.
Here's the code:
import java.awt.*;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame{
public static void main (String argv [])
{
new Test("Window Application");
}
public Test(String title)
{
super(title);
setSize(200, 100);
addWindowListener((WindowListener) new WindowDestroyer());
setVisible(true);
}
private class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
When I try doing javac Test.java I get 2 cannot find symbol errors.
private class WindowDestroyer extends WindowAdapter
public void windowClosing(WindowEvent e)
From the Java 8 docs for WindowAdapter, it is defined as java.awt.event.WindowAdapter.
You need to import the class first:
import java.awt.event.WindowAdapter;
in addition to your other imports.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
As a side note, you might be tempted to just do
import java.awt.event.*;
to avoid import errors like that in the future.
I suggest reading the discussions on Why is using a wild card with a Java import statement bad? to have an idea on the pro's and con's of doing so.
I can see, that you create simple Swing application window and close it close window. You do it in incorrect way. It is much better to use setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) (only if you not plan to do smth. special before). And use SwingUtilities.invokeLater() to execute asynchronously on the AWT event dispatching thread:
public class Test extends JFrame {
public static void main(String... ars) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
public Test() {
super("Window Application");
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I have a Java Application, I am wanting to format a button as either Active or Inactive (Also possibly a hover method).
The code as I would like to implement it:
//Home Tab - Active by default
home = new TabButton();
home.setSize(new Dimension(tabWidth, tabHeight));
home.setFont(getLauncherFont(34));
home.setForeground(Color.white);
home.setText("HOME");
home.setBounds(160, 0, tabWidth, tabHeight);
home.setActive(); --> This Method is what I would like to create
I already have a class to create a JButton for the tab:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
}
I have researched abstract classes. But my TabButton class already extends JButton.
I would like a method like this:
public void setActive(){
this.setBackground(Color.red);
//Any other changes a want to make regularly
}
That can simply be implemented like this home.setActive();
My Question I suppose is: Is it easy enough to implement what I am looking for, or will I have to got the long way and set all attributes manually every time?
What you've described in the post can be done like this:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {// initialize
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
// add your own methods or override JButton methods
public void setActive(){
//Add code
//example: setEnabled(true);
}
}
I'm a beginner in Java and I followed a tutorial to write this program (yes, I that much of a beginner) and it works perfectly when I run it on Eclipse. It also runs great on the computer I coded it on. However, if I send it to another computer (just the .jar file) and run it, it fails because it can't find the icon. Here is everything I've got. The icon I'm using is saved in the bin folder along with all the class files for the program. For privacy reasons, I replaced certain lines with "WORDS".
The tutorial I followed in two parts:
Part 1 - https://buckysroom.org/videos.php?cat=31&video=18027
Part 2 - https://buckysroom.org/videos.php?cat=31&video=18028
My main class (I called it apples cause the tutorial did).
import javax.swing.JFrame;
public class apples {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(1920,1080);
go.setVisible(true);
}
}
And now my second class, "Gui":
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton custom;
public Gui () {
super("WORDS");
setLayout(new FlowLayout());
Icon b = new ImageIcon(getClass().getResource("b.png"));
custom = new JButton(null, b);
custom.setToolTipText("WORDS");
add(custom);
HandlerClass handler = new HandlerClass();
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
}
}
}
Thank you so much for helping!
It's worth reading Loading Images Using getResource where it's explained in detail along with loading images from jar as well.
You can try any one based on image location.
// Read from same package
ImageIO.read(getClass().getResourceAsStream("b.png"));
// Read from src/images folder
ImageIO.read(getClass().getResource("/images/b.png"))
// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/b.png"))
Read more...
I'm trying to play with LWUIT and siMple app like "hello world"..
But first line in StartApp() - Display.init(this) - causes app to close throwing uncaught exception..
I'm totally confused..
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Main extends MIDlet {
private int selectedIndex;
private Form f;
public Main() {}
public void startApp() {
System.out.println("before");
try {
System.out.println("during");
Display.init(this);
} catch(Exception h) {
System.out.println("after");
h.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
Please help.
Take a look on your imports. I think that the problem is there. I will remove the javax.microedition.lcdui.*;
Here in this web, you can see, how the Nokia UI Demo starts.
Nokia UI DEMO MIDlet
Another solution that I find looking on my code. Put the Display.init(this)in the Midlet constructor instead in the startApp method.
Before any Form is shown the Developer must invoke Display.init(Object m) in order to register the current MIDlet.