Hello. I have made a program that draws a graph and such. However,
when I try to Maximize or change the window size I get these...ummm...
artifacts? appearing. It seems that some of my labels are being
reprinted with higher and higher Y-coordinates.
Is there a way to refresh everything or clear away the unwanted text?
A compileable example is below (Hopefully it's not to long...)
//
// Draw a graph
//
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
class Ass3_1 extends Frame {
class MyData { // data class
int currentX = 10;
int currentY = 10;
boolean brokerFeeStat = false;
int IBM[] = {28};
public void setX(int in_X){
currentX = in_X;
}
public void setY(int in_Y){
currentY = in_Y;
}
public void setBrokerFee(boolean in_Fee){
brokerFeeStat = in_Fee;
}
public int[] getIBM(int index){
switch (index) {
case 0:
return IBM;
}
return IBM;
}
public int getX() { return currentX; }
public int getY() { return currentY; }
public boolean getBrokerFee() { return brokerFeeStat; }
}
MyData theData = new MyData(); // data object
//Draw Y-axis
g.drawLine(150,70,150,200);
//Draw X-axis
g.drawLine(150,200,300,200);
//Add a Y-axis label
Label priceLabel = new Label("Purchase Price ($)"); //add a
label
priceLabel.setBounds(15,135,105,20);
thePanel.add(priceLabel);
//Draw small lines
int y = 180;
for(int i = 0; i<=4;i++){
g.drawLine(146,y,154,y);
y-=20;
}
//Write numbers
g.drawString("0",142,205);
int num = 185;
int myInt = 10;
for(int i=0;i<=4;i++){
g.drawString(Integer.toString(myInt),130,num);
num-=20;
myInt+=10;
}
//Add an X-axis label
Label companyLabel = new Label("Company"); //add a label
companyLabel.setBounds(200,245,60,20);
thePanel.add(companyLabel);
//Draw 3 small lines
g.drawLine(175,195,175,205);
g.drawLine(225,195,225,205);
g.drawLine(275,195,275,205);
//Draw company names
g.drawString("IBM",166,220);
g.drawString("Compaq",201,220);
g.drawString("Dell",264,220);
int base = 200;
int IBMValues[] = theData.getIBM(0);
//draw IBM graph
g.fillRect(151,(base - (IBMValues[0]*2)),50,(IBMValues[0]*2));
//Title
g.drawString("Profit/Loss Calculator",165,50);
}
}
MyPanel thePanel = new MyPanel(); //drawing object
//Extend the MouseAdapter
class MyPL extends MouseAdapter {
public void mousePressed(MouseEvent e){
theData.setX(e.getX());
theData.setY(e.getY());
thePanel.repaint();
}
}
class MyBL implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("The BrokerFee was: " +
theData.getBrokerFee());
theData.setBrokerFee(!theData.getBrokerFee());
System.out.println("the BrokerFee is now: " +
theData.getBrokerFee()+"\n");
}
}
// here we set up the frame
public void startup() {
this.setTitle("Assignment 3_1 Frame");
this.setSize(400,500);
this.setLayout(null); //align by hand
thePanel.setBounds(0,0,400,500);
this.add(thePanel);
this.setVisible(true);
this.addWindowListener(new ExitListener());
MyPL myMAdapt = new MyPL();
thePanel.addMouseListener(myMAdapt);
//Create a button
Button aButtonA = new Button("Toggle Broker Fee");
aButtonA.setBounds(165,280,110,40);
//Attach ActionListener to button
MyBL myAction = new MyBL();
aButtonA.addActionListener(myAction);
//Add button to Panel
thePanel.add(aButtonA);
}
class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
}
// boot up class
class ABootUp3_1 {
public static void main(String [] args) {
Ass3_1 theFrame = new Ass3_1();
theFrame.startup();
}
}
Roedy Green - 01 Feb 2006 02:17 GMT
>Hello. I have made a program that draws a graph and such. However,
>when I try to Maximize or change the window size I get these...ummm...
>artifacts? appearing. It seems that some of my labels are being
>reprinted with higher and higher Y-coordinates.
see http://mindprod.com/jgloss/flicker.html
Your problem is not flicker per se, but that information may be
helpful.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Weidenfeller - 01 Feb 2006 09:31 GMT
> A compileable example is below (Hopefully it's not to long...)
That fragment of course can't compile ...
From the code fragment and your vague description I can guess that you
are mixing Components with graphics drawing. That combination doesn't
work well in Java. You also use absolute coordinates and a null layout
manager (typically a very bad idea). And I can't find any code which in
any way is supposed to handle the graph resizing under this conditions
(no layout manager, no coordinate transformations). Oh, and you don't
start the GUI from the EDT (but that is a requirement Sun silently
introduced and is unlikely to affect you here).
You should fix all the issues one by one.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
fb - 02 Feb 2006 22:06 GMT
>> A compileable example is below (Hopefully it's not to long...)
>
[quoted text clipped - 12 lines]
>
> /Thomas
Thanks for the advise. I must admit, I don't know what a component is,
but I assume it's a button.
This is for a Java class. We haven't covered layout managers yet. But
I will look into it.
As for an EDT, I don't have a clue as to what that is...could you shed
some light on that one?
Thanks!
fb
Roedy Green - 03 Feb 2006 08:34 GMT
>Thanks for the advise. I must admit, I don't know what a component is,
>but I assume it's a button.
see http://mindprod.com/jgloss/jcomponent.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Weidenfeller - 03 Feb 2006 09:41 GMT
> Thanks for the advise. I must admit, I don't know what a component is,
> but I assume it's a button.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html
and every subclass of it.
> As for an EDT, I don't have a clue as to what that is...could you shed
> some light on that one?
See the FAQ and Sun's GUI tutorial.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
fb - 02 Feb 2006 21:59 GMT
> Hello. I have made a program that draws a graph and such. However,
> when I try to Maximize or change the window size I get these...ummm...
[quoted text clipped - 4 lines]
>
> A compileable example is below (Hopefully it's not to long...)
Actually I made an error. It did not compile in it's original form...It
seems I posted a copy I was in the process of editing...Sorry.
The program does what it's supposed to, it's merely a minor problem.
//
// Draw a graph
//
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
class Ass3_1 extends Frame {
class MyData { // data class
int currentX = 10;
int currentY = 10;
boolean brokerFeeStat = false;
int IBM[] = {28};
public void setX(int in_X){
currentX = in_X;
}
public void setY(int in_Y){
currentY = in_Y;
}
public void setBrokerFee(boolean in_Fee){
brokerFeeStat = in_Fee;
}
public int[] getIBM(int index){
switch (index) {
case 0:
return IBM;
}
return IBM;
}
public int getX() { return currentX; }
public int getY() { return currentY; }
public boolean getBrokerFee() { return brokerFeeStat; }
}
MyData theData = new MyData(); // data object
class MyPanel extends Panel { // component on which we will draw
public void paint(Graphics g) { // method in which component
painting occurs
//Draw Y-axis
g.drawLine(150,70,150,200);
//Draw X-axis
g.drawLine(150,200,300,200);
//Add a Y-axis label
Label priceLabel = new Label("Purchase Price ($)"); //add a
label
priceLabel.setBounds(15,135,105,20);
thePanel.add(priceLabel);
//Draw small lines
int y = 180;
for(int i = 0; i<=4;i++){
g.drawLine(146,y,154,y);
y-=20;
}
//Write numbers
g.drawString("0",142,205);
int num = 185;
int myInt = 10;
for(int i=0;i<=4;i++){
g.drawString(Integer.toString(myInt),130,num);
num-=20;
myInt+=10;
}
//Add an X-axis label
Label companyLabel = new Label("Company"); //add a label
companyLabel.setBounds(200,245,60,20);
thePanel.add(companyLabel);
//Draw 3 small lines
g.drawLine(175,195,175,205);
g.drawLine(225,195,225,205);
g.drawLine(275,195,275,205);
//Draw company names
g.drawString("IBM",166,220);
g.drawString("Compaq",201,220);
g.drawString("Dell",264,220);
int base = 200;
int IBMValues[] = theData.getIBM(0);
//draw IBM graph
g.fillRect(151,(base - (IBMValues[0]*2)),50,(IBMValues[0]*2));
//Title
g.drawString("Profit/Loss Calculator",165,50);
}
}
MyPanel thePanel = new MyPanel(); //drawing object
//Extend the MouseAdapter
class MyPL extends MouseAdapter {
public void mousePressed(MouseEvent e){
theData.setX(e.getX());
theData.setY(e.getY());
thePanel.repaint();
}
}
class MyBL implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("The BrokerFee was: " +
theData.getBrokerFee());
theData.setBrokerFee(!theData.getBrokerFee());
System.out.println("the BrokerFee is now: " +
theData.getBrokerFee()+"\n");
}
}
// here we set up the frame
public void startup() {
this.setTitle("Assignment 3_1 Frame");
this.setSize(400,500);
this.setLayout(null); //align by hand
thePanel.setBounds(0,0,400,500);
this.add(thePanel);
this.setVisible(true);
this.addWindowListener(new ExitListener());
MyPL myMAdapt = new MyPL();
thePanel.addMouseListener(myMAdapt);
//Create a button
Button aButtonA = new Button("Toggle Broker Fee");
aButtonA.setBounds(165,280,110,40);
//Attach ActionListener to button
MyBL myAction = new MyBL();
aButtonA.addActionListener(myAction);
//Add button to Panel
thePanel.add(aButtonA);
}
class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
}
// boot up class
class ABootUp3_1 {
public static void main(String [] args) {
Ass3_1 theFrame = new Ass3_1();
theFrame.startup();
}
}