Related
I took this piece of code from my software. I have a panel called pnlUserInfo. Inside of it, I have another panel called pnlStatistikat, which has yet another panel inside it (just for graphics) called pnGrafika. In the first lines of code, I draw something and then I have no idea how to wrap it inside pnGrafik. (How to call this class Grafika inside of pnGrafik, so the object can be shown)
class Grafika extends JPanel{
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillArc(0, 0, 100, 100, 0, (int)pnUserInfo.tOL);
g.setColor(Color.YELLOW);
g.fillArc(0, 0, 100, 100, (int)pnUserInfo.tOL, (int)pnUserInfo.tOK);
g.setColor(Color.GREEN);
g.fillArc(0, 0, 100, 100, (int)pnUserInfo.tOK+(int)pnUserInfo.tOL, (int)pnUserInfo.tOS);
g.setColor(Color.BLUE);
g.fillArc(0, 0, 100, 100, (int)pnUserInfo.tOK+(int)pnUserInfo.tOL+(int)pnUserInfo.tOS, (int)pnUserInfo.tOP);
}
}
public class pnUserInfo extends JPanel {
public pnUserInfo() {
final JPanel pnlStatistikat = new JPanel();
pnlStatistikat.setBounds(0, 0, 530, 628);
pnlProfiliKryesor.add(pnlStatistikat);
pnlStatistikat.setBackground(myColor);
pnlStatistikat.setLayout(null);
pnlStatistikat.setVisible(false);
JPanel pnGrafik = new JPanel();
pnGrafik.setBounds(250, 30, 110, 110);
pnGrafik.add(new Grafika());
pnlStatistikat.add(pnGrafik);
pnGrafik.setBackground(myColor);
pnGrafik.setLayout(null);
pnGrafik.setVisible(true);
}}
See comments :
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Grafika extends JPanel{
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillArc(0, 0, 100, 100, 0, 30);
g.setColor(Color.YELLOW);
g.fillArc(0, 0, 100, 100, 30, 50);
g.setColor(Color.GREEN);
g.fillArc(0, 0, 100, 100, 50, 90);
g.setColor(Color.BLUE);
g.fillArc(0, 0, 100, 100, 90,120);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new pnUserInfo());
frame.setVisible(true);
}
}
class pnUserInfo extends JPanel {
public pnUserInfo() {
setLayout(null);//missing
JPanel pnlStatistikat = new JPanel();
pnlStatistikat.setLayout(null);
pnlStatistikat.setBounds(0, 0, 300, 250);
pnlStatistikat.setBackground(Color.CYAN);
add(pnlStatistikat); //missing
//remove pnlStatistikat.setVisible(false);
JPanel pnGrafik = new JPanel();
pnGrafik.setLayout(null);
pnGrafik.setBounds(50, 50, 200, 200);
pnGrafik.setBackground(Color.YELLOW);
Grafika graf = new Grafika();
graf.setBounds(30, 30, 110, 110);//missing
pnGrafik.add(graf);
pnlStatistikat.add(pnGrafik);
pnGrafik.setVisible(true);
}}
It's simple
pnGraphik.add(new Graphika());
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);
}
}
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
I am trying to make a triangle out of a sloop can a made in the graphics panel but I cant seem to get the code to work right. this is what I have so far.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class PyramidSoupCans {
/**
* #param args
*/
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(800, 800);
panel.setBackground(new Color(219, 204, 186));
Graphics g = panel.getGraphics();
for(int i=1;i<=10;i++){
for(int j=1;j<=10-i; j++){
System.out.print(" ");
}
for (int k=1;k<=2 * i-1;k++){
draw_can( g, i, k);
}
System.out.println();
}}
public static void draw_can(Graphics g, int x, int y){
int sizeX = 225;
int sizeY = 340;
int cornerX = 37;
int cornerY = 80;
g.setColor(new Color(138, 138, 138));
g.fillRoundRect(cornerX, 246, sizeX, 180, 150, 45);
g.setColor(new Color(243, 243, 243));
g.fillRoundRect(cornerX, cornerY, sizeX, sizeY, 150, 45);
g.setColor(new Color(162,22,5));
g.fillRoundRect(cornerX, 70, sizeX, 190, 150, 45);
g.setColor(new Color(138, 138, 138));
g.fillOval(cornerX, 65, sizeX, 45);
g.setColor(Color.white);
g.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,45));
g.drawString("Campbell's",45,150);
g.setFont(new Font("SanSerif",Font.BOLD,20));
g.drawString("CONDENSED",85,200);
g.setColor(new Color(162,22,5));
g.setFont(new Font("SanSerif",Font.BOLD,40));
g.drawString("TOMATO",60,360);
g.setFont(new Font("Serif",Font.BOLD,40));
g.setColor(new Color(157, 131, 82));
g.fillOval(113, 223, 70, 70);
g.drawString("SOUP",95,410);
}}
I am trying to make a triangle out of a sloop can a made in the graphics panel but I cant seem to get the code to work right. this is what I have so far.
That's all. Thanks
I wasn't able to make a triangle, but I did get the soup can to draw.
I made lots of changes to your code. I did keep your class name, PyramidSoupCans.
The important changes include.
I decided to use Swing components, since you named a DrawingPanel that you didn't include as part of your code.
You always start a Swing application with a call to the SwingUtilities invokeLater method. This ensures that the Swing components are created and updated on the Event Dispatch thread.
I created a drawing panel from a JPanel, and put the JPanel in a JFrame with some basic decorations.
I put your drawing code in the place where Swing provides a Graphics object to draw on. You should always override the JPanel paintComponent method when you want to draw on a Swing GUI.
Here's the code:
package com.ggl.testing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PyramidSoupCans implements Runnable {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new PyramidSoupCans());
}
#Override
public void run() {
frame = new JFrame("Pyramid Soup Cans");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel(300, 500);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = -4147433638611078320L;
public DrawingPanel(int width, int height) {
this.setPreferredSize(new Dimension(width, height));
this.setBackground(new Color(219, 204, 186));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int sizeX = 225;
int sizeY = 340;
int cornerX = 37;
int cornerY = 80;
g.setColor(new Color(138, 138, 138));
g.fillRoundRect(cornerX, 246, sizeX, 180, 150, 45);
g.setColor(new Color(243, 243, 243));
g.fillRoundRect(cornerX, cornerY, sizeX, sizeY, 150, 45);
g.setColor(new Color(162, 22, 5));
g.fillRoundRect(cornerX, 70, sizeX, 190, 150, 45);
g.setColor(new Color(138, 138, 138));
g.fillOval(cornerX, 65, sizeX, 45);
g.setColor(Color.white);
g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 45));
g.drawString("Campbell's", 45, 150);
g.setFont(new Font("SanSerif", Font.BOLD, 20));
g.drawString("CONDENSED", 85, 200);
g.setColor(new Color(162, 22, 5));
g.setFont(new Font("SanSerif", Font.BOLD, 40));
g.drawString("TOMATO", 60, 360);
g.setFont(new Font("Serif", Font.BOLD, 40));
g.setColor(new Color(157, 131, 82));
g.fillOval(113, 223, 70, 70);
g.drawString("SOUP", 95, 410);
}
}
}
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);
}