Reputation: 137
JButtons are not displayed until or unless I hover on the buttons.
package paint;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Rehan Shakir
*/
public class PaintFrame extends JFrame implements MouseMotionListener,ActionListener
{
private int x=0;
private int y=0;
private final JButton red,yellow,blue,green;
private final JPanel p;
//private final JLabel l;
private final Container c;
private Color color;
public PaintFrame()
{
setTitle("Paint");
setSize(800,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setLayout(new FlowLayout());
p = new JPanel();
p.setLayout(new GridLayout(4,1));
red = new JButton("Red");
red.setBackground(Color.red);
p.add(red);
yellow = new JButton("Yellow");
yellow.setBackground(Color.yellow);
p.add(yellow);
blue = new JButton("Blue");
blue.setBackground(Color.blue);
p.add(blue);
green = new JButton("Green");
green.setBackground(Color.green);
p.add(green);
red.addActionListener(this);
yellow.addActionListener(this);
blue.addActionListener(this);
green.addActionListener(this);
c = this.getContentPane();
c.setLayout(new BorderLayout());
JLabel l = new JLabel("Drag the mouse to draw",JLabel.RIGHT);
c.add(l,BorderLayout.SOUTH);
c.add(p,BorderLayout.WEST);
c.addMouseMotionListener(this);
setVisible(true);
}
@Override
public void mouseDragged(MouseEvent e )
{
x= e.getX();
y= e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e)
{
}
@Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Red"))
color = Color.red;
else if(s.equals("Yellow"))
color = Color.yellow;
else if(s.equals("Blue"))
color = Color.blue;
else if(s.equals("Green"))
color = Color.green;
else
color = Color.BLACK;
}
@Override
public void paint(Graphics g)
{
g.setColor(color);
g.fillOval(x, y, 4, 4);
}
}
Main Class
public class Paint {
package paint;
public static void main(String[] args) {
// TODO code application logic here
PaintFrame Jf = new PaintFrame();
}
}
When I execute this program only the first JButton is displayed on the screen. I have to hover on other buttons so that they can be displayed on the screen.
Why does this happen?
Upvotes: 2
Views: 331
Reputation: 285403
Immediately from your title, I could guess that you weren't calling the super's painting method in your override -- and I was right:
@Override
public void paint(Graphics g)
{
// ********** no super call here! *******
g.setColor(color);
g.fillOval(x, y, 4, 4);
}
By not doing this, you're not allowing the component to do its important house-keeping painting, including the painting of child components and removal of "dirty" pixels
This:
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(color);
g.fillOval(x, y, 4, 4);
}
will likely fix things
Having said this, I must add:
paintComponent
method override of a class that extends JPanel insteadsuper.paintComponent(g)
in your JPanel's overridden method (for the same reasons).Example using a BufferedImage:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class MainPaintPanel extends JPanel {
private PaintPanel paintPanel = new PaintPanel();
public MainPaintPanel() {
MyMouse myMouse = new MyMouse();
paintPanel.addMouseListener(myMouse);
paintPanel.addMouseMotionListener(myMouse);
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 3, 3));
addColorButton(btnPanel, Color.RED, "Red");
addColorButton(btnPanel, Color.YELLOW, "Yellow");
addColorButton(btnPanel, Color.BLUE, "Blue");
addColorButton(btnPanel, Color.GREEN, "Green");
setLayout(new BorderLayout());
add(paintPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.LINE_START);
}
class MyMouse extends MouseAdapter {
Point p1 = null;
private void moveOval(MouseEvent e) {
if (p1 == null) {
return;
}
Point p2 = e.getPoint();
paintPanel.addLine(p1, p2);
}
@Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
moveOval(e);
p1 = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
moveOval(e);
p1 = null;
}
}
private void addColorButton(JPanel btnPanel, Color color, String name) {
JButton button = new JButton(new ButtonAction(name, color));
button.setBackground(color);
btnPanel.add(button);
}
class ButtonAction extends AbstractAction {
private Color color;
public ButtonAction(String name, Color color) {
super(name);
this.color = color;
}
@Override
public void actionPerformed(ActionEvent e) {
paintPanel.setOvalColor(color);
}
}
private static void createAndShowGui() {
MainPaintPanel mainPanel = new MainPaintPanel();
JFrame frame = new JFrame("Painting GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
@SuppressWarnings("serial")
class PaintPanel extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final int OVAL_WIDTH = 4;
private static final Stroke BASIC_STROKE = new BasicStroke(OVAL_WIDTH);
private BufferedImage img;
private Color ovalColor;
public PaintPanel() {
img = new BufferedImage(PREF_W, PREF_W, BufferedImage.TYPE_INT_ARGB);
}
public void addLine(Point p1, Point p2) {
if (img != null && ovalColor != null) {
Graphics2D g2 = img.createGraphics();
g2.setStroke(BASIC_STROKE);
g2.setColor(ovalColor);
g2.drawLine(p1.x, p1.y, p2.x, p2.y);
g2.dispose();
repaint();
}
}
public void setOvalColor(Color ovalColor) {
this.ovalColor = ovalColor;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
Upvotes: 3