Text in applet
hello !!
my problem is i want to write a arabic text in my applet and show it with no probleme !!
Message was edited by: gotterez
gotterez
That can be done. See this tutorial:
http://java.sun.com/docs/books/tutorial/i18n/index.html
Similar Messages
-
how to set EXTERNAL URL link for text in applet
Click me
such type of external web page link is possible in applet
how
regards -
Pixel length of text in applet output useing drawString()
is thier a way to find the pixel length of a string of text when im going to output it useing drawString in a applet
to say i am going to "draw hello world" in a applet
i wish to use drawRect around it i know were it starts i want to know the length of the letters in pixel size
each one seems to differ so each letter has a unknown length i need to know the lenght of the combined letters of the string of text
or even each individual letter and i can write a algorithim to add um
this is a problem ive encoutered before never solved and now am encountering again.The TextLayout class is also handy in this area.
/* <applet code="TextSizeTest" width="400" height="300"></applet>
* use: >appletviewer TextSizeTest.java
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class TextSizeTest extends JApplet
public void init()
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new TextSizePanel());
public static void main(String[] args)
JApplet applet = new TextSizeTest();
JFrame f = new JFrame(applet.getClass().getName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(applet);
f.setSize(400,300);
f.setLocation(200,200);
applet.init();
applet.start();
f.setVisible(true);
class TextSizePanel extends JPanel
String text;
Font font;
public TextSizePanel()
text = "A test string";
font = new Font("lucida sans regular", Font.PLAIN, 24);
setBackground(Color.white);
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics metrics = font.getLineMetrics(text, frc);
float width = (float)font.getStringBounds(text, frc).getWidth();
float ascent = metrics.getAscent();
float descent = metrics.getDescent();
float x = (w - width)/2;
float y = (h + ascent)/2 - descent;
g2.drawString(text, x, y);
g2.setPaint(Color.red);
g2.draw(new Rectangle2D.Double(x, y - ascent, width, ascent + descent));
} -
my news applet scrolls text across the screen, the text however is flickering. my draw code is below.
screen2D.clearRect(0,0,getSize().width,getSize().height);
screen2D.drawString(obj.news[obj.currentItem],obj.x,obj.y);
repaint();
your help would be much appreciated
Arnold OreeDouble Buffering would seem to be the answer to your problems. I'm no expert but the following worked for me where I previous was drawing directly to the screen. Do all your drawing in the offscreen buffer area and then transfer it to the screen with the last statement in your paint method.
I've lifted some code for an applet I created a while back. Hopefully you can see what's going on.
Override the update method
as noted.
// buffer stuff
Graphics bufferGraphics;
Image offScreen;
Dimension dim;
public void paint (Graphics g)
bufferGraphics.clearRect(0,0,dim.width, dim.height);
bufferGraphics.setFont(new Font ("serif", Font.BOLD,18));
// set up board
for (int row = 0; row <=7; ++ row)
for (int column = 0; column <=7; ++ column)
// white squares
if ((row+column)%2 == 0)
bufferGraphics.setColor (new Color(0,0,0));
// black squares
if ((row+column)%2 == 1)
bufferGraphics.setColor (new Color (255,255,255));
// attacked squares
bufferGraphics.fillRect (40+(column*40), 75+(row*40), 40, 40);
if (board [row][column] !=0)
bufferGraphics.setColor (new Color(255,0,0));
bufferGraphics.drawString("X", 52 +(column*40), 101 +(row*40));
} // end row loop
} // end column loop
bufferGraphics.setColor (new Color (255,0,0));
bufferGraphics.drawLine (38,73,360,73); // top
bufferGraphics.drawLine (38,74,360,74); // top
bufferGraphics.drawLine (38,75,360,75); // top
bufferGraphics.drawLine (40,75,40,395); // left
bufferGraphics.drawLine (39,75,39,395); // left
bufferGraphics.drawLine (38,75,38,395); // left
bufferGraphics.drawLine (38,395,360,395); // bottom
bufferGraphics.drawLine (38,396,360,396); // bottom
bufferGraphics.drawLine (38,397,360,397); // bottom
bufferGraphics.drawLine (360,75,360,395); // right
bufferGraphics.drawLine (361,73,361,397); // right
bufferGraphics.drawLine (362,73,362,397); // right
bufferGraphics.setColor (new Color (255,0,0));
g.drawImage (offScreen, 0, 0, this);
} // end paint
public void update (Graphics g)
paint (g);
} // end update -
I know you can't download a file from an applet directly 'cause of the security contraints, what I'm asking is if anyone knows another simple way to take text written in the applet by the user and place it into a file and allow the user to download it. I know I can just send that info back to the server and have the user download it from there, but I'm hoping there's a way for the user to get it directly from the applet. Any help is greatly appreciated.
Im doing a program to do with my final year project and i have to perform a search when the user types in any search term and then sends the search to Google and the results are sent to a .txt file but i think that code will help you. It takes everything from the text field and puts it into a String called search_term
search_term = search_field.getText();
Hope this helps someway
[email protected] -
Implement color to text in applet
just wondering how you implement color into an applet then apply it to text that you have.
ThanksNot really sure what you're trying to do, but you can use the 2D api to set paint colors and assign them to a font. Take a look at the 2D api tutorial - it's really good.
-
Image,Text drag in applet
Hai there,
I want to allow a user to show an image and /or text on applet
and must allow it to be dragged.
I am able to do them separately oe. can drag image or text but
when both are present they act as single unit while dragging.
How should i allow each one to be dragged separately ?
Should i use seperate threads for both of them?// <applet code="DragTest" width="400" height="400"></applet>
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class DragTest extends JApplet
public void init()
DragPanel dragPanel = new DragPanel(getImage());
DragMeister listener = new DragMeister(dragPanel);
dragPanel.addMouseListener(listener);
dragPanel.addMouseMotionListener(listener);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(dragPanel);
private BufferedImage getImage()
String path = "images/Bird.gif";
ClassLoader cl = DragTest.class.getClassLoader();
BufferedImage image = null;
try
image = ImageIO.read(cl.getResourceAsStream(path));
catch(IOException ioe)
System.err.println("read error: " + ioe.getMessage());
return image;
public static void main(String[] args)
JApplet applet = new DragTest();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(applet);
f.setSize(400,400);
f.setLocation(200,200);
applet.init();
f.setVisible(true);
class DragPanel extends JPanel
BufferedImage image;
String text;
Rectangle imageRect;
Rectangle textRect;
public DragPanel(BufferedImage image)
this.image = image;
text = "hello world";
imageRect = new Rectangle(120, 200, image.getWidth(), image.getHeight());
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = g2.getFont().deriveFont(Font.ITALIC, 24f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text, frc);
if(textRect == null)
int w = (int)font.getStringBounds(text, frc).getWidth();
int h = (int)(lm.getAscent() + lm.getDescent());
textRect = new Rectangle(60, 40, w, h);
g2.setPaint(Color.blue);
g2.drawString(text, textRect.x, textRect.y + lm.getAscent());
g2.drawImage(image, imageRect.x, imageRect.y, this);
//g2.setPaint(Color.red);
//g2.draw(textRect);
//g2.draw(imageRect);
public void setRect(Rectangle r, int x, int y)
r.setLocation(x,y);
repaint();
public Rectangle[] getRects()
return new Rectangle[] { textRect, imageRect };
class DragMeister extends MouseInputAdapter
DragPanel dragPanel;
Rectangle selectedRect;
Point offset;
boolean dragging;
public DragMeister(DragPanel dp)
dragPanel = dp;
offset = new Point();
dragging = false;
public void mousePressed(MouseEvent e)
Point p = e.getPoint();
Rectangle[] rects = dragPanel.getRects();
for(int j = 0; j < rects.length; j++)
if(rects[j].contains(p))
selectedRect = rects[j];
offset.x = p.x - rects[j].x;
offset.y = p.y - rects[j].y;
dragging = true;
break;
public void mouseReleased(MouseEvent e)
dragging = false;
public void mouseDragged(MouseEvent e)
if(dragging)
int x = e.getX() - offset.x;
int y = e.getY() - offset.y;
dragPanel.setRect(selectedRect, x, y);
} -
HELP!!!!!! Applet has messed up my JSC
During the test of integrating applet into JSF pages, my JSC IDE suddently got a problem with applet. Please help me out!!!
My applet test project:- Open a new project
- Drop a Label, a Button, and a StaticText into page
- Set button action as "label1.setText("click");"
- Uncheck "escape" property of StaticText and Set "Text" to <applet/>
Compile and Run the project- A square box with "x" is shown (this is right for the applet in the page)
- BUT CLICKING BUTTON GETS NO ACTION!
If I remove <applet/> from StaticText, Everything is fine.
Do the same thing using JSC on another computer:- Everything works fine even though I integrate a real applet class.
Uninstall my JSC2
Delete folder /Program Files/Sun/Creator2, and /doc and settings/user/.Creator,
Clean up Registry and restart computer
Download JSC 2 update 1
Install JSC 2 update 1
Try the applet test project- EVEN WORSE! THE BUTTON DOES NOT SHOW UP!!!
- It only works if I remove the <applet/> from staticText
Can anyone here help me out? Any help is really appreciated!
Thanks in advance,I've tried in both and it looks like the IE submit somehow gets broken when there is a problem with the applet. You get the following exception in the Java Console.
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)I was able to get an applet working with a Creator app in IE when the <applet> tag contained valid applet information. Please try continuing to build the web application with a VALID set of applet tags (that won't break).
Cheers!
-David -
"Java Applet Window" appears in my popup menu
Hello
I have added a popup menu to my applet but whenever the popup menu appears, I also get the text "Java Applet Window" at its bottom.
What is causing this?
Can I get rid of it?Yes you can add a local user policy or local system policy that grants an AWT permission that regards "Java Applet Window" more precisely permission java.awt.AWTPermission "showWindowWithoutWarningBanner";
You can use a tool to add policy, create new policy files etc. Read about "policytool" in http://java.sun.com/j2se/1.4.1/docs/tooldocs/tools.html
Probably, signing tha applet jar, you can obtain necessary permission without modifing local policy. But I'm not sure.
Stefano. -
Hi.
I am hoping that someone can help me here. I am creating an Applet as part of a college project and need to be able to save and load data from a TextArea. I have managed to get the file to save but I can't seem to get it to load. No error message pops up after I load the same file, but no text appears in the TextArea. I am providing the class DiaryData and the code extracts for the Load and Save routes. Can someone tell me how to get this working please.
DiaryData
import java.io.*;
public class DiaryData implements Serializable
private String DiaryText;
// Default constructor
public DiaryData()
DiaryText = null;
// Set DiaryText to text in applet
public void setData(String passText)
DiaryText = passText;
// Return DiaryText
public String getData()
return DiaryText;
Load
void Bload_Action(ActionEvent event)
// Load data
try
FileDialog FileBox;
Frame MW = new Frame("MyFrame");
FileBox = new FileDialog(MW,"Open File",FileDialog.LOAD);
FileBox.setVisible(true);
myPath = FileBox.getDirectory() + FileBox.getFile();
File infile = new File(myPath);
FileInputStream inStream = new FileInputStream(infile);
ObjectInputStream myOIS = new ObjectInputStream(inStream);
DiaryData theDD = (DiaryData) myOIS.readObject();
myTextArea.setText(theDD.getData());
inStream.close();
myOIS.close();
catch(Exception e)
JOptionPane.showMessageDialog(null,"Error! " + e);
Save
void Bsave_Action(ActionEvent event)
// Save data
try
dd.setData(myTextArea.getText());
FileDialog FileBox;
Frame MW = new Frame("MyFrame");
FileBox = new FileDialog(MW,"Save File",FileDialog.SAVE);
FileBox.setVisible(true);
myPath = FileBox.getDirectory() + FileBox.getFile();
File outfile = new File(myPath);
FileOutputStream outStream = new FileOutputStream(outfile);
ObjectOutputStream myOOS = new ObjectOutputStream(outStream);
DiaryData theDD = new DiaryData();
myOOS.writeObject(theDD);
outStream.close();
myOOS.close();
catch(Exception e)
JOptionPane.showMessageDialog(null,"Error! " + e);
Any help is grately appreicated.DiaryData theDD = new DiaryData();
myOOS.writeObject(theDD);Duh! What do you think that writes to your file? Do you think it magically calls your setData method? You called setData on a "dd" instance but you're not writing that one, now are you? -
Hello Folks
I am having problem accesing the applet from the web application my applet runs fine if i run it in the IDE.
These are my methods in the bean
public String getApplet() {
applet = "<applet code=\"applet.PrinterApplet.class\" codebase=\".\" archive=\"SSignedPrinterApplet.jar\" width=\"400\" height=\"400\">"+
"<PARAM NAME=\"fileName\" VALUE=\"TITLE_12732.pdf\">"+
"<PARAM NAME=\"basePath\" VALUE=\"<%=basePath%>\">"+
"</applet>";
return applet;
//"<applet code=\"PrinterApplet.class\" width=\"300\" height=\"300\"/>";
public void setApplet(String applet) {
this.applet = applet;
here is my jsf call
<h:outputText value="#{activitySummaryBean.applet}" styleClass="labelTextBlack"></h:outputText>
it does not invoke the applet it just displays the text.
<applet code="applet.PrinterApplet.class" codebase="." archive="SSignedPrinterApplet.jar" width="400" height="400"><PARAM NAME="fileName" VALUE="TITLE_12732.pdf"><PARAM NAME="basePath" VALUE="<%=basePath%>"></applet>
I was refering to the link below
http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/applet.html?feed=DSC
please let me know your thoughts
Thanks,
SurendraHi Surendra,
I had same problem today. After a while i've found the solution:
You have to set the escape attribute of your <h:outputText /> tag to false. Because as default it's set to true and the HTML related character are escaped...
Result will be like this:
<h:outputText value="#{SessionBean.appletHTML}" escape="false" />Good luck,
Vaclav -
I want to run program in applet
I wrote program in applet using Fote for java CE , but can't run it .
please, help me!Sorry ;
My program is import the JApplet class And Also I implement HTML file
I use Fote For Java CE program to run it.
Thanks ;
My program is:
==============
// Java core packages
import java.awt.Graphics; // import class Graphics
// Java extension packages
import javax.swing.JApplet; // import class JApplet
public class WelcomeApplet extends JApplet {
// draw text on applet?s background
public void paint( Graphics g )
// call inherited version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
} // end class WelcomeApplet
My HTML file is:
================
<html>
<applet code = "WelcomeApplet.class" width = "300" height = "45">
</applet>
</html> -
Text ..... Help
hi,
I have very simple problem, i have to write a Text in applet( or canvas ),Text will contain multiply-line And Paragraph .Text must be not be editable.
Thank you....Simpler ways are to put it into one or more labels or an uneditable text area.
If you want to do it yourself you can use the LineBreakMeasurer and TextLayout
classes in a Canvas or Panel (AWT) or a JLabel or JPanel (Swing).
See LineBreakMeasurer api comments for examples. -
Hello
I just started a Java programming class and this is my first post
I tried running this Applet from our book but get the error "java.lang.NoSuchMethodError:
main Exception in thread "main".
// Fig. 3.9: WelcomeApplet2.java
// Displaying multiple strings in an applet.
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.JApplet; // import class JApplet
public class WelcomeApplet2 extends JApplet {
// draw text on applet�s background
public void paint( Graphics g )
// call superclass version of method paint
super.paint( g );
// draw two Strings at different locations
g.drawString( "Welcome to", 25, 25 );
g.drawString( "Java Programming!", 25, 40 );
} // end method paint
} // end class WelcomeApplet2
The book talks about needing an applet viewer which it has in the book cd. I didn't wish
to install the sdk from the book as it wants to unload the version already installed. So my
programs may uninstall with it. But nonetheless the error persists.
Any ideas?
Steve BrockHi
I'm not 100% sure whether that what you wanted but anyway ill show it to you :)
// Fig. 3.9: WelcomeApplet2.java
// Displaying multiple strings in an applet.
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.JApplet; // import class JApplet
import javax.swing.*;
import java.awt.*;
public class WelcomeApplet2 extends JApplet
JFrame frame = new JFrame(); //creating a Frame so you can use paint()
// to learn more about this go to java tutorials Swing
public void createFrame()
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupFrame(frame.getContentPane());
frame.pack();
frame.setSize(100,100);
frame.setVisible(true);
public void setupFrame(Container pane)
pane.add(new Panel());
public class Panel extends JPanel
// draw text on appletys background
public void paint( Graphics g )
// call superclass version of method paint
// super.paint( g );
// draw two Strings at different locations
g.drawString( "Welcome to", 25, 25 );
g.drawString( "Java Programming!", 25, 40 );
public static void main(String[] args) // always in java code
WelcomeApplet2 newObj = new WelcomeApplet2(); //creating an instance of this class
newObj.createFrame();
ok this code will work... and yours wasn't because you have to have main method, because after you have compiled your code, JVM searches class for this method to execute the program. -
hi
i am trying to print some text with applet but it is not working here is my code
sMsg="printing a applet is tough job";
// Find the default service
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintService service[]= PrintServiceLookup.lookupPrintServices(flavor, null);
// Create the print job
byte[] b=sMsg.getBytes();
DocPrintJob job = service[0].createPrintJob();
Doc doc = new SimpleDoc(b, flavor, null);
try{
job.print(doc, null);
}catch(Exception e){System.out.print(e.getLocalizedMessage());}
it is not doing anything not even the error is displayed
when i execute this code i get dialog box which ask me to allow this applet to print or not but after that ,nothing happens.
plz help me.
thanks in advance.Hans,
I came across a similar problem a few months ago. I came up with a work-around, and although it's not great, it met the demands of my current project.
A normal applet can't write files to a user's computer because that could create some big security problems. However, an applet can communicate with the server on which it's located. SO, I created a servlet that resides on the same server. When the user wants to print something from the applet, he clicks a Print button which opens a URLConnection and sends the data to the servlet. The servlet then creates an HTML document on the server and formats the data nicely. Then, back in the applet, I used this line to open a new browser window and show the HTML file:
getAppletContext().showDocument(new URL(theUrl), "_blank")
The user can then use the browser's print function to print the data.
I know it's kind of a roundabout way, but I was pressed for time. Let us know if you come up with something better.
-sheepy.
Maybe you are looking for
-
Attachment not getting created in PI from CRM
Hi Everyone, I am trying to create attachments in PI from CRM but it is not getting created there. I am executing the following steps: By default I am getting the attachment data in Binary format so I am using this function module to convert it into
-
Hi everybody, Im come from Vietnam, Im trying to purchase this plan (link) to save money ($19,99/month) because I will have a design course in Singapore next month. But my course during only in 7 months so can I have a discount for this plan? In addi
-
JSF 2.0: View Scope Problem
Hey, I 'm having trouble using the JSF 2.0 view scope. My application is a search page displaying some search parameters and a list of results. The search parameters are backed by a session scope bean, the results a backed by a request scoped bean an
-
HTTP 403 Error in AS2 Connection
Hi, Would really appreciate some inputs on this one. Just to give a background, we're tying to establish an AS2 connection between us and a 3rd party. The 3rd party tried to send a test message using our connecton details, but they got an HTTP 403 E
-
Custom Digital Signature Preference Edits
I have created some custom digital signatures using existing certificates and unique graphics. I have a question about being able to edit what appears to be "canned" reason statements available in the "Configure Signature Appearance" location in Sign