I'm creating a Java program that uses Swing to draw a face, and then I am using MouseListener to respond to mouse clicks to make one of the eyes blink. How would I make one of the eyes blink using MouseListener? The method paint(Graphics g) can only be created once with that name, so if I want to repeat it and edit it under the MouseListener code with one of the eyes turned into a line for blinking, how would I do that?
Here is my code so far:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Sans extends JPanel {
public void paint(Graphics g) {
super.paintComponent(g);
setSize(650, 650);
g.drawOval(110, 250, 500, 275);
g.setColor(new Color(226, 222, 217));
g.fillOval(110, 250, 500, 275);
g.drawOval(475, 300, 75, 75);
g.setColor(new Color(74, 199, 226));
g.fillOval(475, 300, 75, 75);
g.drawOval(505, 330, 15, 15);
g.setColor(new Color(0, 0, 0));
g.fillOval(505, 330, 15, 15);
g.drawOval(175, 300, 75, 75);
g.setColor(new Color(0, 0, 0));
g.fillOval(175, 300, 75, 75);
g.drawOval(205, 330, 15, 15);
g.setColor(new Color(232, 255, 243));
g.fillOval(205, 330, 15, 15);
g.drawOval(350, 375, 20, 50);
g.setColor(new Color(0, 0, 0));
g.fillOval(350, 375, 20, 50);
g.drawArc(290, 360, 150, 150, 180, 180);
g.setColor(new Color(255, 255, 255));
g.fillArc(290, 360, 150, 150, 180, 180);
}
public static void main(String[] args) {
Font font = new Font("TimesRoman", Font.PLAIN, 15);
JFrame frame = new JFrame();
Sans spook = new Sans();
frame.add(spook);
frame.setSize(750, 750);
frame.setTitle("I'VE GOTTEN A TON OF WORK DONE TODAY. A SKELE-TON.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public class BlinkHandler implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
}
I am tring to get four side shadow effect for Tabbedpane.
My code follows like this,
MianClass
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
class NimbusBaseDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane;
int i;
private UIManager.LookAndFeelInfo[] lafs;
public NimbusBaseDemo() {
try {
// Set nimbus look and feel. nimbusBase works only for it.
new NimbusBaseUI();
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Nimbus Base Demo");
setSize(400, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
tabbedPane = new JTabbedPane();
tabbedPane.addTab("World Cities", new CitiesPanel());
tabbedPane.addTab("Colors ", new ColorsPanel());
tabbedPane.addTab("World Cities", new CitiesPanel());
add(tabbedPane);
}
public static void main(String args[]) {
new NimbusBaseDemo();
}
class CitiesPanel extends JPanel {
public CitiesPanel() {
JButton b1 = new JButton("New York");
add(b1);
JButton b2 = new JButton("London");
add(b2);
JButton b3 = new JButton("Hong Kong");
add(b3);
JButton b4 = new JButton("Tokyo");
add(b4);
}
}
class ColorsPanel extends JPanel {
public ColorsPanel() {
JCheckBox cb1 = new JCheckBox("Red");
cb1.setEnabled(true);
add(cb1);
JCheckBox cb2 = new JCheckBox("Green");
cb2.setEnabled(true);
add(cb2);
JCheckBox cb3 = new JCheckBox("Blue");
add(cb3);
}
}
}
I am extending NimbusLookAndFeel class to get ride of default theme for tabbed pane. by this all my tabbed pane's in my project follows same theme.
NimbusBaseUI class
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusBaseUI extends NimbusLookAndFeel {
public NimbusBaseUI() {
super(); // Initialisation and installating
try {
new TabbedPaneTheme(this);
UIManager.setLookAndFeel(this);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void initialize() {
// TODO Auto-generated method stub
super.initialize();
}
}
Finally i wrote a custome theme class to apply TabbedPane. here i put tabbedpane property parameters to getdefaults() method of NimbusLookAndFeel.
TabbedPaneTheme class
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.Painter;
import javax.swing.plaf.ColorUIResource;
public class TabbedPaneTheme {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
/** Color of shadow */
/** Color of shadow */
protected Color shadowColor = new Color(128, 128, 128, 140);
// protected Color shadowColor = new Color(0,0,0);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TabbedPaneTheme(NimbusBaseUI nimbusUI) {
// TODO Auto-generated constructor stub
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected].shadow",
ColorUIResource.BLUE);
nimbusUI.getDefaults().put("TabbedPane:TabbedPaneTab.contentMargins",
new Insets(0, 6, 10, 6)); // (// top, left, bottom, right)
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTabArea.contentMargins",
new Insets(0, 0, -6, 0));
nimbusUI.getDefaults().put("TabbedPane.tabAreaInsets",
new Insets(2, -1, -1, -5));
nimbusUI.getDefaults().put("TabbedPane.contentBorderInsets",
new Insets(-1, -1, -1, -5));
nimbusUI.getDefaults().put("TabbedPane.tabsOverlapBorder",
new Insets(-5, -5, -5, -5));
nimbusUI.getDefaults().put("Panel.opaque", false);
nimbusUI.getDefaults().put("TabbedPane.shadow", new Color(255, 0, 0));
nimbusUI.getDefaults().put("TabbedPane.focus", new Color(255, 0, 0));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Selected].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put("TabbedPane:TabbedPaneTab[Selected].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Selected].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+Pressed].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected+Pressed].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+Pressed].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Default+Enabled+Selected+Pressed+Focused].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Default+Enabled+Selected+Pressed+Focused].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Default+Enabled+Selected+Pressed+Focused].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Focused+Selected+MouseOver].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Focused+Selected+MouseOver].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Focused+Selected+MouseOver].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Focused+Selected].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Focused+Selected].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver+Pressed+Focused].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver+Pressed+Focused].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+Selected+MouseOver+Pressed+Focused].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled].backgroundPainter",
new TabbedPanePainter(new Color(98, 97, 93), new Color(127,
127, 119)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled].textForeground",
new Color(255, 255, 255));
nimbusUI.getDefaults().put("TabbedPane:TabbedPaneTab[Enabled].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter",
new TabbedPanePainter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+MouseOver].textForeground",
new Color(91, 113, 132));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTab[Enabled+MouseOver].font",
new Font("Myriad Pro", Font.BOLD, 12));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter",
new TabbedPane_TabView_Painter(
new Color(255, 255, 255), new Color(255, 255,
255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter",
new TabbedPane_TabView_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTabArea[Focused+Selected].backgroundPainter",
new TabbedPane_TabView_Painter(
new Color(255, 255, 255), new Color(255, 255,
255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneTabArea[Focused+Selected+MouseOver].backgroundPainter",
new TabbedPane_TabView_Painter(
new Color(255, 255, 255), new Color(255, 255,
255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Enabled].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Selected].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Focused].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Default].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.backgroundPainter",
new TabbedPane_TabContent_Painter(Color.WHITE, Color.WHITE));
nimbusUI.getDefaults().put("TabbedPane:TabbedPaneContent.background",
Color.WHITE);
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneContent.[Enabled+MouseOver].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255,
255), new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Selected].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneContent.[Enabled+Selected].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255,
255), new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneContent.[Enabled+Focused].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255,
255), new Color(255, 255, 255)));
nimbusUI.getDefaults().put(
"TabbedPane:TabbedPaneContent.[Selected].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255, 255),
new Color(255, 255, 255)));
nimbusUI.getDefaults()
.put("TabbedPane:TabbedPaneContent.[Enabled+Pressed].backgroundPainter",
new TabbedPane_TabContent_Painter(new Color(255, 255,
255), new Color(255, 255, 255)));
}
public class TabbedPane_TabContent_Painter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public TabbedPane_TabContent_Painter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
#Override
public void paint(Graphics2D g, Object c, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255),
false);
g.setPaint(gradPaint);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(188, 188, 187, 130));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setStroke(new BasicStroke());
}
}
public class TabbedPane_TabView_Painter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public TabbedPane_TabView_Painter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
#Override
public void paint(Graphics2D g, Object c, int w, int h) {
Color shadowColor = Color.black;
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), 150);
g.setColor(shadowColorA);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f),
(h / 2.0f), dark, true);
g.setPaint(gradPaint);
}
}
public class TabbedPanePainter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public TabbedPanePainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
#Override
public void paint(Graphics2D g, Object c, int w, int h) {
Color shadowColor = Color.black;
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), 150);
g.setColor(shadowColorA);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f),
(h / 2.0f), dark, true);
g.setPaint(gradPaint);
// g.fillRect(2, 2, (w - 5), (h - 5));
g.fillRoundRect(2, 2, (w - 5), (h - 5), 6, 6);
}
}
}
I got results like
But i want to acchive shadow effect four sides of tabbedpane and shadow for selected tabbedpane TAB also.
Like this
Appetiate your help & suggestions.
To get better help sooner:
Check this out: Nimbus Defaults (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel) and remove unnecessary keys from the TabbedPaneTheme class, e.g. TabbedPane:...font, TabbedPane:TabbedPaneContent.[Enabled+Pressed]...
Remove unrelated code: Color light, dark;, CitiesPanel, ColorsPanel class, NimbusBaseUI#initialize() method, ...
TabbedPane_TabView_Painter is not do anything.
Here's my attempt:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusTabbedPaneTest {
public JComponent makeUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("World Cities", new JScrollPane(new JTree()));
tabbedPane.addTab("Colors ", new JSplitPane());
tabbedPane.addTab("World Cities", new JScrollPane(new JTextArea()));
tabbedPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
return tabbedPane;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
try {
new NimbusBaseUI();
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame("Nimbus Base Demo");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new NimbusTabbedPaneTest().makeUI());
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class NimbusBaseUI extends NimbusLookAndFeel {
public NimbusBaseUI() {
super();
try {
new TabbedPaneTheme(this);
UIManager.setLookAndFeel(this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class TabbedPaneTheme {
protected static int OVERPAINT = 6;
protected static int shadowOffset = 1;
protected static int strokeSize = 2;
protected static Dimension arcs = new Dimension(10, 10);
public TabbedPaneTheme(NimbusBaseUI nimbusUI) {
UIDefaults d = nimbusUI.getDefaults();
d.put("TabbedPane:TabbedPaneContent.contentMargins", new Insets(0, 5, 5, 5));
//d.put("TabbedPane:TabbedPaneTab.contentMargins", new Insets(2, 8, 3, 8));
//d.put("TabbedPane:TabbedPaneTabArea.contentMargins", new Insets(3, 10, 4, 10));
d.put("TabbedPane:TabbedPaneTabArea.contentMargins",
new Insets(3, 10, OVERPAINT, 10));
Painter<JComponent> tabAreaPainter = new TabAreaPainter();
d.put("TabbedPane:TabbedPaneTabArea[Disabled].backgroundPainter",
tabAreaPainter);
d.put("TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter",
tabAreaPainter);
d.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter",
tabAreaPainter);
d.put("TabbedPane:TabbedPaneTabArea[Enabled+Pressed].backgroundPainter",
tabAreaPainter);
d.put("TabbedPane:TabbedPaneContent.backgroundPainter",
new TabContentPainter());
Painter<JComponent> tabPainter = new TabPainter(Color.ORANGE, false);
d.put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter",
tabPainter);
d.put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter",
tabPainter);
d.put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter",
tabPainter);
Painter<JComponent> selectedTabPainter = new TabPainter(Color.WHITE, true);
d.put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter",
selectedTabPainter);
d.put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter",
selectedTabPainter);
d.put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter",
selectedTabPainter);
d.put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter",
selectedTabPainter);
d.put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter",
selectedTabPainter);
d.put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter",
selectedTabPainter);
}
private static class TabPainter implements Painter<JComponent> {
private final Color color;
private int r = 6;
private int x = 3;
private int y = 3;
private boolean selected;
public TabPainter(Color color, boolean selected) {
this.color = color;
this.selected = selected;
}
#Override public void paint(Graphics2D g, JComponent c, int width, int height) {
int ex = selected ? OVERPAINT : 0;
Graphics2D g2 = (Graphics2D) g.create(0, 0, width, height + ex);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = width - 6 - 1;
int h = height + r;
for (int i = 0; i < 3; i++) {
g2.setColor(new Color(0, 0, 0, 20));
g2.fill(new RoundRectangle2D.Double(x - i, y - i, w + i + i, h, r, r));
}
g2.setColor(color);
g2.fill(new RoundRectangle2D.Double(x, y, w, h + 4, r, r));
g2.dispose();
}
}
private static class TabAreaPainter implements Painter<JComponent> {
#Override public void paint(Graphics2D g, JComponent c, int w, int h) {
Graphics2D g2 = (Graphics2D) g.create(0, 0, w, h);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RoundRectangle2D r = new RoundRectangle2D.Double(
shadowOffset,// X position
shadowOffset + h - OVERPAINT,// Y position
w - strokeSize - shadowOffset, // width
40, // height
arcs.width, arcs.height);// arc Dimension
g2.setPaint(Color.CYAN);
g2.fill(r);
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(strokeSize));
g2.draw(r);
g2.dispose();
}
}
private static class TabContentPainter implements Painter<JComponent> {
#Override public void paint(Graphics2D g, JComponent c, int w, int h) {
Graphics2D g2 = (Graphics2D) g.create(0, 0, w, h);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(0, -OVERPAINT);
h += OVERPAINT;
RoundRectangle2D r = new RoundRectangle2D.Double(
shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g2.setPaint(Color.WHITE);
g2.fill(r);
g2.setColor(Color.ORANGE);
g2.setStroke(new BasicStroke(strokeSize));
g2.draw(r);
g2.dispose();
}
}
}
I am trying to draw on a JPanel and add it to a JFrame in my createAndShowGui method. I have tried a few different things: creating the JPanel in the createAndShowGui method, adding the drawing to the JFrame, etc... The one thing that is common, I don't see any of my graphics!
Note: I am able to get the graphics to display in a JTabbedPane but not on a JPanel, which is what I actually want them to show up on to make the code more object oriented.
Edit:
Here is the working concept self contained example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DrawPanelMain extends JPanel {
/*
* Variables used to set the value of preferred height and width
*/
public static final double version = 0.0;
JPanel switchPanel = new JPanel();
JPanel testPanel = new JPanel();
JPanel btnPanel = new JPanel();
DrawEllipses drawEllipses = new DrawEllipses(POINT_LIST);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initializePointList();
createAndShowGui();
}
});
}
public static java.util.List<Point> POINT_LIST = new ArrayList<>();
/*
* This loop will initialize POINT_LIST with the set of points for drawing the ellipses.
* The for each loop initializes points for the top row and the second for loop draws the
* right triangle.
*/
public static void initializePointList() {
int ellipsePointsYCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620};
int ellipsePointsXCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620, 680};
int xx = 80;
for (int aXt : ellipsePointsXCoordinate) {
POINT_LIST.add(new Point(aXt, xx));
}
for (int i = 0; i < ellipsePointsYCoordinate.length; i++) {
for (int j = i; j < ellipsePointsYCoordinate.length; j++) {
POINT_LIST.add(new Point(ellipsePointsXCoordinate[i], ellipsePointsYCoordinate[j]));
}
}
}
public DrawPanelMain() {
testPanel.setBackground(Color.RED);
switchPanel.add(drawEllipses);
setLayout(new BorderLayout());
add(switchPanel, BorderLayout.CENTER);
add(testPanel, BorderLayout.EAST);
add(btnPanel, BorderLayout.SOUTH);
getPreferredSize();
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
}
public static void createAndShowGui() {
JFrame frame = new JFrame("RF Connection Panel " + version);
frame.setLayout(new BorderLayout());
frame.add(new DrawPanelMain());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(false);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
/*
* AddSwitchAction will add a new pane to the tabbedPane when the add switch button is clicked
*/
private class AddSwitchAction extends AbstractAction {
public AddSwitchAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
int index = 0;
DrawEllipses tabComponent = new DrawEllipses(POINT_LIST);
switchPanel.add(tabComponent, index++);
}
}
}
#SuppressWarnings("serial")
class DrawEllipses extends JPanel {
private final int PREF_W = 750; //Window width
private final int PREF_H = 750; //Window height
private static final int OVAL_WIDTH = 30;
private static final Color INACTIVE_COLOR = Color.RED;
private static final Color ACTIVE_COLOR = Color.green;
private java.util.List<Point> points;
private java.util.List<Ellipse2D> ellipses = new ArrayList<>();
private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();
/*
* This method is used to populate "ellipses" with the initialized ellipse2D dimensions
*/
public DrawEllipses(java.util.List<Point> points) {
this.points = points;
for (Point p : points) {
int x = p.x - OVAL_WIDTH / 2;
int y = p.y - OVAL_WIDTH / 2;
int w = OVAL_WIDTH;
int h = OVAL_WIDTH;
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
ellipses.add(ellipse);
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
MyMouseAdapter mListener = new MyMouseAdapter();
addMouseListener(mListener);
addMouseMotionListener(mListener);
}
/*
* paintComponent is used to paint the ellipses
*/
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ellipse2D ellipse : ellipses) {
g2.setColor(ellipseColorMap.get(ellipse));
g2.fill(ellipse);
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.draw(ellipse);
}
/*
* Set the font characteristics, color, and draw the row labels.
*/
g.setFont(new Font("TimesRoman", Font.BOLD, 18));
g.setColor(Color.BLACK);
//Along the top row
g.drawString("External Port", 10, 50);
g.drawString("1", 135, 50);
g.drawString("2", 195, 50);
g.drawString("3", 255, 50);
g.drawString("4", 315, 50);
g.drawString("5", 375, 50);
g.drawString("6", 435, 50);
g.drawString("7", 495, 50);
g.drawString("8", 555, 50);
g.drawString("9", 615, 50);
g.drawString("10", 672, 50);
//Along the Y-axis
g.drawString("Radio 2", 40, 145);
g.drawString("3", 90, 205);
g.drawString("4", 90, 265);
g.drawString("5", 90, 325);
g.drawString("6", 90, 385);
g.drawString("7", 90, 445);
g.drawString("8", 90, 505);
g.drawString("9", 90, 565);
g.drawString("10", 90, 625);
//Along the X-Axis
g.drawString("1", 135, 670);
g.drawString("2", 195, 670);
g.drawString("3", 255, 670);
g.drawString("4", 315, 670);
g.drawString("5", 375, 670);
g.drawString("6", 435, 670);
g.drawString("7", 495, 670);
g.drawString("8", 555, 670);
g.drawString("9", 615, 670);
//Draws a 3DRect around the top row of ellipse2D objects
g2.setColor(Color.lightGray);
g2.draw3DRect(120, 60, 580, 40, true);
g2.draw3DRect(121, 61, 578, 38, true);
g2.draw3DRect(122, 62, 576, 36, true);
}
/*
* MouseAdapter is extended for mousePressed Event that detects if the x, y coordinates
* of a drawn ellipse are clicked. If the color is INACTIVE it is changed to ACTIVE and
* vice versa.
*/
private class MyMouseAdapter extends MouseAdapter {
#Override
/*
* When mousePressed event occurs, the color is toggled between ACTIVE and INACTIVE
*/
public void mousePressed(MouseEvent e) {
Color c;
for (Ellipse2D ellipse : ellipses) {
if (ellipse.contains(e.getPoint())) {
c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
ellipseColorMap.put(ellipse, c);
}
}
repaint();
}
}
/*
* This method will set the dimensions of the JFrame equal to the preferred H x W
*/
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
switchPanel.add(title, tabComponent);
By default a JPanel uses a FlowLayout which respects the preferred size of the component. The preferred size of your component is (0, 0) so there is nothing to paint. Also, you are using the "title" string which is incorrect (and obsolete as has already been mentioned). That string represents a constraint for the layout manager. You can't just make up a String value. In any case FlowLayout does not accept any contraints so you should just be using:
switchPanel.add(tabComponent);
I am trying to draw on a JPanel
When doing custom painting you need to override the getPreferredSize() of the panel so the layout manager can use the information. If you don't override this method then the size is (0, 0) so there is nothing to paint.
Edit:
First some general comments:
Don't hardcode sizes of the panel. Your hardcoded size of (1200 x 750) is too large for my monitor. If you want full screen then use frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Post code that you are actually testing. As has already been mentioned your posted code doesn't even add the "switchPanel" to the frame.
You haven't updated the code to show how you override the getPreferredSize() method.
Finally, I see in your code that you add the panel dynamically to the visible GUI. In this case the general code should be:
panel.add(....);
panel.revalidate(); // to invoke the layout manager otherwise size is still (0, 0)
panel.repaint();
You are adding DrawEllipses instances to switchPanel using an obsolete method add(String,Component); you should use something like add(component, index). Also, You don't add switchPanel to anything (commented out in DrawPanelMain ctor).
I can't seem to get my head around this, I know that repaint() is involved but not sure how to use it.
Here is my code
public CarPanel()
{
repaint();
rects = new Rectangle[]
{
new Rectangle(25, 35, 30, 80, Color.GREEN,"8"),
new Rectangle(65, 35, 30, 80, Color.GREEN,"7"),
new Rectangle(105, 35, 30, 80, Color.GREEN,"6"),
new Rectangle(145, 65, 30, 50, Color.GREEN,"2"),
new Rectangle(185, 65, 30, 50, Color.green,"1"),
new Rectangle(25, 170, 30, 50, Color.GREEN,"13"),
new Rectangle(65, 170, 30, 50, Color.GREEN,"12"),
new Rectangle(105, 170, 30, 50, Color.GREEN,"11"),
new Rectangle(145, 170, 30, 50, Color.GREEN,"3"),
new Rectangle(190, 160, 92, 80, Color.BLACK,"Attendant Station"),
new Rectangle(25, 280, 30, 50, Color.GREEN,"15"),
new Rectangle(65, 280, 30, 50, Color.GREEN,"14"),
new Rectangle(105, 280, 30, 80, Color.GREEN,"10"),
new Rectangle(145, 280, 30, 80, Color.GREEN,"9"),
new Rectangle(185, 280, 30, 50, Color.GREEN,"5"),
new Rectangle(225, 280, 30, 50, Color.GREEN,"4")
};
}//end carPark
public void changeShapeColor(){
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.white);
for (Rectangle rect : rects) {
g.setColor(Color.BLACK);
g.drawString(rect.number, rect.x, rect.y);
g.setColor(rect.color);
g.fillRect(rect.x, rect.y, rect.w, rect.h);
}//end for
}//end paintComponent
}//end JPanel
When a condition is met elsewhere in the code I want to change the color from green to red.
Any help is greatly appreciated :)
public void changeShapeColor(Color newColor) {
for (Rectangle rect : rects) {
rect.color = newColor;
}
}
and then call the changeShapeColor-method from whenever your condition is changed. And make sure your component is invalidated, so it will be redrawn.
One way would be to change the Rectangle to take a color function instead of a color, so use a code structure like this:
public class Rectangle {
private ColorFunction function;
// ... etc
public Color getColor() { return function.getColor(); }
}
public interface ColorFunction {
public Color getColor();
}
public class MyColorChoice implements ColorFunction {
public Color getColor() {
return isConditionMet() ? Color.RED : Color.GREEN;
}
}