Reputation: 3
ok so i am trying to make a small java paint program but i am having trouble trying to get my buttons to work. I was able to get one color when i instantiated the MyPanel class, but i changed it around in an attempt to make use of my buttons. I basically have it setup to where each button activates a certain constructor in the MyPanel class and each constructor activates the appropriate methods to change the colors so I can draw. Im really new to programming so any advice you guys could give me would be much appreciated. I also have an extra class that is instantiated in the MyPanel class which i didnt bother putting in because i know the problem isnt in there. All the Red class does is set the color
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
public class Driver
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
SwingUtilities.isEventDispatchThread();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton red = new JButton("red");
JButton blue = new JButton("blue");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.setSize(1000,1000);
panel.add(red);
panel.add(blue);
blue.addActionListener( new Action() );
red.addActionListener( new Action() );
}
}//end driver class
public class Action implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
MyPanel d = new MyPanel();
}
}//end Action
public class MyPanel extends JPanel
{
private int xCoord = 50;
private int yCoord = 50;
private int squareW = 20;
private int squareH = 20;
Red red = new Red();
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.yellow));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveSquarer(e.getX(),e.getY());
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
moveSquarer(e.getX(),e.getY());
}
});
}
private void moveSquare(int x, int y)
{
if ((xCoord != x) || (yCoord!= y)) {
repaint(xCoord,yCoord,squareW+1,squareH+1);
red.setX(x);
red.setY(y);
repaint(red.getX(),red.getY(),red.getWidth()+1,red.getLength()+1);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("hi",10,20);
red.paintSquare(g);
} //end MyPanel
Upvotes: 0
Views: 85
Reputation: 17971
I basically have it setup to where each button activates a certain constructor in the MyPanel class and each constructor activates the appropriate methods to change the colors so I can draw.
I can't see it since your ActionListener is the same for both buttons:
private static void createAndShowGUI() {
...
blue.addActionListener( new Action() );
red.addActionListener( new Action() );
...
}
public class Action implements ActionListener {
@Override
public void actionPerformed( ActionEvent e ) {
MyPanel d = new MyPanel();
}
}
This method doesn't even add this new panel to the frame so it will never be visible.
Also what is this object?
Red red = new Red();
You may follow this suggested tips to achieve your goal:
MyClass
with a method to set the desired color.For instance:
public class MyPanel extends JPanel {
private Color color = Color.red;
// this color should be used within paintComponent() method
public void setColor(Color newColor) {
color = newColor;
repaint();
}
...
}
Upvotes: 1