| |||
font handling
Archived examples from previous versions of courses example from a Well House Consultants training course
More on Archived examples from previous versions of courses [link]
Source code: chough.java Module: J610
--- ARCHIVED EXAMPLE (may not be current) ---
--- See notes at http://www.wellho.net/mouth/2654.html --- // Well House Consultants, 2002. /** General Graphing Applet */ /* Fancy up the graphics and font handling! */ import java.awt.*; import java.util.*; import java.io.*; public class hen extends java.applet.Applet { // Data related - fixed across applets static int value_count; static int highest; static int [] income; static String title; static int start_year; // To share across instances to allow consistency static Font large_font, medium_font, small_font; // Applet instance specific static int window_width; static int window_height; // Extended / Replaced methods public void init() { // Values to Graph Vector income_build = new Vector(); String passed_in = this.getParameter("values"); StringTokenizer values_in = new StringTokenizer(passed_in,","); while (values_in.hasMoreTokens()) { Integer next_value = Integer.valueOf(values_in.nextToken().trim()); income_build.addElement(next_value); } income = new int [income_build.size()]; for (int k=0;k<income_build.size();k++) { income[k] = ((Integer)income_build.elementAt(k)).intValue(); } // What parameters *are* available?? // getParameterInfo // Title title = this.getParameter("title"); if (title == null) title = "Web Author has not supplied Title"; // Base Scale start_year = (Integer.valueOf(this.getParameter("base"))).intValue(); // Other initialisations large_font = new Font("TimesRoman",Font.BOLD,36); medium_font = new Font("TimesRoman",Font.ITALIC,18); small_font = new Font("TimesRoman",Font.PLAIN,9); } public void start() { } public void paint(Graphics mycanvas) { mycanvas.setColor(Color.white); // window_width = getSize().width-1; // Java 1.1 // window_height = getSize().height-1; window_width = size().width-1; // Java 1.0 window_height = size().height-1; Font headline_font, label_font; if (window_width > 500 && window_height > 400) { headline_font = large_font; label_font = medium_font; } else { headline_font = medium_font; label_font = small_font; } FontMetrics headline_metrics = getFontMetrics(headline_font); FontMetrics label_metrics = getFontMetrics(label_font); mycanvas.fillRect(0,0,window_width+1,window_height+1); highest = income[0]; for (int k=1;k<income.length;k++) if (highest < income[k]) highest = income[k]; value_count = income.length; mycanvas.setColor(Color.blue); for (int k=0;k<income.length;k++) { int xfrom = toXPixel(k); int xto = toXPixel(k+1); int yto = toYPixel(0); int yfrom = toYPixel(income[k]); mycanvas.fillRect(xfrom,yfrom,xto-xfrom,yto-yfrom); } mycanvas.setColor(Color.red); int xstep = getAxisStep(income.length); int ystep = getAxisStep(highest); grid(mycanvas,xstep,ystep); mycanvas.setColor(Color.blue); int textpos = toXPixel(income.length/2) - headline_metrics.stringWidth(title) / 2 ; if (textpos < 60) { textpos = toXPixel(income.length/2) - label_metrics.stringWidth(title) / 2 ; mycanvas.setFont(label_font); } else { mycanvas.setFont(headline_font); } mycanvas.drawString(title, textpos, window_height/2-10); mycanvas.setFont(label_font); for (int k=0; k<income.length;k+=(2*xstep)) { String year = String.valueOf(k+start_year); int xfrom = toXPixel(k)-label_metrics.stringWidth(year)/2; int yfrom = toYPixel(0)+label_metrics.getAscent()+1; mycanvas.drawString(year,xfrom,yfrom); } for (int k=0; k<highest;k+=(2*ystep)) { String level = String.valueOf(k); textpos = toXPixel(0)-5-label_metrics.stringWidth(level); int yfrom = toYPixel(k); mycanvas.drawString(level,textpos,yfrom); } } // I feel an object coming on here! private static int getAxisStep(int range) { int stepsize, step = 0, power = 1; int [] nice_steps = {1,2,5} ; while (range/(stepsize=(nice_steps[step%nice_steps.length]*power)) > 15) if (++step%nice_steps.length == 0) power *=10; return stepsize; } private static void grid(Graphics graph_area,int xsep, int ysep) { int y1 = toYPixel(0); int y2 = toYPixel(highest); for (int k=0;k<value_count;k+=xsep) { int xval = toXPixel(k); graph_area.drawLine(xval,y1,xval,y2); } int x1 = toXPixel(0); int x2 = toXPixel(value_count); for (int k=0;k<highest;k+=ysep) { int yval = toYPixel(k); graph_area.drawLine(x1,yval,x2,yval); } graph_area.drawRect(x1,y2,x2-x1,y1-y2); } private static int toXPixel (int value) { // Allow 80 pixels to left for labelling return value*(window_width-100)/value_count+80; } private static int toYPixel (int value) { // bottom half of window, allowing 40 pixels for title return window_height - value*(window_height-40)/highest/2 - 20; } public void stop() { } public void destroy () { } } Learn about this subject
This module and example are covered on our public Java Extra course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or on site course for you.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Java and associated technologies are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Archived examples from previous versions of courses" training module. You'll find a description of the topic and some
other closely related examples on the "Archived examples from previous versions of courses" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |