I have been trying the round button but there is a problem here. Currently I am getting a button like this :
What I want to do is fill the remaining portions in the button square. How do I do this ? This is the code that painted this button.
import javax.swing.*;
import java.awt.*;
class Tester extends JButton {
public Tester(String label) {
super(label);
setContentAreaFilled(false);
Dimension size = this.getPreferredSize();
size.width = size.height = Math.max(size.width,size.height);
this.setPreferredSize(size);
}
#Override
public void paintComponent(Graphics g) {System.out.println("Inside the paintComponent method");
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0,0,getSize().width-1,getSize().height-1);
System.out.println(getSize().width);
System.out.println(getSize().height);
super.paintComponent(g);
}
public static void main(String args[]) {
JFrame fr = new JFrame();
JPanel p = new JPanel();
JButton button = new Tester("Click Me !");
button.setBackground(Color.GREEN);
p.add(button);
fr.add(p);
fr.setVisible(true);
fr.setSize(400,400);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How do I fill the remaining portions in the button ?
You can just use the fillrect() method to fill the remaining portion before you call fillOval()
#Override
public void paintComponent(Graphics g) {
System.out.println("Inside the paintComponent method");
g.setColor(Color.red); // here
g.fillRect(0, 0, this.getWidth(), this.getHeight()); //here
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
System.out.println(getSize().width);
System.out.println(getSize().height);
super.paintComponent(g);
}
Doing like above I'll get
Hope this helps.
Simply use something like Graphics#fillRect to fill a rectangular area
public void paintComponent(Graphics g) {
System.out.println("Inside the paintComponent method");
// Or what ever background color you want...
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0,0,getSize().width-1,getSize().height-1);
System.out.println(getSize().width);
System.out.println(getSize().height);
super.paintComponent(g);
}
Related
I've done this plenty of times but I'm stuck. I've checked previous projects of mine and can't find the answer. The rectangle is supposed to show up at 400,400 and be 100,100 big, (Bottom right corner). When the start button is press I want it to show the rectangle. Thanks in advance!
!!!: There are multiple classes in this I just didn't post them since they don't have a use, if they do I'll post them.
class MazeRunner extends JFrame implements ActionListener, MouseMotionListener {
JButton b1;
int pkp = 0;
MazeRunner(){
b1= new JButton("Start");
add(b1);
b1.addActionListener(this);
b1.setBounds(10,10,50,50);
addMouseMotionListener(this);
setTitle("Maze Runner");
setLayout(null);
setSize(500, 500);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new MazeRunner();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1){
remove(b1);
pkp++;
validate();
repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
if (pkp == 1) {
if(e.getX() >= 400 && e.getX() <= 500 && e.getY() >= 400 && e.getY() <= 500) {
dispose();
}
}
}
protected void paintComponent(Graphics g) {
draw(g);
}
private void draw(Graphics g) {
if(pkp == 1) {
g.setColor(Color.BLACK);
g.fillRect(400, 400, 100, 100);
}
repaint();
}
}
First of all the method paintComponent() is missing it's constructor so it should look like
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
second of all, im not sure if Graphics g object has a method called "fillRect"
so I would recommend you to convert it to object of type Graphics2D, so in this case it would be:
public void draw(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if(pkp == 1) {
g2.setColor(Color.BLACK);
g2.fillRect(400, 400, 100, 100);
}
repaint();
}
I found a decorator pattern example with Swing component. The following code is drawing three buttons on a JFrame. One of the button has slash on it and implemented with Decorator pattern. The original one has paint() method, but I replaced paint() with paintComponent(Graphics g), then it fails to draw lines on the button. Is it impossible to use paintComponent() instead of paint()? If possible how to do that? What is the problem of this trial? What I am missing?
Original code link is Decorator pattern in Java.
public class DecoWindow extends JFrame implements ActionListener {
JButton Quit;
public DecoWindow() {
super("Deco Button");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel();
getContentPane().add(jp);
jp.add(new CoolDDecorator(new JButton("Cbutton")));
jp.add(new SlashDDecorator(new CoolDDecorator(new JButton("Dbutton"))));
jp.add(Quit = new JButton("Quit"));
Quit.addActionListener(this);
setSize(new Dimension(200, 100));
setVisible(true);
Quit.requestFocus();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
public static void main(String argv[]) {
new DecoWindow();
}
}
class DDecorator extends JComponent {
public DDecorator(JComponent c) {
setLayout(new BorderLayout());
add("Center", c);
}
}
class SlashDDecorator extends DDecorator {
int x1, y1, w1, h1;
public SlashDDecorator(JComponent c) {
super(c);
}
public void setBounds(int x, int y, int w, int h) {
x1 = x; y1 = y;
w1 = w; h1 = h;
super.setBounds(x, y, w, h);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.drawLine(0, 0, w1, h1);
}
}
class CoolDDecorator extends DDecorator {
boolean mouse_over; //true when mose over button
JComponent thisComp;
public CoolDDecorator(JComponent c) {
super(c);
mouse_over = false;
thisComp = this; //save this component
//catch mouse movements in inner class
c.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
mouse_over = true; //set flag when mouse over
thisComp.repaint();
}
public void mouseExited(MouseEvent e) {
mouse_over = false; //clear flag when mouse not over
thisComp.repaint();
}
});
}
//paint the button
public void paintComponent(Graphics g) {
super.paintComponent(g); //first draw the parent button
Graphics2D g2d = (Graphics2D) g;
//if the mouse is not over the button
//erase the borders
if (!mouse_over) {
Dimension size = super.getSize();
g2d.setColor(Color.lightGray);
g2d.drawRect(0, 0, size.width - 1, size.height - 1);
g2d.drawLine(size.width - 2, 0, size.width - 2, size.height - 1);
g2d.drawLine(0, size.height - 2, size.width - 2, size.height - 2);
}
}
}
paint() calls paintComponent() then paintChildren(). Your component is painting the slash and returning from paintComponent(). Then the default paint() implementation moves on, eventually painting the children, which are those buttons, which then just paint right ontop of your slash.
Your IDE should let you place a breakpoint in your paint code. You can check the callstack to see what's going on. If you aren't using an IDE, you can look at what Swing is doing by looking at JComponent.java in src.zip within your JDK.
Why do you want to use paintComponent() anyway?
I have a question. I created the Round Button, the code references from someone else cause I'm beginner. I want to set the position for it, but It seem to be unsuccessful. I thought the error is from its contains.
public final class ButtonDesigned extends JButton {
public ButtonDesigned (String label) {
super(label);
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,size.height);
setPreferredSize(size);
setContentAreaFilled(false);
}
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1,getSize().height-1);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1, getSize().height-1);
}
Shape shape;
public boolean contains(int x, int y) {
if (shape == null ||
!shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
public static void main(String[] args) {
JButton button = new ButtonDesigned ("Click");
button.setBackground(Color.gray);
button.setLocation(70, 70);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "you have clicked");
}
});
enter code here
JFrame frame = new JFrame();
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
I am trying to draw a circle when a button is pressed.
public Buttons(Panel panel){
addStud = new JButton("+ Student");
addStud.setToolTipText("Add a student");
addStud.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
//draw circle here
}
});
This is my button in Button.java
public void draw(Graphics g){
g.setColor(Color.BLUE);
g.fillOval(200, 200, 400, 50);
}
This is what I am trying to call in Panel.java
I have a public void paintComponent(Graphics g) but I don't want the circle to be drawn immediately.
I have tried initializing Panel() and calling it with no success. What can I do here?
class MyPanel extends JPanel {
Boolean drawBlue = false;
public void drawBlueCircle( Boolean draw ) {
drawBlue = draw;
repaint();
}
protected void paintComponent( Graphics g ) {
if ( drawBlue ) {
g.setColor(Color.BLUE);
g.fillOval(200, 200, 400, 50);
}
}
}
Then in your button's actionPerformed method call
myPanel.drawBlueCircle(true);
where myPanel is the instance of MyPanel that you created.
I need to draw a rectange,circle and line and then animate them(press left -> it(all objects) moves left for examp.)
I draw objects like this
class MyCanvas extends JComponent {
int x = 10;
int y = 10;
public MyCanvas()
{
Action someaction = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
x+=30;
//revalidate();
repaint();
}
};
this.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), someaction);
}
#Override
public void paintComponent(Graphics g) {
g.drawRect(x,y, 200, 200);
g.drawOval(x, y, 50, 50);
g.drawLine(x, y, 50, 30);
}
}
But it doesn't move.
use paintComponent(Graphics g) for Swing JComponents instead of paint(Graphics g)
use KeyBindings for KeyEvents for Swing JComponents
put Objects (prepare before paintComponent(Graphics g)) to the Array, paint elements from Array in paintComponent(Graphics g)