I am trying to figure out how to repaint()
a class that extends the JComponent
. I want to have each class extend JComponent
and then supply its own paintComponent()
. ( super.paintComponent()
. This way I can make a circle class or whatever class that I want to make and then I can repaint()
it when ever I want to as long as it is added to the JPanel
that goes in the JFrame
. Below are all of the code that are needed to run.
Class 1:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class DDHGenericFrame extends JFrame {
private static final long serialVersionUID = 1L;
/* User defined class that is below */
DDHGenericPanel d = new DDHGenericPanel();
public DDHGenericFrame() {
initUI();
}
public final void initUI() {
add(d);
setSize(650, 350);
setTitle("Lines");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
DDHGenericFrame ex = new DDHGenericFrame();
ex.setVisible(true);
}
}
Class 2:
import java.awt.Graphics;
import javax.swing.JPanel;
class DDHGenericPanel extends JPanel {
private static final long serialVersionUID = 1L;
DDHGenericPanel() {
System.out.println("DDH Generic JPanel");
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//paint code goes here of whatever
}
}
Class 3:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class DDHCircleOne extends JComponent {
private static final long serialVersionUID = 1L;
int counter = 0;
public DDHCircleOne() {
counter++;
System.out.println("DDHCircle Constructor");
}
public void paintComponent(Graphics g2) {
super.paintComponent(g2);
Graphics2D c2D = (Graphics2D) g2;
c2D.setColor(Color.yellow);
c2D.fillRect(0, 0, getSize().width, getSize().height);
if (counter % 2 == 2) {
System.out.println("RED");
counter++;
c2D.setColor(Color.red);
c2D.fillRect(0, 0, getSize().width, getSize().height);
} else {
System.out.println("BLUE");
counter++;
c2D.setColor(Color.blue);
c2D.fillRect(0, 0, getSize().width, getSize().height);
}
}
}
The above three classes are what I have in my package. I want DDHCircleOne
to be able to repaint it self whenever an instance of it is created and calls the method repaint()
. This way I can just make a class that extends JComponent
and then override the paintComponent()
and then call repaint()
.
If I had a button that said repaint in red and another button that says repaint in blue the circle or whatever I had in the frame will repaint itself when the variableName.repaint()
is called.
doughauf@outlook.com
public void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
Never invoke repaint() in a painting method. This will cause an infinite loop.
There is no need for you generic panel. You can add a component to any panel.
If you want the color of the circle to change, then you need to change a property of your component. For example you can just use the setForeground(...) method to change the color:
//c2D.setColor(Color.red);
c2D.setColor( getForeground() );
So when you click on your button you just invoke circle.setForeground(Color.RED);
The setForeground() method of JComponent will invoke the repaint() method on the component for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.