Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Creating Graphics

I am in a beginning Java class. The goal of this program is to change the background to yellow when the mouse enters, change the background to black when the mouse exits, create a small circle on left click, and create a large circle on right click. The mouse enter and exit methods work just fine, but i cannot get it to draw the circles. Any help would be greatly appreciated. Thanks.

[code]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JMouseFrame extends JFrame implements MouseListener {
int width = 0;
int height = 0;
int x = 0;
int y = 0;

public JMouseFrame(){
setTitle("Mouse Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
}

public void mouseClicked(MouseEvent e){
int whichButton = e.getButton();
if(whichButton == MouseEvent.BUTTON1){
width = 20;
height = 20;
repaint();
}
else if(whichButton == MouseEvent.BUTTON2){
width = 50;
height = 50;
repaint();
}
else
repaint();
}

public void mouseEntered(MouseEvent e) {
Container con = getContentPane();
con.setBackground(Color.YELLOW);
}

public void mouseExited(MouseEvent e) {
Container con = getContentPane();
con.setBackground(Color.BLACK);
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void paint(Graphics g){
super.paint(g);
g.drawOval(getX(), getY(), width, height);
}

public static void main(String[] args){
JMouseFrame frame = new JMouseFrame();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
[/code]

Sign In or Register to comment.