on
Italy
- Get link
- X
- Other Apps
import java.awt.*;
import javax.swing.*;
public class OvalPaint extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.fillOval(getWidth()/4, getHeight()/4,
getWidth()/2, getHeight()/2);
}
public static void main(String args[]) {
JFrame frame = new JFrame("OvalPaint");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OvalPaint panel = new OvalPaint();
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
public class OvalPaint extends JPanel {
Here, we're making a subclass of JPanel which is a widget that we can add to a frame:JFrame frame = new JFrame("OvalPaint");
...
OvalPaint panel = new OvalPaint();
frame.add(panel);
The following block of code Graphics method which is the key in this
section. We'll never call this directly. The system calls it:public void paintComponent(Graphics g) {
...
}
Inside that block of code, we have: public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.fillOval(getWidth()/4, getHeight()/4,
getWidth()/2, getHeight()/2);
}
The Graphics.g is a kind of painting tool. We're
telling it what color to paint with and then what shape to paint, where
it goes, and how big it is.public void paintComponent(Graphics g) {}
The parameter g is a Graphics object. Actually, the object referenced by g is an instance of the Graphics2D class.Graphics2D graphics2d = (Graphics2D) g;Why do we care?
import java.awt.*;
import javax.swing.*;
public class OvalPaint extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D graphics2d = (Graphics2D)g;
graphics2d.setColor(Color.orange);
graphics2d.fillRect(0, 0, getWidth(), getHeight());
GradientPaint gradient = new GradientPaint (
getWidth()/4, getHeight()/4, Color.red,
getWidth()*3/4, getHeight()*3/4, Color.orange);
graphics2d.setPaint(gradient);
graphics2d.fillOval(getWidth()/4, getHeight()/4, getWidth()/2, getHeight()/2);
}
public static void main(String args[]) {
JFrame frame = new JFrame("OvalPaint with Gradient");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OvalPaint panel = new OvalPaint();
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WhichOne implements ActionListener
{
JFrame frame;
public static void main(String args[]) {
WhichOne w = new WhichOne();
w.go();
}
public void go() {
frame = new JFrame("Which One?");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawing drawPanel = new MyDrawing();
JButton colorButton = new JButton("Color");
JButton shapeButton = new JButton("Shape");
colorButton.addActionListener(this);
shapeButton.addActionListener(this);
frame.getContentPane().add(BorderLayout.WEST, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, shapeButton);
frame.setSize(300, 200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
class MyDrawing extends JPanel
{
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.fillOval(getWidth()/4, getHeight()/4,
getWidth()/2, getHeight()/2);
}
}
String messageColor = "Color Changed"; String messageShape = "Shape Changed"; ... colorButton.addActionListener(this); shapeButton.addActionListener(this);It's querying the event object to find out which button has been clicked:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == colorButton) {
System.out.println(messageColor);
} else {
System.out.println(messageShape);
}
frame.repaint();
}
This works!colorButton.addActionListener(new ColorButtonListener()); shapeButton.addActionListener(new ShapeButtonListener());However, we cannot use the reference variables such as messageColor and messageShape of the WhichOne class. These classes (ColorButtonListener and ShapeButtonListener) won't have access to the variables they need to act on, in the example, messages.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class WhichOne { JFrame frame; JButton colorButton; JButton shapeButton; String messageColor = "Color Changed"; String messageShape = "Shape Changed"; public static void main(String args[]) { WhichOne w = new WhichOne(); w.go(); } public void go() { frame = new JFrame("Which One?"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyDrawing drawPanel = new MyDrawing(); colorButton = new JButton("Color"); shapeButton = new JButton("Shape"); colorButton.addActionListener(new ColorButtonListener()); shapeButton.addActionListener(new ShapeButtonListener()); frame.getContentPane().add(BorderLayout.WEST, colorButton); frame.getContentPane().add(BorderLayout.CENTER, drawPanel); frame.getContentPane().add(BorderLayout.EAST, shapeButton); frame.setSize(300, 200); frame.setVisible(true); } } class ColorButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println(messageColor); } } class ShapeButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println(messageShape); } } class MyDrawing extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.orange); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.red); g.fillOval(getWidth()/4, getHeight()/4, getWidth()/2, getHeight()/2); } }
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WhichOne
{
JFrame frame;
JButton colorButton;
JButton shapeButton;
String messageColor = "Color Changed";
String messageShape = "Shape Changed";
public static void main(String args[]) {
WhichOne w = new WhichOne();
w.go();
}
public void go() {
frame = new JFrame("Which One?");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawing drawPanel = new MyDrawing();
colorButton = new JButton("Color");
shapeButton = new JButton("Shape");
colorButton.addActionListener(new ColorButtonListener());
shapeButton.addActionListener(new ShapeButtonListener());
frame.getContentPane().add(BorderLayout.WEST, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, shapeButton);
frame.setSize(300, 200);
frame.setVisible(true);
}
class ColorButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println(messageColor);
}
}
class ShapeButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println(messageShape);
}
}
}
class MyDrawing extends JPanel
{
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.fillOval(getWidth()/4, getHeight()/4,
getWidth()/2, getHeight()/2);
}
}
Comments
Post a Comment