Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Frame

 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you imagined your screen being divisible into 4 quadrants; I am trying to write a program that does the following. By implementing the ActionListener interface and the Timer class, every 3 seconds fill one quadrant with the JFrame I created. Any idea on how I can get the frame to pop up in each quadrant?
// Begin Code

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

class Asignment3{
public static void main(String[] args){
GUI guiObj = new GUI();
}
}

class GUI extends Frame{
int res;
static final int ds = 72;//default scale, 72 units/inch
static final int hSize = 6;//horizonal size = 6 inches
static final int vSize = 6;//vertical size = 6inches

GUI(){//constructor
//Get screen resolution
res = Toolkit.getDefaultToolkit().
getScreenResolution();
//Set Frame size
this.setSize(hSize*res,vSize*res);
this.setVisible(true);
this.setTitle("Assignment 3");

//Window listener to terminate program.
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}//end constructor
//-----------------------------------------------------//

//Override the paint() method
public void paint(Graphics g){
//Downcast the Graphics object to
//a Graphics2D object
Graphics2D g2 = (Graphics2D)g;

//Scale device space to produce inches
//on the screen
// based on actual screen resolution.
g2.scale((double)res/72,(double)res/72);

//Translate origin to center of Frame
g2.translate((hSize/2)*ds,(vSize/2)*ds);

//Draw x-axis
g2.draw(new Line2D.Double(
-2.5*ds,0.0,2.5*ds,0.0));
//Draw y-axis
g2.draw(new Line2D.Double(
0.0,-2.5*ds,0.0,2.5*ds));

//Upper left quadrant, Solid red fill
Ellipse2D.Double circle1 =
new Ellipse2D.Double(
-2.0*ds,-2.0*ds,2.0*ds,2.0*ds);
g2.setPaint(new Color(120,8,8));//red
g2.fill(circle1);
g2.draw(circle1);


Ellipse2D.Double circle2 =
new Ellipse2D.Double(
0.0*ds,-2.0*ds,2.0*ds,2.0*ds);
g2.setPaint(
new GradientPaint(
0.5f*ds,-1.0f*ds,Color.green,
1.5f*ds,-1.0f*ds,Color.blue,false));
g2.fill(circle2);
g2.draw(circle2);

//Lower left quadrant
//Gradient red to orange, cyclic along
//horizontal axis
Ellipse2D.Double circle3 =
new Ellipse2D.Double(
-2.0*ds,0.0*ds,2.0*ds,2.0*ds);
g2.setPaint(
new GradientPaint(
-1.15f*ds,1.0f*ds,Color.yellow,
-0.85f*ds,1.0f*ds,Color.black,true));
g2.fill(circle3);
g2.draw(circle3);

//Lower right quadrant
//Gradient red to orange, cyclic along
// 45 degree angle
Ellipse2D.Double circle4 =
new Ellipse2D.Double(
0.0*ds,0.0*ds,2.0*ds,2.0*ds);
g2.setPaint(
new GradientPaint(
0.0f*ds,0.0f*ds,Color.pink,
0.25f*ds,0.25f*ds,Color.green,true));
g2.fill(circle4);
g2.draw(circle4);

}//end overridden paint()

}//end class GUI
//==============================//

// end code
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic