• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applets Bars

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this code which producers some 'verticle' colourd bars in an Applet. How should I alter the code to make the bars appear horizontal ?
heres the code:

package untitled4;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Color.*;

public class Applet1 extends Applet {
boolean isStandalone = false;

Color colours[] = {Color.black, Color.blue, Color.cyan,
Color.darkGray, Color.gray, Color.green, Color.lightGray,
Color.magenta, Color.orange, Color.pink, Color.red,
Color.white, Color.yellow};

public void paint(Graphics bars)
//this method displays the applet using the Graphics class to draw it.
{
int incX = 260/colours.length;
for (int i = 0; i < colours.length; i++)
{
bars.setColor(colours[i]);
bars.fillRect(i * incX, 0, (i + 1) * incX, 260);
}
}
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(260,300);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm "code"ing your post so that it's easier to read.

Originally posted by Michael Munro:
I have this code which producers some 'verticle' colourd bars in an Applet. How should I alter the code to make the bars appear horizontal ?
heres the code:

 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I forgot to mention, I have been told: "Simply add the code as is, without import statements or a 'class', to the previously generated program, replacing the similary named method therein."

thats what it says in my text book, which just sounds confusing.
 
Ken Loh
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I were you I would concentrate on the paint method as I could see that it calls fillRect().

As you want to change from displaying vertical to horizontal lines(or was it the other way around?) why not just swap the values for the x and y argument to fillRect() ?

Originally posted by Michael Munro:


 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I'm not sure how to do that

[ April 17, 2005: Message edited by: Michael Munro ]
[ April 17, 2005: Message edited by: Michael Munro ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the first step to solving the problem is to understand what the paint method currently is doing. Do you understand that?
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I undestand that, I think its the bit which has (graphics bars) in brackets, obviously the code needs to coorespond to that.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, how would you explain what the bars.fillRect line is doing?
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats where the lines appear in the applet but it doesn't control if the go verticle or horizontal. at least I don't think they do.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes, it does, kind of. Do you know what the parameters to the fillRect method mean? If not, you should research that at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I can do most of that, I can drawArc, drawRect, drawOval and put the measuments in etc...
But there is no infomation there for what I need to do. The code I have above displays vertical bars I need to alter that so they display horizontally. Its not a draw." " type code.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Munro:
Thats where the lines appear in the applet but it doesn't control if the go verticle or horizontal. at least I don't think they do.



1. If you look at just one bar (let's say the BLUE one), what determines whether it's horizontal or vertical?

2. Next, let's say all the bars are square (width==height). What would determine whether they were horizontal or vertical stripes?

I'll answer that one: It's their positions relative to each other. If the squares are in a stack on top of each other, they'd look like short wide horizontal stripes. On the other hand, if they are next to each other then they would look like short wide vertical stripes.

3. Ok, so how are "on top of each other" and "next to each other" expressed in terms of the method arguments in the code you have? i.e. Fill in the blanks in this: "The ___th argument of the ____ call for each successive colored bar is ____."

You've been led to the water, but you're going to have to do the drinking yourself.

Ryan
 
Mike Meakin
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iv'e don it now anyway:

int incY = 260/colours.length;
for (int i = 0; i < colours.length; i++)
{
bars.setColor(colours[i]);
bars.fillRect(0, i * incY, 260, (i + 1) * incY);



[ April 18, 2005: Message edited by: Michael Munro ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic