Changing colours of objects
Hey all,
1st let me say, this IS a school assignment, so I don't expect you to answer directly. However I would like a nudge or two in the right direction so I can actually complete the assignment.
Okay. We have to write a program which uses anonymous inner classes of Mouse Events to change the colour of a shape in a JPanel.
So...... this is what I have......
Circle class:
Code :
import java.awt.*;
// Class definition
public class Circle extends Point {
protected int size;
// Constructor method declaration
public Circle() {
super();
// set size and offset y co-ordinate to display below point
size = 50;
yPos = 100;
}
// Method to update new size of the circle
public void reSize(int newSize) {
size = size + newSize;
}
// An overiding method to display circle on screen as inherited method from class Point not suitable
public void display(Graphics g) {
// Call method fillOval from class Graphics to draw the circle of variable diameter of size
g.fillOval(xPos, yPos, size, size);
}
}
This just creates a circle from class called point. Simple enough.
Point class:
Code :
import java.awt.*;
// Class definition
public class Point {
protected int xPos, yPos;
// Constructor method declaration
public Point() {
xPos = 100;
yPos = 100;
}
// Method to update new position of point on screen
public void moveXY(int newX, int newY) {
xPos = xPos + newX;
yPos = yPos + newY;
}
// Cannot resize a point but method required to support polymorphism
public void reSize(int newSize) {
}
// Method to print X,Y co-ordinates of point on screen
public void outputXYCoords(Graphics g) {
g.drawString("X Coord = " + xPos + " Y Coord = " + yPos, 5, yPos-10);
}
// Method to draw point on screen at new X.Y position
public void display(Graphics g) {
g.fillOval(xPos, yPos, 5, 5);
}
}
Again, not very complicated.
Main code:
Code :
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import java.awt.Color.*;
import javax.swing.*;
import javax.swing.event.*;
// Class definition
public class PolyMorphDemo extends JPanel implements ActionListener {
private JLabel mouseStatus;
private int startX, startY, endX, endY, diffX, diffY;
private JButton makeBigger, makeSmaller;
private JComboBox shapeChoice;
private Point firstPoint;
private Circle firstCircle;
private Square firstSquare;
private Doughnut firstDoughnut;
private Point aShape;
// Constructor method declaration
public PolyMorphDemo() {
makeBigger = new JButton("Make Bigger");
add(makeBigger);
makeBigger.addActionListener(this);
makeSmaller = new JButton("Make Smaller");
add(makeSmaller);
makeSmaller.addActionListener(this);
shapeChoice = new JComboBox();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
add(shapeChoice);
shapeChoice.addActionListener(this);
// Instantiate shape classes
firstPoint = new Point();
firstCircle = new Circle();
firstSquare = new Square();
firstDoughnut = new Doughnut();
// Initially assign aShape a Point object
aShape = firstPoint;
// Create a new window using the Swing class JFrame and add this panel
makeFrame();
}
public void actionPerformed(ActionEvent e) {
// Manipulate respective object when buttons pressed
mouseStatus = new JLabel();
add(mouseStatus);
this.addMouseListener(new MouseAdapter() {
// This is the bit I'm having trouble with - this simply doesn't implement
Color color = Color.darkGray;
public void paint(Graphics g) {
g.setColor(this.color);
}
// The next bit works fine apart from the this.color line - it just doesn't recolour the shape to gray
public void mousePressed(MouseEvent me){
mouseStatus.setText("Mouse pressed X: " +
me.getX() + " Y: " + me.getY());
startX = me.getX();
startY = me.getY();
this.color = Color.darkGray;
repaint();
}
});
this.addMouseListener(new MouseAdapter() {
// Same problem as above
Color color = Color.black;
public void paint(Graphics g) {
g.setColor(this.color);
}
// Same as above - this moves the object fine
public void mouseReleased(MouseEvent me) {
mouseStatus.setText("Mouse released X: " +
me.getX() + " Y: " + me.getY());
endX = me.getX();
endY = me.getY();
diffX = endX - startX;
diffY = endY - startY;
mouseStatus.setText("Mouse has moved X: " +
diffX + " Y: " + diffY);
aShape.moveXY(diffX,diffY);
this.color = Color.black;
repaint();
}
});
if (e.getSource() == makeBigger) {
aShape.reSize(20);
}
else if (e.getSource() == makeSmaller) {
aShape.reSize(-20);
}
// checkbox assigns object to aShape
else if (e.getSource() == shapeChoice) {
if (shapeChoice.getSelectedItem().equals("Circle"))
aShape = firstCircle;
else if (shapeChoice.getSelectedItem().equals("Square"))
aShape = firstSquare;
else if (shapeChoice.getSelectedItem().equals("Point"))
aShape = firstPoint;
else if (shapeChoice.getSelectedItem().equals("Doughnut"))
aShape = firstDoughnut;
}
// Ask Java to repaint the window to reflect the updates made
repaint();
}
// Provide this method to instruct Java what to do when repainting the window
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Call the respective methods from respective class to print the new co-ordinate values
aShape.outputXYCoords(g);
// Call the respective methods from respective class to redraw the shape
aShape.display(g);
}
// Create a window frame
public void makeFrame() {
// Instantiate a window frame using Swing class JFrame
JFrame frame = new JFrame("PolyMorphDemo");
// When the window is closed terminate the application
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// set initial size of window
frame.setSize(800, 600);
// add the current object to the centre of the frame and make visible
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.setVisible(true);
}
}
As you can tell, there are also 2 other classes (square and doughnut) which create a square and a doughnut (who'd have thought it, eh? :D ) in place of a circle or point.
So basically, every piece of functionality (moving according to the mouse drag), making bigger, smaller etc. works.
The only problem is that I simply cannot get the object to change colour to gray when the mouse button is pressed, and turn back to black when it's released. I've tried several avenues to get this working, and none have worked, so if someone could provide a little aid, that'd be very much appreciated.
Thanks,
Tom.
Re: Changing colours of objects
Hey there tommyf, I know you didn't come here for a lecture, but It would be worthwhile for you to read up on Java best practices, as there are a few iffy code segments in the program - i.e. Avoid naming your classes the same as pre-existing libraries as It can cause some confusion (Point).
Instead of naming each and every issue as to why you can't achieve your goals, I made a basic program demonstrating painting with Java, while listening for mouse events.
Code java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public final class Drawing extends JPanel implements MouseMotionListener, MouseListener{
private int x,y; //x and y coords
private Color color; //colour of the circle
private final int CIRCLE_SIZE = 25; //size
public Drawing(){
super();
x = 0;
y = 0;
color = Color.PINK;
addMouseListener(this); //add listeners
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; //down cast
g2.setColor(color);
g2.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE); //draw circle
}
@Override
public void mousePressed(MouseEvent e) {
color = Color.YELLOW;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
color = Color.BLUE;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {}
public static void main(String... args){
final JFrame frame = new JFrame("JPF Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Drawing());
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
If you compile that program yourself, and read the code you should notice how mouse listeners work and a cleaner way to paint.
Obviously that is just an example and isn't immediately going to solve all your questions, which it isn't meant to, but hopefully you will learn through it.