My issues is that I have created a JPanel that draws a gradient as a background. But when I go to add components to it (like a JButton) it does nothing...
Please help!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CocoaTrashBar extends JPanel {
String titleText = "";
Color topLineColor = new Color(128, 128, 128);
Color bottomLineColor = new Color(69, 69, 69);
Color gradientTop = new Color(116, 116, 116);
Color gradientBottom = new Color(81, 81, 81);
public CocoaTrashBar() {
setDefaults();
}
public CocoaTrashBar(String title) {
setDefaults();
this.titleText = title;
}
public void setTitle(String title) {
this.titleText = title;
this.repaint();
}
private void setDefaults() {
super.setOpaque(true);
this.setPreferredSize(new Dimension(0, 24));
installWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
topLineColor = new Color(128, 128, 128);
bottomLineColor = new Color(69, 69, 69);
gradientTop = new Color(116, 116, 116);
gradientBottom = new Color(81, 81, 81);
repaintComponent();
}
public void windowLostFocus(WindowEvent e) {
topLineColor = new Color(171, 171, 171);
bottomLineColor = new Color(103, 103, 103);
gradientTop = new Color(156, 156, 156);
gradientBottom = new Color(121, 121, 121);
repaintComponent();
}
}, this);
}
private static void installWindowFocusListener(
WindowFocusListener focusListener, JComponent component) {
component.addPropertyChangeListener("Frame.active",
createFrameFocusPropertyChangeListener(focusListener, component));
}
private static PropertyChangeListener createFrameFocusPropertyChangeListener(
final WindowFocusListener focusListener, final JComponent component) {
return new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Window window = SwingUtilities.getWindowAncestor(component);
boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
if (hasFocus) {
focusListener.windowGainedFocus(
new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
} else {
focusListener.windowLostFocus(
new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
}
}
};
}
private void repaintComponent() {
this.repaint();
}
#Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
// Draw first line
g2d.setPaint(topLineColor);
g2d.drawLine(0, 0, this.getWidth(), 0);
// Draw last line
g2d.setPaint(bottomLineColor);
g2d.drawLine(0, 23, this.getWidth(), 23);
// Draw gradient
GradientPaint gradient = new GradientPaint(
this.getX(),
this.getY()+1,
gradientTop,
this.getX(),
this.getHeight()-1,
gradientBottom);
g2d.setPaint(gradient);
g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
if(titleText != null) {
g2d.setFont(new Font("", Font.BOLD, 13));
g2d.setColor(Color.WHITE);
g2d.drawString(titleText, 10, 16);
}
g.dispose();
}
}
the problem is with your paint method override calling paintComponent
replace your paint method with this:
protected void paintComponent(Graphics g)
{
if (isOpaque())
{
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
Graphics2D g2d = (Graphics2D) g;
// Draw first line
g2d.setPaint(topLineColor);
g2d.drawLine(0, 0, this.getWidth(), 0);
// Draw last line
g2d.setPaint(bottomLineColor);
g2d.drawLine(0, 23, this.getWidth(), 23);
// Draw gradient
GradientPaint gradient = new GradientPaint(this.getX(), this.getY() + 1, gradientTop, this.getX(), this.getHeight() - 1, gradientBottom);
g2d.setPaint(gradient);
g2d.fillRect(this.getX(), this.getY() + 1, this.getWidth(), this.getHeight() - 2);
if (titleText != null)
{
g2d.setFont(new Font("", Font.BOLD, 13));
g2d.setColor(Color.WHITE);
g2d.drawString(titleText, 10, 16);
}
}
In method paint(Graphics g), your first line is:
super.paintComponent(g);
This will paint your button. The rest of that method will proceed to paint over that button:
g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
Hence, you see no button. Make that first line the last line and you should be good.
Related
I am working on a class that can rotate a wheel around the center. The wheel is created using graphics2d, but I can not figure out exactly how to get the wheel to rotate around the center.
Currently, the wheel rotates, but not exactly about the origin.
My ultimate goal here is to create the wheel so that it is multicolored as well as a program around it, but my main concern here is getting the rotating wheel to work. If you could point me in the right direction I would be forever grateful!
Here is my current code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class RotateApp {
private static final int N = 3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(N, N, N, N));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new RotatePanel());
frame.pack();
frame.setVisible(true);
System.out.println();
}
});
}
}
class RotatePanel extends JPanel implements ActionListener {
private static final int SIZE = 256;
private static double DELTA_THETA = Math.PI / 90;
private final Timer timer = new Timer(25, this);
private Image image = RotatableImage.getImage(SIZE);
private double dt = DELTA_THETA;
private double theta;
public RotatePanel() {
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(
image.getWidth(null), image.getHeight(null)));
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
image = RotatableImage.getImage(SIZE);
dt = -dt;
}
});
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
}
public void actionPerformed(ActionEvent e) {
theta += dt;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
}
class RotatableImage {
private static final Random r = new Random();
static public Image getImage(int size) {
BufferedImage bi = new BufferedImage(
size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(10.0f));
g2d.draw(new Line2D.Double(0, 100, 100, 100));
g2d.draw(new Line2D.Double(100, 100, 200, 100));
g2d.draw(new Line2D.Double(100, 0, 100, 100));
g2d.draw(new Line2D.Double(100, 100, 100, 200));
g2d.draw(new Line2D.Double(25, 25, 100, 100));
g2d.draw(new Line2D.Double(100, 100, 175, 175));
g2d.draw(new Line2D.Double(175, 25, 100, 100));
g2d.draw(new Line2D.Double(100, 100, 25, 175));
g2d.draw(new Ellipse2D.Double(0, 0, 200, 200));
g2d.dispose();
return bi;
}
}
You can use the Rotated Icon class to do the rotation for you so you don't have to worry about all the rotation logic and the rotation logic is in a reusable class.
An example of using this class would be:
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class Rotation4 extends JPanel
{
private JLabel label;
private RotatedIcon rotated;
private int degrees;
public Rotation4(Image image)
{
setLayout( new GridBagLayout() );
Icon icon = new ImageIcon( image );
rotated = new RotatedIcon(icon, 0);
rotated.setCircularIcon(true);
label = new JLabel(rotated);
label.setOpaque(true);
label.setBackground(Color.RED);
add(label, new GridBagConstraints());
setDegrees( 0 );
}
public void setDegrees(int degrees)
{
this.degrees = degrees;
rotated.setDegrees( degrees );
label.revalidate();
label.repaint();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
Image bi = RotatableImage.getImage(210);
final Rotation4 r = new Rotation4(bi);
final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
r.setDegrees( value );
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(r));
f.add(slider, BorderLayout.SOUTH);
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
static class RotatableImage
{
private static final Random r = new Random();
static public Image getImage(int size)
{
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(10.0f));
g2d.draw(new Line2D.Double(5, 105, 205, 105));
g2d.draw(new Line2D.Double(105, 5, 105, 205));
g2d.draw(new Line2D.Double(35, 35, 175, 175));
g2d.draw(new Line2D.Double(175, 35, 35, 175));
g2d.draw(new Ellipse2D.Double(5, 5, 199, 199));
g2d.setColor(Color.BLACK);
g2d.fillOval(100, 100, 10, 10);
g2d.dispose();
return bi;
}
}
}
Note I also had to make changes with your image and your painting. These changes will need to be made whether you use the RotatedIcon or do the rotation code yourself:
The image size was changed to 210. This is because your stroke size is 10, so you need to account for the extra pixels in the circle outline.
You need to change the original of the circle by half the stroke size. So in this case the origin becomes (5, 5).
The size of the oval needs to be changed to 199. This is because of the way the outline of the oval is painted. 1 extra pixel is needed for the outline. If you leave the size at 200 then 1 pixel of the outline will be lost. This is not very noticeable when using a stroke size of 10, but if you use a size of 1, then the outline will be missing at the right and bottom edges.
The locations of your lines needs to be changes. you don't want the line right to the edge of the circle because then you will get a flat line at the edge instead of the rounded line. So I started the line 5 pixels from the start and ended it 5 pixels from the end.
Ok, with little modification and fewer "spokes", I got your wheel rotate centric (1.) and multicolor (2.):
UPDATE on 1.) To make centric rotation in your (original) code just chage SIZEto 200!
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class RotateApp {
private static final int N = 3;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(N, N, N, N));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new RotatePanel());
frame.pack();
frame.setVisible(true);
System.out.println();
});
}
}
class RotatePanel extends JPanel implements ActionListener {
private static final int SIZE = 256;
private static final double DELTA_THETA = Math.PI / 90;
private final Timer timer = new Timer(25, this);
private Image image = RotatableImage.getImage(SIZE);
private double dt = DELTA_THETA;
private double theta;
public RotatePanel() {
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(SIZE, SIZE));
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
dt = -dt;
image = RotatableImage.getImage(SIZE);
}
});
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(theta,128,128);
g2d.drawImage(image, 0, 0, null);
}
#Override
public void actionPerformed(ActionEvent e) {
theta += dt;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
}
class RotatableImage {
private static final Random r = new Random();
static public Image getImage(int size) {
BufferedImage bi = new BufferedImage(
size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
final Color c1 = Color.getHSBColor(r.nextFloat(), 1, 1);
final Color c2 = Color.getHSBColor(r.nextFloat(), 1, 1);
g2d.setPaint(c1);
g2d.setStroke(new BasicStroke(10.0f));
g2d.draw(new Line2D.Double(0, size/2, size, size/2));
g2d.setPaint(c2);
g2d.draw(new Line2D.Double(size/2, 0, size/2, size));
g2d.setPaint(c1);
g2d.draw(new Ellipse2D.Double(0, 0, size, size));
g2d.dispose();
return bi;
}
}
Explanation:
So the "wobble" in your solution came from the fact, that you sized the image and the container/panel 256x256, but "based" your wheel layout on 200x200 only. I fixed all dimensions, and drew a correct cross, the g2d.rotate(theta,128,128); (!) relates then to the center.
Multi-color(easy): You can invoke setPaint() between each shape! ;)
I am trying to make a button using the Rectangle object and am also trying to make the color change on hover, and it will not change.
I have made my code have more generic names for variables that way it would not confuse, here it is:
public class MouseHandler extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if(mx > button.x && mx < button.x+button.width &&
my > button.y && my < button.y+button.height) {
buttonHover = true;
} else {
buttonHover = false;
}
}
}
And I tried calling these lines of code also, but it wouldn't work:
if(buttonHover)
g.setColor(hoverColor);
g.drawRect(button.x, button.y, button.width, button.height);
I will put my full code at the bottom, with the actual variable names. Thanks for the help!
package trivia;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class Main extends JFrame{
boolean mainMenu = true;
boolean startHover;
static Color tan = Color.decode("#F4EBC3");
static Color darkGreen = Color.decode("#668284");
static Color buttonColor = Color.decode("#A2896B");
static Color borderColor = Color.decode("#333333");
static Color buttonHover = Color.decode("#F5B66E");
Rectangle header = new Rectangle(0, 0, 500, 100);
Rectangle body = new Rectangle(0, 100, 500, 400);
Rectangle start = new Rectangle(150, 150, 200, 40);
Rectangle howToPlay = new Rectangle(150, 225, 200, 40);
Rectangle quit = new Rectangle(150, 300, 200, 40);
public Main() {
setTitle("Trivia Game!");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
#Override
public void paint(Graphics g) {
Dimension d = this.getSize();
if(mainMenu = true){
g.setColor(darkGreen);
g.fillRect(header.x, header.y, header.width, header.height);
g.setFont(new Font("Courier", Font.BOLD, 24));
g.setColor(Color.BLACK);
drawCenteredString("Trivia Game!", d.width, 125, g);
g.setColor(tan);
g.fillRect(body.x, body.y, body.width, body.height);
g.setColor(buttonColor);
g.fillRect(start.x, start.y, start.width, start.height);
g.setColor(borderColor);
g.drawRect(start.x, start.y, start.width, start.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Start", d.width, 340, g);
g.setColor(buttonColor);
g.fillRect(howToPlay.x, howToPlay.y, howToPlay.width, howToPlay.height);
g.setColor(borderColor);
g.drawRect(howToPlay.x, howToPlay.y, howToPlay.width, howToPlay.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("How To Play", d.width, 490, g);
g.setColor(buttonColor);
g.fillRect(quit.x, quit.y, quit.width, quit.height);
g.setColor(borderColor);
g.drawRect(quit.x, quit.y, quit.width, quit.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Quit?", d.width, 640, g);
g.setColor(buttonColor);
g.fillRect(start.x, start.y, start.width, start.height);
g.setColor(borderColor);
g.drawRect(start.x, start.y, start.width, start.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Start", d.width, 340, g);
if(startHover)
g.setColor(buttonHover);
g.drawRect(start.x, start.y, start.width, start.height);
}
}
public void drawCenteredString(String s, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int x = (w - fm.stringWidth(s)) / 2;
int y = (fm.getAscent() + (h- (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
public static void main(String[] args) {
#SuppressWarnings("unused")
Main m = new Main();
}
public class MouseHandler extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if(mx > start.x && mx < start.x+start.width &&
my > start.y && my < start.y+start.height) {
startHover = true;
System.out.println("yes");
} else {
startHover = false;
System.out.println("no");
}
}
}
}
I'll start with...
1) If it truly just a rectangle you want to deal with. Please use https://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html , Java has been kind enough to make your life simple please don't throw it away. (ignore this keeping here for reference)
2) You should implement MouseMotionListener... I did it for you.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class test extends JFrame implements MouseMotionListener {
boolean mainMenu = true;
boolean startHover;
static Color tan = Color.decode("#F4EBC3");
static Color darkGreen = Color.decode("#668284");
static Color buttonColor = Color.decode("#A2896B");
static Color borderColor = Color.decode("#333333");
static Color buttonHover = Color.decode("#F5B66E");
Rectangle header = new Rectangle(0, 0, 500, 100);
Rectangle body = new Rectangle(0, 100, 500, 400);
Rectangle start = new Rectangle(150, 150, 200, 40);
Rectangle howToPlay = new Rectangle(150, 225, 200, 40);
Rectangle quit = new Rectangle(150, 300, 200, 40);
public test() {
setTitle("Trivia Game!");
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
this.addMouseMotionListener(this);
}
#Override
public void paint(Graphics g) {
Dimension d = this.getSize();
if (mainMenu = true) {
g.setColor(darkGreen);
g.fillRect(header.x, header.y, header.width, header.height);
g.setFont(new Font("Courier", Font.BOLD, 24));
g.setColor(Color.BLACK);
drawCenteredString("Trivia Game!", d.width, 125, g);
g.setColor(tan);
g.fillRect(body.x, body.y, body.width, body.height);
g.setColor(buttonColor);
g.fillRect(start.x, start.y, start.width, start.height);
g.setColor(borderColor);
g.drawRect(start.x, start.y, start.width, start.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Start", d.width, 340, g);
g.setColor(buttonColor);
g.fillRect(howToPlay.x, howToPlay.y, howToPlay.width,
howToPlay.height);
g.setColor(borderColor);
g.drawRect(howToPlay.x, howToPlay.y, howToPlay.width,
howToPlay.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("How To Play", d.width, 490, g);
g.setColor(buttonColor);
g.fillRect(quit.x, quit.y, quit.width, quit.height);
g.setColor(borderColor);
g.drawRect(quit.x, quit.y, quit.width, quit.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Quit?", d.width, 640, g);
g.setColor(buttonColor);
g.fillRect(start.x, start.y, start.width, start.height);
g.setColor(borderColor);
g.drawRect(start.x, start.y, start.width, start.height);
g.setFont(new Font("Courier", Font.BOLD, 20));
g.setColor(Color.black);
drawCenteredString("Start", d.width, 340, g);
if (startHover)
g.setColor(buttonHover);
g.drawRect(start.x, start.y, start.width, start.height);
}
}
public void drawCenteredString(String s, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int x = (w - fm.stringWidth(s)) / 2;
int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
public static void main(String[] args) {
#SuppressWarnings("unused")
test m = new test();
}
#Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("lol");
int mx = e.getX();
int my = e.getY();
if (mx > start.x && mx < start.x + start.width && my > start.y
&& my < start.y + start.height) {
startHover = true;
System.out.println("yes");
} else {
startHover = false;
System.out.println("no");
}
}
}
If you read below you can see there are a lot of things wrong with your code. Don't let that scare you from learning. Do it step by step and you will be fine.
You've several issues going on here:
You're doing drawing directly within a JFrame, a dangerous thing to do, since JFrames hold many components, several I'm sure that you're not familiar with, including borders, rootpane, glasspane, and contentpane, and if you mess up painting, it can mess up the drawing of these critical components.
Also by painting inside of a paint method, you loose all the advantages of Swing graphics.
Also you appear to have most of your program design and logic within a painting method, something that you should never do since you don't have full control over when or even if that method gets called.
Instead, you should create your button component in its own class, separate from the JFrame
Give your class ability to be placed in a JPanel
And give it a rollover capability.
For my money though, I'd just extend a JButton or better, just use a JButton, and make it look the way I want rather than trying to re-invent the wheel.
e.g.,
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
#SuppressWarnings("serial")
public class Main2 extends JPanel {
private static final Color TAN = Color.decode("#F4EBC3");
private static final Color DARK_GREEN = Color.decode("#668284");
private static final Color BUTTON_COLOR = Color.decode("#A2896B");
private static final Color BORDER_COLOR = Color.decode("#333333");
private static final Color BUTTON_ROLLOVER_COLOR = Color.decode("#F5B66E");
private static final String TITLE = "Trivia Game!";
private static final Font TITLE_FONT = new Font("Courier", Font.BOLD, 24);
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W - 30;
private JButton startButton;
private JButton howToPlayButton;
private JButton quitButton;
public Main2() {
JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
titleLabel.setFont(TITLE_FONT);
int blGap = 15;
titleLabel.setBorder(BorderFactory.createEmptyBorder(blGap, blGap, blGap, blGap));
JPanel titlePanel = new JPanel(new GridBagLayout());
titlePanel.setBackground(DARK_GREEN);
titlePanel.add(titleLabel);
JPanel centerInnerPanel = new JPanel(new GridLayout(0, 1, blGap, 2 * blGap));
centerInnerPanel.setOpaque(false);
centerInnerPanel.setBorder(BorderFactory.createEmptyBorder(blGap, blGap, blGap, blGap));
centerInnerPanel.add(startButton = createButton("Start"));
centerInnerPanel.add(howToPlayButton = createButton("How To Play"));
centerInnerPanel.add(quitButton = createButton("Quit?"));
JPanel centerOuterPanel = new JPanel(new GridBagLayout());
centerOuterPanel.setBackground(TAN);
centerOuterPanel.add(centerInnerPanel);
setLayout(new BorderLayout());
add(titlePanel, BorderLayout.PAGE_START);
add(centerOuterPanel, BorderLayout.CENTER);
}
private JButton createButton(String name) {
final JButton button = new JButton(name);
button.setFont(TITLE_FONT.deriveFont(20F));
button.setBackground(BUTTON_COLOR);
Border emptyBorder = BorderFactory.createEmptyBorder(5, 25, 5, 25);
Border lineBorder = BorderFactory.createLineBorder(BORDER_COLOR);
Border nestedBorder = BorderFactory.createCompoundBorder(lineBorder, emptyBorder);
button.setBorder(nestedBorder);
button.getModel().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel)e.getSource();
if (model.isRollover()) {
button.setBackground(BUTTON_ROLLOVER_COLOR);
} else {
button.setBackground(BUTTON_COLOR);
}
}
});
return button;
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
int w = Math.max(super.getPreferredSize().width, PREF_W);
int h = Math.max(super.getPreferredSize().height, PREF_H);
return new Dimension(w, h);
}
private static void createAndShowGui() {
Main2 mainPanel = new Main2();
JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
For some reason, a string drawn to a BufferedImage appears differently to one drawn straight to a JComponent.
Here's an example. The top string is drawn directly, while the bottom is drawn using a buffer.
What is going on here?
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Map;
public class Main {
static class Canvas extends JComponent
{
#Override
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);
g.drawString("OMFG look at this 'S'", 10, 20);
BufferedImage bi = new BufferedImage(150,50,BufferedImage.TYPE_INT_RGB);
Graphics2D imageG = bi.createGraphics();
imageG.setColor(Color.WHITE);
imageG.fillRect(0, 0, 150, 50);
imageG.setColor(Color.BLACK);
imageG.setFont(g.getFont());
imageG.drawString("OMFG look at this 'S'", 10, 10);
g.drawImage(bi, 0, 30, this);
}
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setMinimumSize(new Dimension(150, 80));
jf.add(new Canvas());
jf.setVisible(true);
}
}
I'm getting extremely pixilated corners when i try to make a rounded rectangle. Is there any way to smooth them out?
Here's an image (Notice the corners):
Here is the code for the Button that I subclass and override the paint method (The one with the pixilated corner):
public class ControlButton extends JButton {
public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
public ControlButton(String text) {
setText(text);
}
public ControlButton() {
}
#Override
protected void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g.create();
g2.setPaint(new GradientPaint(
new Point(0, 0),
BUTTON_TOP_GRADIENT,
new Point(0, getHeight()),
BUTTON_BOTTOM_GRADIENT));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2.dispose();
}
}
Try this:
RenderingHints qualityHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
qualityHints.put(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY );
g2.setRenderingHints( qualityHints );
Take a look at the documentation:
http://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
Code:
import javax.swing.*;
import java.awt.*;
public class ControlButton extends JButton {
public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
public ControlButton(String text) {
setText(text);
}
public ControlButton() {
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
RenderingHints qualityHints =
new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(qualityHints);
g2.setPaint(new GradientPaint(new Point(0, 0), BUTTON_TOP_GRADIENT, new Point(0, getHeight()),
BUTTON_BOTTOM_GRADIENT));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2.dispose();
}
public static void main(String args[]) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ControlButton("Hello, World"));
frame.pack();
frame.setVisible(true);
}
}
I did this:
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
I feel it gave me smoother edges than Dave Jarvis' method but I could be wrong.
Edited with a SSCCE and a workaround
I want to programmatically change the location of a JDialog.
public class SSCCE {
public static void main(String[] pArgs) {
JDialog dialog = new JDialog();
dialog.setSize(300, 300);
dialog.setLocation(10, 10);
dialog.setVisible(true);
}
}
However, this is not working, the new dialog is always located in the center of the screen. I suspect it has something todo with my L&F (Windows 7 64bit, jdk 1.6_30), when setting the dialog to be undecorated,
public class SSCCEWorksButUndecorated {
public static void main(String[] pArgs) {
JDialog dialog = new JDialog();
dialog.setSize(300, 300);
dialog.setLocation(10, 10);
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
dialog.setVisible(true);
}
}
The location is excalty where I specified.
But I need a decorated dialog, has anyone else seen this problem before or am I missing something?
Workaround by settings location after setVisible() for modal dialogs
public class SSCCEWorkaroundForModalDialogs {
public static void main(String[] pArgs) {
final JDialog dialog = new JDialog();
dialog.setSize(300, 300);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog.setLocation(10, 10);
}
});
dialog.setVisible(true);
}
}
The move method documentation says:
This method changes layout-related information, and therefore,
invalidates the component hierarchy.
The method changes the geometry-related data. Therefore, the native
windowing system may ignore such requests, or it may modify the
requested data, so that the Window object is placed and sized in a way
that corresponds closely to the desktop settings.
I assume you can do a call to revalidaite() before setting the dialog visible again.
I did it my way -
package com.whodesire.element;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class MessageEdge {
private JDialog messageEdge = null;
private JDialog tempMessage = null;
private Point elmLoc = null;
private String[] option;
private JButton btnOK;
private JLayeredPane lp = null;
private int top = 70;
MessageEdge(JFrame owner){
owner.setAlwaysOnTop(false);
}
public void ShowMessage(final String msg){
ShowMessage(msg, "OK");
}
public void ShowMessage(final String msg, final String... option){
this.option = option;
init();
}
protected void setRootPaneStroking(JRootPane rootPane){
rootPane.setWindowDecorationStyle(JRootPane.NONE);
KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
Action action = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
disposeMessage();
}
};
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", action);
};
private void init(){
top = 70;
tempMessage = new JDialog(){
private static final long serialVersionUID = 1L;
{
setUndecorated(true);
setType(Type.UTILITY);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBackground(new Color(0, 0, 0, 0));
elmLoc = Elementos.getElementosLocationPoint();
setSize(460, 57);
setLayout(new GridLayout(1, 1));
if(lp == null)
lp = getModelJLayeredPane();
add(lp);
setLocation(elmLoc.x-10, elmLoc.y+top);
setAlwaysOnTop(true);
validate();
setVisible(true);
}
};
new Thread(new Runnable(){
public void run(){
try{
OneMethod.playSound("catchyMessage");
whileLoop:
while(true){
//System.out.println("top value is : " + top + ", is top value bigger than -47 : " + (top > -47));
if(top > -47){
top -= 7;
tempMessage.setLocation(elmLoc.x-10, elmLoc.y+top);
}else{
top = -47;
initMessageEdge();
break whileLoop;
}
Thread.sleep(4);
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}).start();
}
private void initMessageEdge(){
tempMessage.setVisible(false);
tempMessage.remove(lp);
tempMessage.dispose();
//Elementos is my JFrame
messageEdge = new JDialog(Elementos.getFrame(), true){
private static final long serialVersionUID = 1L;
{
setUndecorated(true);
setType(Type.UTILITY);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setRootPaneStroking(getRootPane());
setBackground(new Color(0, 0, 0, 0));
requestFocusInWindow();
elmLoc = Elementos.getElementosLocationPoint();
setSize(460, 57);
setLayout(new GridLayout(1, 1));
if(lp == null)
lp = getModelJLayeredPane();
add(lp);
setLocation(elmLoc.x-10, elmLoc.y+top);
setAlwaysOnTop(true);
validate();
}
};
messageEdge.setVisible(true);
messageEdge.requestFocus();
}
private JLayeredPane getModelJLayeredPane(){
JLayeredPane layer = new JLayeredPane();
layer.setBounds(0, 0, 460, 57);
layer.setLayout(null);
JPanel panel = new JPanel(){
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics gr){
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.01f));
g.setColor(new Color(255, 196, 51));
g.fillRect(0, 0, 460, 57);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
g.setColor(new Color(255, 196, 51));
g.fillRect(0, 0, 460, 47);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
g.setColor(new Color(244, 171, 50));
int[] xxPoly = {0, 10, 10};
int[] yyPoly = {47, 47, 57};
Polygon pplg = new Polygon(xxPoly, yyPoly, xxPoly.length);
g.fillPolygon(pplg);
}
};
panel.setSize(450, 57);
panel.setOpaque(true);
panel.setLayout(null);
layer.add(panel);
btnOK = new JButton(){
private static final long serialVersionUID = 1L;
#Override
protected void paintComponent(Graphics gr) {
final Graphics2D g = (Graphics2D) gr.create();
g.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE,
new Point(0, getHeight()), new Color(255, 196, 51)));
g.fillRect(0, 0, getWidth(), getHeight());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(Color.BLACK);
g.setFont(new Font(OneMethod.getSegoeUIBoldFont().getFamily(), Font.BOLD, 11));
g.drawString(option[0], 8, 17);
g.dispose();
super.paintComponent(g);
}
};
btnOK.setFocusPainted(false);
btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
btnOK.setBounds(460-30, (int)((47/2)-(25/2)), 30, 25);
layer.add(btnOK);
btnOK.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent evt){
btnOK.setBorder(BorderFactory.createEmptyBorder());
btnOK.setBorder(BorderFactory.createLoweredBevelBorder());
}
#Override
public void mouseReleased(MouseEvent evt){
btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
disposeMessage();
Elementos.getFrame().setAlwaysOnTop(true);
}
});
layer.moveToFront(btnOK);
return layer;
}
private void disposeMessage(){
messageEdge.setModal(false);
messageEdge.setVisible(false);
messageEdge.dispose();
}
}
Hope it may help you and others, Cheers...