How to draw from multiple methods in Java? - java

Okay, so I'm trying to make a smiley face program, when it first starts up it's supposed to show the default smiley face(this part works and it's great I guess)
But then it should give you two buttons to pick from the smile and the frown buttons which should redraw the faces to show one smiling or frowning but it doesn't work for some reason.
I've been reading about graphics and I know you're not supposed to call them from outside the paint() method so I changed my code accordingly and I can tell the buttons are working because I make them print out something each time but the actual redrawing isn't working. I've tried using repaint() and revalidate() as well. For some reason if you use repaint() it repaints more and more each time which is odd, but maybe it's supposed to behave that way?
Can someone please take a look at the code and let me know what you think is the problem or where I should be looking for a solution I've used java for a while but I never use graphics :/ I've read your supposed to use setVisible/setSize(or pack()) at the end and that actually helped with some issues I had earlier but I don't get what to do when you want to draw multiple things most examples only show drawing one thing.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JSmileFacePanel2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton smile = new JButton("SMILE");
JButton frown = new JButton("FROWN");
public JSmileFacePanel2() {
setLayout(new FlowLayout());
setTitle("JSmileFace-V2: Jose M. Tobar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(frown);
add(smile);
setSize(800, 800);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
//by default should show smiling
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
smile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("SMILE BUTTON CLICKED");
drawSmile(g);
}
});
frown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("FROWN BUTTON CLICKED");
drawFrown(g);
}
});
}
public void drawSmile(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
repaint();
}
public void drawFrown(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
repaint();
}
public static void main(String[] args) {
JSmileFacePanel2 js = new JSmileFacePanel2();
}
}

You are still calling painting methods outside the paint method, because the actionPerformed methods different methods than the paint method. It doesn't matter that they are textually inside it, it's still a different method.
Also, you are repeatedly adding the action listeners every time that there is a repaint event, which will slow your application down to a halt and it will not repaint correctly either.
So, check whether you should smile or frown inside the paint method and add the action listeners inside the constructor.
The code then becomes:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JSmileFacePanel2 extends JFrame {
private static final long serialVersionUID = 1L;
// by default should show smiling
private boolean doSmile = true;
JButton smile = new JButton("SMILE");
JButton frown = new JButton("FROWN");
public JSmileFacePanel2() {
setLayout(new FlowLayout());
setTitle("JSmileFace-V2: Jose M. Tobar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(frown);
add(smile);
smile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("SMILE BUTTON CLICKED");
doSmile = true;
repaint();
}
});
frown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("FROWN BUTTON CLICKED");
doSmile = false;
repaint();
}
});
setSize(800, 800);
setVisible(true);
}
public void paint(final Graphics g) {
super.paint(g);
if (doSmile) {
drawSmile(g);
} else {
drawFrown(g);
}
}
public void drawSmile(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
}
public void drawFrown(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
}
public static void main(String[] args) {
JSmileFacePanel2 js = new JSmileFacePanel2();
}
}

In a word, no. But, you're on the right track though.
Remember, you're running in an event driven environment, meaning that something happens and then you respond to it.
Painting should do nothing more then just paint the current state, in your example, every time paint is called, you're adding a new ActionListener to the buttons, so you could end up with 3+ ActionListeners attached to each of your buttons just when the screen is made visible, which will result in some seriously weird behavior.
You should also not modify the state of the component, directly or indirectly from within your paint methods, which may cause a repaint to be scheduled, this will result in your paint method been called repeatedly, which will eventually consume your CPU cycles, messy to say the least.
You should also avoid extending from top level containers and overriding paint (generally), preferring instead to use a JPanel and overriding it's paintComponent method instead.
Two main reasons for this, first, it locks you into a single use case, meaning you can't re-use your components and secondly painting directly to a top level container like JFrame could have you painting underneath the frame's decorations/borders and because of the way painting works, even having the frames content painting over it.
See Painting in AWT and Swing and Performing Custom Painting for more details
The general solution is to use a flag to change the way in which your painting process works and change this flag accordingly when you want to (from within the button's ActionListeners for example)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JSmileFace {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new JSmileFace();
}
public JSmileFace() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SmilyPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SmilyPane extends JPanel {
JButton smile = new JButton("SMILE");
JButton frown = new JButton("FROWN");
private boolean frowning = false;
public SmilyPane() {
setLayout(new FlowLayout());
add(frown);
add(smile);
smile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frowning = false;
repaint();
}
});
frown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("FROWN BUTTON CLICKED");
frowning = true;
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
if (frowning) {
drawFrown(g);
} else {
drawSmile(g);
}
}
public void drawSmile(Graphics g) {
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
}
public void drawFrown(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
}
}
}

major issue: you're adding a lot of actionlisters in your code...
...whenever repaint() is called, it calls internally the paint(Graphics g) method and inside your paint-method you're adding a actionListener (again and again and again, whenever you redraw)
try to add the actionListener within your constructor:
public JSmileFacePanel2() {
setLayout(new FlowLayout());
setTitle("JSmileFace-V2: Jose M. Tobar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(frown);
add(smile);
setSize(800, 800);
setVisible(true);
//here:
smile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("SMILE BUTTON CLICKED");
drawSmile(g);
}
});
frown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("FROWN BUTTON CLICKED");
drawFrown(g);
}
});
}

a minor flaw:
don't call drawFrown(g) or drawSmile(g) inside your actionListener (you can't anymore, you don't know graphics anymore) instead call repaint().
But before you repaint() you tell your graphic what will be drawn (i'm using an int for this, better use Enums but it will work for right now
i'm just doing the smile-part, the frown part is the same
private int style = 0;
public JSmileFacePanel2() {
//other code as above
...
smile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("SMILE BUTTON CLICKED");
style = 1; //1=smile, 2=frown
repaint(); //this calls internally the paint(g) method
}
});
}
and adjust your paint method:
public void paint(Graphics g) {
super.paint(g);
//by default should show smiling
g.setColor(Color.YELLOW);
g.fillOval(200, 200, 500, 500);
g.setColor(Color.BLUE);
g.fillOval(300, 360, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(600, 360, 50, 50);
g.drawArc(400, 400, 100, 40, 180, 185);
if(style == 1){
drawSmile(g);
}
if(style == 2){
drawFrown(g);
}
}

Related

Why is repaint() not calling paintcomponent in a swing timer?

When I use repaint() in the swing timer method startTimer, it just simply doesnt call paintcomponent(). Everything works inside of the timer method except repaint(). Im pretty new at java so if anyone could help me with this problem or point out any other errors I would greatly appreciate it. thank you
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Board extends JPanel {
Timer timer;
int count;
int clockNumber;
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(30, 30, 640, 640);//makes a black square
for(int i=30;i<=510; i+=160)//adds white columns
{
for(int j=30; j<=510; j+=160)
{
g.clearRect(i, j, 80, 80);
}
}
for(int i=110; i<=590; i+=160)//adds black columns
{
for(int j=110; j<=590; j+=160)
{
g.clearRect(i, j, 80, 80);
}
}
g.setFont(new Font("Monospace", Font.BOLD, 30));
g.setColor(Color.WHITE);
g.drawString("a", 85, 660);
g.drawString("c", 245, 660);
g.drawString("e", 405, 660);
g.drawString("g", 565, 660);
g.drawString("7", 35, 140);
g.drawString("5", 35, 300);
g.drawString("3", 35, 460);
g.drawString("1", 35, 620);
g.setColor(Color.BLACK);
g.drawString("b", 165, 660);
g.drawString("d", 325, 660);
g.drawString("f", 485, 660);
g.drawString("h", 645, 660);
g.drawString("8", 35, 60);
g.drawString("6", 35, 220);
g.drawString("4", 35, 380);
g.drawString("2", 35, 540);
System.out.println(clockNumber);
g.drawString(String.valueOf(clockNumber), 300, 300);
}
public void showX(Graphics g)
{
g.setFont(new Font("wrongFont", Font.BOLD, 200));
g.setColor(Color.RED);
g.drawString("X", 35, 540);
}
public void boardImage()
{
JFrame frame=new JFrame();
frame.setSize(600, 600);
frame.getContentPane().add(new Board());
frame.setLocationRelativeTo(null);
frame.setBackground(Color.LIGHT_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
CoordinateGame game=new CoordinateGame();
frame.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int x=e.getX();
int y=e.getY();
}
});
}
public int clockNumber()
{
return clockNumber;
}
public void startTimer(int seconds)
{
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
clockNumber=count;
if(count==0)
{
timer.stop();
}
else
{
System.out.println(count);
clockNumber--;
count--;
repaint();
}
}
};
timer=new Timer(1000, action);
timer.setInitialDelay(0);
timer.start();
count=seconds;
}
}
Since you didn't provide a runnable example I should suppose that you're doing something like
Board board=new Board();
board.boardImage();
board.startTimer();
The problem is in boardImage() that creates a new instance of Board and adds that instance to the JFrame, not the original board, on which you start the timer.
So replace
frame.getContentPane().add(new Board());
with
frame.getContentPane().add(this);
Consider also removing the explicit setSize on the frame and add override getPreferredSize on your custom component, then use frame.pack() to automatically adjust the frame size to the board.

whats wrong with my code that I have to press a button twice before making the triangle appear

learning the basics here.
I just want to know how would I have to arrange the code such that the paint method runs at the beginning of the program and not when you click a button.
or am I forgetting something important?
i know that paint method is automatically called when you set the visibility to true but other than that, im not sure.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Triangle extends JFrame implements ActionListener
{
private JButton b1 = new JButton ("Blue");
private JButton b2 = new JButton ("Red");
private boolean color = true;
public Triangle()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("Triangle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setLocation(300,300);
setVisible(true);
}
public void paint(Graphics g)
{
if(color == true)
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
getContentPane().setBackground(Color.red);
}
else
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
getContentPane().setBackground(Color.BLUE);
}
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == b2)
{
color = true;
repaint();
}
else
{
color = false;
repaint();
}
}
}
my paint method was originally like this
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
if(color == true)
{
getContentPane().setBackground(Color.red);
}
else
{
getContentPane().setBackground(Color.BLUE);
}
}

EasterEgg in Java

package easteregg;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
}
}
public class EasterEgg {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
my problem is i dont know how to put lines inside the oval
i want to make a easterEgg please help me
i have modified the paint method to draw one pattern please check the below code
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
g.drawLine(120, 40, 140, 60);
g.drawLine(140, 60, 150, 40);
g.drawLine(150, 40, 170, 60);
g.drawLine(170, 60, 190,40);
}
and the output is
this is just a hint to draw lines but better to create one method which will draw that pattern form one end to other end of the oval

Panel wont show in the window

I am trying to start the program with jcheckbox hat selected and rectangle visible then the Rectangle disappears when the checkbox is unselected and repainted as checkbox is selected again. When I run the program and check the box another check box appears or left of the frame.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Head extends JPanel {
JCheckBox hat;
public Head() {
hat = new JCheckBox("Hat");
hat.setSelected(true);
hat.addItemListener(new CheckSelection());
add(hat);
}
class CheckSelection implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}
public void paintComponent(Graphics g) {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
}
}
public static void main(String[] args) {
Head head = new Head();
JFrame f = new JFrame();
f.add(head);
f.setSize(400, 400);
//f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You've broken the paint chain by not calling the paintComponent's super method
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
} else {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
}
}
The Graphics context is a shared resource between components, one of the jobs of paintComponent is to prepare the Graphics for painting, typically by filling it with the background color of the component. So failing to call super.paintComponent means that what ever was previously painted to the Graphics context will still be there
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing

Removing a circle from JFrame

I am creating a Towers of Hanoi game and have sucedded in printing the shapes with a time delay (that part of the code has been removed while I try to get all the ovals where they are supposed to be) I am wondering how I would go about removing the circle. As you can see I have tried the clearRect(); method but that doesn't work. Is there a way I can set if the method is visible on JPanel since each circle has its own method? I think that would be the easiest way but if anyone has a better idea go for it! Thanks for any help and I have attached my code
package towersofhanoi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.Object;
/*g.fillOval(60 = horizontal distance , 540= vertical distance, 400 = width, 60 = height) */
public class TowersOfHanoi extends JPanel {
private int clock = 0;
private Color circles = new Color(176, 56, 251);
public static void main(String[] args) {
// Print the shapes and frame
TowersOfHanoi drawRectangle = new TowersOfHanoi();
JFrame frame = new JFrame("Towers of Hanoi");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(drawRectangle);
frame.setSize(1250, 800);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
drawRectangle.nextFrame();
drawRectangle.repaint();
}
});
timer.setRepeats(true);
timer.start();
}
public void nextFrame() {
clock++;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
frame1(g);
frame2(g);
frame3(g);
frame4(g);
frame5(g);
frame6(g);
frame7(g);
frame8(g);
frame9(g);
}
private Color frame1(Graphics g) {
Color pegs = new Color(251, 129, 56);
g.setColor(pegs);
// peg 1
g.fillRect(250, 300, 25, 450);
// peg 2
g.fillRect(600, 300, 25, 450);
// peg 3
g.fillRect(950, 300, 25, 450);
// bottom
g.fillRect(200, 700, 825, 50);
// create a color for circles
// cirle 7 (Labeled from bottom to top)
g.setColor(circles);
g.fillOval(60, 640, 400, 60);
g.setColor(Color.BLACK);
g.drawOval(60, 640, 400, 60);
return circles;
}
private void frame2(Graphics g) {
// circle 6
g.setColor(circles);
g.fillOval(85, 580, 350, 60);
g.setColor(Color.BLACK);
g.drawOval(85, 580, 350, 60);
}
private void frame3(Graphics g) {
// circle 5
g.setColor(circles);
g.fillOval(110, 520, 300, 60);
g.setColor(Color.BLACK);
g.drawOval(110, 520, 300, 60);
}
private void frame4(Graphics g) {
// circle 4
g.setColor(circles);
g.fillOval(135, 465, 250, 55);
g.setColor(Color.BLACK);
g.drawOval(135, 465, 250, 55);
}
private void frame5(Graphics g) {
// circle 3
g.setColor(circles);
g.fillOval(160, 420, 200, 45);
g.setColor(Color.BLACK);
g.drawOval(160, 420, 200, 45);
}
private void frame6(Graphics g) {
// circle 2
g.setColor(circles);
g.fillOval(185, 380, 150, 40);
g.setColor(Color.BLACK);
g.drawOval(185, 380, 150, 40);
}
private void frame7(Graphics g) {
// circle 1
g.setColor(circles);
g.fillOval(210, 345, 100, 35);
g.setColor(Color.BLACK);
g.drawOval(210, 345, 100, 35);
}
public void frame8(Graphics g) {
g.clearRect(210, 345, 100, 35);
g.setColor(circles);
g.fillOval(560, 665, 100, 35);
g.setColor(Color.BLACK);
g.drawOval(560, 665, 100, 35);
}
public void frame9(Graphics g) {
g.clearRect(185, 380, 150, 40);
g.setColor(circles);
g.fillOval(890, 660, 150, 40);
g.setColor(Color.BLACK);
g.drawOval(890, 660, 150, 40);
}
}
I am wondering how I would go about removing the circle.
The super.paintComponent(...) will clear the painting on the panel so that is all the is needed.
As you can see I have tried the clearRect();
It is not needed (see my comment above), but then you invoke the fillOval(...) and drawOval(...) methods again so the painting is redone. So the code is doing exactly as you asked it to do.
Is there a way I can set if the method is visible on JPanel
You need a Boolean indicator telling the paint method what to do. Something like:
if (paintOval1)
frame1(g);
if (paintOval2)
frame2(g);
Of course that is approach is very brute force and not a very good approach if for say you have 100 circles to paint. The code become too big.
So, instead you should create a custom class that contains four properties (x, y, width, height, isPainted). Then you create an instance of this class for each circle and add the class to an ArrayList. Something like:
ArrayList<CustomClass> circles = new ArrayList<CustomClass>();
circles.add( new CustomClass(60, 640, 400, 60, true) );
circles.add( new CustomClass(85, 580, 350, 60, true) );
Then in the paintCompnent() method your code becomes simpler:
for (CustomClass circle: circles.get)
{
if (circle.isPainted())
{
g.setColor(...);
g.fillOval(circle.getX(), circle,getY(), circle.getWidth(), circle.getHeight());
...
}
}
Finally you would need a method to change the state of painting a circle. Something like:
pubic void setCirclePainted(int circle, Boolean isPainted)
{
CustomClass circle = circles.get(circle);
circle.setPainted( isPainted );
}
So the key for you is to create your "CustomClass" and give it a proper name. Then you need to implement all the getter/setter methods of the class so you can access the properties of the class.
I actually found that the easiest way for me to understand and do was to use else and else if statements to display the shape based on time. Thanks for the other suggestions though!
public void paintComponent(Graphics g) {
super.paintComponent(g);
frame1(g);
frame2(g);
frame3(g);
frame4(g);
if (clock<= 5) {
frame5(g);
}
else if(clock >= 6) {
frame9(g);
}
frame6(g);
frame7(g);
frame8(g);
}

Categories

Resources