3. How do you specify a particular color to be used as fill when using the Graph
ID: 3563640 • Letter: 3
Question
3. How do you specify a particular color to be used as fill when using the Graphics class?
4. How would you create a SanSerif font of point size 14 that is bold and italic? Give an example.
5. Given a graphics object g, write a few lines of code to have that graphics object draw a green circle (filled in) that has a diameter of 100 pixels.
6. Given a graphics object g, write a few lines of code to draw a red rectangle (not filled in) with its upper left corner at position 25, 25 with a width of 100 and a height of 50.
7. What is the paintComponent method? How is it used in Java graphics? How does a program intentionally cause the paintComponent method to execute?
8. What does invoking the base class paintComponent method accomplish?
9. What window events can a program handle? Explain the difference between the WindowListener interface and the WindowAdapter.
10. What interface does an application need to use if it wants to get updates on the current position of the mouse cursor? How does the program get the x,y coordinates of the mouse cursor?
11. Write a few lines of code to create a timer in Java which fires an ActionEvent every 250 msec. Assume the existence of a TimerHdlr class which needs to respond to timer events.
12. Describe the types of information available to a program when using the KeyListener interface.
Explanation / Answer
1)The class defines coordinate systems in terms of the operations needed to transform the standard coordinate system (currently J2000) to the coordinate system associated with the object. Most coordinate systems will be defined as simple rotations, but some coordinate systems may involve more complext transformations. A factory method is available to generate Coordinate systems from a given string. Typically a string is comprised of an initial and an epoch (e.g., "B1950", "J1975.5"). Any string beginning with "G" is assumed to be Galactic coordinates.
By default, Java 2D uses the same coordinate system as AWT. The origin is in the upper-left corner of the drawing surface. X coordinate values increase to the right, and Y coordinate values increase as they go down. When drawing to a screen or an off-screen image, X and Y coordinates are measured in pixels. When drawing to a printer or other high-resolution device, however, X and Y coordinates are measured in points instead of pixels (and there are 72 points in one inch).
2)These are the various commands to do that:
drawRect():
This is the method of the Graphics class (The Graphics class is used to drawing different-different type of shapes). This method draws the rectangle. It takes some integer value as parameter. This method is written like : Graphics.drawRect(x, y, height, width);.
x - This is the variable represents the row no. or the x - coordinate.
y - This is also a variable represents the column no. or the y - coordinate.
drawOval():
This is the method of the Graphics class which draws the oval on the frame. This method takes argument same as the drawRect() method. In this method first come the width and then height is specified.
fillRect():
This is the method of the Graphics class which is used to fill rectangle with the specified color which is set before using the setColor() method of the Graphics class. It also takes argument same as the drawRect() method.
fillOval():
This is also the method of the Graphics class which is used to fill the oval with color specified in the setColor() method before. This method also takes argument same as the drawOval() method.
Here is a example
import javax.swing.*;
import java.awt.*;
public class DrawingColor{
public static void main(String[] args) {
DrawingColor d = new DrawingColor();
}
public DrawingColor(){
JFrame frame = new JFrame("Drawing with Alpha");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyComponent());
frame.setSize(400,400);
frame.setVisible(true);
}
public class MyComponent extends JComponent{
public void paint(Graphics g){
int height = 200;
int width = 120;
g.setColor(Color.red);
g.drawRect(10,10,height,width);
g.setColor(Color.gray);
g.fillRect(11,11,height,width);
g.setColor(Color.red);
g.drawOval(250,20, height,width);
g.setColor(Color.magenta);
g.fillOval(249,19,height,width);
}
}
}
3)Here is a example
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
public class DemoFont extends Applet {
private Font font1, font2, font3;
public void init(){
// create a font object: 12-point bold times roman
font1 = new Font( "Serif", Font.BOLD, 12 );
// create a font object: 24-point italic courier
font2 = new Font( "Monospaced", Font.ITALIC, 24 );
// create a font object: 14-point plain helvetica
font3 = new Font( "SansSerif", Font.PLAIN, 14 );
}
public void paint( Graphics g ) {
// set the current font to font1
g.setFont( font1 );
// draw a string in font font1
g.drawString( "Serif 12 point bold.", 20, 20 );
// change the current font to font2
g.setFont( font2 );
// draw a string in font font2
g.drawString( "Monospaced 24 point italic.", 20, 40 );
// change the current font to font3
g.setFont( font3 );
// draw a string in font font3
g.drawString( "SansSerif 14 point plain.", 20, 60 );
}
}
7 ,8)
The (very) short answer to your question is that paintComponent is called "when it needs to be." Sometimes it's easier to think of the Java Swing GUI system as a "black-box," where much of the internals are handled without too much visibility.
There are a number of factors that determine when a component needs to be re-painted, ranging from moving, re-sizing, changing focus, being hidden by other frames, and so on and so forth. Many of these events are detected auto-magically, and paintComponent is called internally when it is determined that that operation is necessary.
I've worked with Swing for many years, and I don't think I've ever called paintComponent directly, or even seen it called directly from something else. The closest I've come is using the repaint() methods to programmatically trigger a repaint of certain components (which I assume calls the correct paintComponent methods downstream.
In my experience, paintComponent is rarely directly overridden. I admit that there are custom rendering tasks that require such granularity, but Java Swing does offer a (fairly) robust set of JComponents and Layouts that can be used to do much of the heavy lifting without having to directly override paintComponent. I guess my point here is to make sure that you can't do something with native JComponents and Layouts before you go off trying to roll your own custom-rendered components.
9) 17 down vote accepted
WindowListener is interface which force you to override all of the methods, while WindowAdapter is implementation of WindowListener and you only need to override the method(s) that you interest to deal with.
WindowListener is interface which mean you cant instantiation the WindowListener, while WindowAdapter is concrete class that you can use new operator to instantiation.
When you use WindowAdapter, the code is more clean where your class only override the method(s) that you want. For example:
WindowListener
public class CloseListener implements WindowListener {
// im not interest on this event, but still need to override it
@Override
public void windowOpened(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
// im not interest on this event, but still need to override it
@Override
public void windowIconified(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowDeiconified(WindowEvent e) {
}
}
WindowAdapter
While using adapter the code is cleaner:
// at JFrame class
addWindowListener(new CloseListener());
// reusable Close Listener
public class CloseListener extends WindowAdapter {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
}
Or
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
So I would recommend to use WindowAdapter, but not must follow. However, two of the API about the same just that WindowAdapter exists as convenience for creating listener objects.
EDIT:
Since WindowListener is interface, you can implement it at your JFrame subclass.
public class MainWindow extends JFrame implements WindowListener {
// this is ok
}
public class MainWindow extends JFrame, WindowAdapter {
// this is not allow
}
But you cant do it with WindowAdapter.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.