The paint function is not called in Java Swing - java

My purpose - to create a board with lines, using Java Swing.
After I made a board and added color to it, I tried to produce lines. To do this, I inherit from JPanel and added paintComponent method. But when I run the application, the method is not called.
I added default constructor with super();
I also added to the called constructor super();
I still cannot make the paint method or the paintComponent method to get run;
I tried all of the following posts:
Java Swing paint() not working
Insert Button in JPanel
public class Main {
public static void main(String[] args) {
Board board = new Board(295, 295, "Go board");
}
}
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel{
private int width;
private int height;
private String title;
private JFrame JFrame;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public JFrame getJFrame() {
return JFrame;
}
public void setJFrame(JFrame JFrame) {
this.JFrame = JFrame;
}
public Board(int width, int height, String title){
super();
this.width = width;
this.height = height;
this.title = title;
this.initBoard();
}
public Board(){
super();
}
public void initBoard(){
JFrame f = new JFrame(this.getTitle());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.getHSBColor(25, 75, 47));
f.setSize(this.getWidth(), this.getHeight());
f.setLocation(550, 25);
f.setVisible(true);
this.setJFrame(f);
}
public void paint(Graphics g) {
g.drawLine(10, 10, 250, 10);
System.out.println("Test paint");
}
public void paintComponent(Graphics g) {
g.drawLine(10, 10, 250, 10);
System.out.println("Test paintComponent");
}
}

You never have called your initBoard which contains JFrame (which is bad design). Also you must add your panel to that frame with jframe.add(this);
It would be better to have your frame alone, and add your component to it like this
public static void main(){
JFrame f=new JFrame();
///set all the dimensions and other stuff of the frame
f.setLayout(new BorderLayout);
f.add(new Board(295, 295, "Go board"),BorderLayour.CENTER);
f.setVisible();
}

Related

Drawing ovals on JButton

I'm writing minesweeper and I want to put ovals on mine cells after a mine is clicked. But for now, I just want to put ovals on all cells just to check. I have written the code below. But when I run the program there is no ovals on buttons. I can not see the reason. I would be grateful if I get some suggestions.
public class Cell extends JButton{
...
public void painComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.drawOval(0,0,25,25);
}
public void draw() {
repaint();
}
...
}
public class Grid extends JPanel implements MouseListener{
...
public Grid(){
this.setLayout(new GridLayout(20,20));
cells = new Cell[20][20];
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
cells[i][j] = new Cell(i,j);
cells[i][j].addMouseListener(this);
cells[i][j].draw();
this.add(cells[i][j]);
}
}
plantMines();
setVisible(true);
}
...
}
but when I run the program there is no ovals on buttons.
public void painComponent(Graphics g) {
You made a typo. You spelt "paint" wrong.
When you override a method you should use:
#Override
protected void paintComponent(Graphics g)
This way if you make a typo the compiler will tell you that you are not overriding a method of the class.
Also, don't attempt to draw the oval by using custom painting. Instead you should be adding an Icon to the button. Then you can just change the Icon as required.
You can easily create an simple Icon to use. Here is an example of creating a square Icon:
import java.awt.*;
import javax.swing.*;
public class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JPanel panel = new JPanel( new GridLayout(2, 2) );
for (int i = 0; i < 4; i++)
{
Icon icon = new ColorIcon(Color.RED, 50, 50);
JLabel label = new JLabel( icon );
label.setText("" + i);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
panel.add(label);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.setSize(200, 200);
f.setLocationRelativeTo( null );
f.setVisible(true);
}
}

Java drawing moving rectangles in loop

I need to draw in AWT/Swing rectangles that are moving from frame to frame.
I have a Playground class
public Playground(int sizeX, int sizeY)
{
frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setSize(sizeX, sizeY);
panel.setDoubleBuffered(true);
panel.setVisible(true);
frame.add(panel);
frame.pack();
frame.setSize(sizeX, sizeY);
}
public void refresh()
{
panel.repaint();
}
public Graphics getGraphics()
{
return panel.getGraphics();
}
This is the class in which objects should be drawn:
public class Star {
private static int size = 10;
private int posX;
private int posY;
public Star(int posX, int posY)
{
this.posX = posX;
this.posY = posY;
}
public void paint(Graphics g)
{
g.fillRect(posX, posY, size, size);
}
public int getPosX() {
return posX;
}
public int getPosY() {
return posY;
}
}
This is the main method:
public static void main(String[] args) {
Playground playground = new Playground(400, 400);
Star star = new Star(100, 100);
Star star2 = new Star(125, 125);
while(1 == 1)
{
playground.refresh();
star.paint(playground.getGraphics());
star2.paint(playground.getGraphics());
}
}
The objects are drawn but are flickering, how can I stop it from flickering?
Edit: I solved the flickering for one element, by changing the refresh method to:
public void refresh()
{
panel.getGraphics().clearRect(0,0, panel.getWidth(), panel.getHeight());
}
Unfortunately only one Element is not flickering all others are still flickering.
The following is a one-file mcve that demonstrates moving (rotating for simplicity) a rectangle by custom painting.
One-file meaning that you can copy-paste the entire code into one file (AnimateRectangle.java) and execute it.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class AnimateRectangle {
private JFrame frame;
public AnimateRectangle(Model model){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new MyJPanel(model);
panel.setDoubleBuffered(true);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
void refresh() {
frame.repaint();
}
public static void main(String[] args) throws InterruptedException {
Controller controller = new Controller(400, 400);
while (true) {
Thread.sleep(1000);
SwingUtilities.invokeLater(()->controller.animate());
}
}
}
//"wires" gui and model
class Controller{
private Model model;
private AnimateRectangle view;
Controller(int sizeX, int sizeY){
model = new Model(sizeX, sizeY);
view = new AnimateRectangle(model);
}
void animate() {
int newAngle = (model.getAngle() < 360 ) ? model.getAngle()+1 : 0 ;
model.setAngle(newAngle);
view.refresh();
}
}
//represents the inforamtion the GUI needs
class Model{
int sizeX, sizeY, angle = 0;
public Model(int sizeX, int sizeY) {
this.sizeX = sizeX;
this.sizeY = sizeY;
}
int getSizeX() { return sizeX; }
int getSizeY() {return sizeY;}
int getAngle() {return angle;}
//degrees
void setAngle(int angle) { this.angle = angle; }
}
//a JPanel with custom paint component
class MyJPanel extends JPanel {
private Model model;
public MyJPanel(Model model) {
this.model = model;
setPreferredSize(new Dimension(model.getSizeX(), model.getSizeY()));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.RED);
int sizeX = model.getSizeX(), sizeY = model.getSizeY();
g2d.rotate(Math.toRadians(model.getAngle()), sizeX /2, sizeY/2);
g2d.fillRect(sizeX/4, sizeY/4, sizeX/2, sizeY/2);
}
}
A better option (see camickr comment) is to animate using swing Timer. To do so, remove animate() method, and replace it with :
void animateWithTimer(){
new Timer(1000,new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int newAngle = (model.getAngle() < 360 ) ? model.getAngle()+1 : 0 ;
model.setAngle(newAngle);
view.refresh();
}
}).start();
}
and change main to use it :
public static void main(String[] args) throws InterruptedException {
Controller controller = new Controller(400, 400);
controller.animateWithTimer();
}

Calling a paint or render method from another class - Java

I did allot of research for this problem but couldn't find anything that would help me out.
When I run this app, the only thing I will see is an empty JFrame, the expected output is a JFrame with a black rectangle. This is my code, there are two classes.
public class Test {
public static void main(String[] args) {
Test test = new Test("Frame");
test.setFrame();
Window win = new Window(ObjectId.Window, 10, 10, 10, 10);
win.repaint();
}
private String title;
public Test(String title) {
this.title = title;
}
private void setFrame() {
JFrame frame = new JFrame(title);
frame.setSize(800, 600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
And my second class:
public class Window extends Component {
private ObjectId id;
private int x, y;
private int width, height;
public Window(ObjectId id, int x, int y, int width, int height) {
this.id = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.fillRect(x, y, width, height);
}
Thanks
You didn't add win to the frame ('s contentPane). And you should extend JPanel in the Window-class, not Component.
So if you move Window win = new Window(ObjectId.Window, 10, 10, 10, 10) into the setFrame() method (you don't need the win.repaint()) and call frame.getContentPane().add(win) (or something similar) it will work.

Drawing from one panel class to another panel class.

I have two classes. The first class is called Fishy1, and the second class is called Fishy2. This is the code for my first class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Fishy1 extends JPanel {
Fishy1 fishy1 = new Fishy1();
/* Graphics goes here */
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(50, 50, 50, 50);
}
}
As you can see, the code basically draws an oval in fishy1. And here is the code for my second class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Fishy2 extends JPanel {
Fishy2 fishy2 = new Fishy2();
}
As you can see, in the second class, there is no paintComponet method to draw to fishy2. So, my question is, is there a way to draw to the second class using the paintComponent method in the first class? If there's no way to do it, please let me know. Thank you.
To achieve graphics replication between 2 swing classes at same time
public class Fishs extends JPanel {
//Static list, all fishes panel will display the same objects at same positions
private static List<OvalObj> lstOvalObjects;
public Fishs() {
//if the list is null just initialize it.
lstOvalObjects = lstOvalObjects == null? new ArrayList():lstOvalObjects;
}
/* Graphics goes here */
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
lstOvalObjects.forEach(ovalObject -> g.drawOval(ovalObject.getX(), ovalObject.getY(), ovalObject.getWidth(), ovalObject.getHeight()));
}
public static List<OvalObj> getLstOvalObjects() {
return lstOvalObjects;
}
public static void setLstOvalObjects(List<OvalObj> lstOvalObjects) {
Fishs.lstOvalObjects = lstOvalObjects;
}
}
The oval object:
public class OvalObj{
private int x,y,width,height;
public OvalObj(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Implementation:
//First Frame
JFrame frame1 = new JFrame();
frame1.setLayout(new BorderLayout());
//First Fish Panel that will go to frame1
Fishs fish1 = new Fishs();
fish1.setVisible(true);
frame1.add(fish1, BorderLayout.CENTER);
frame1.pack();
frame1.setVisible(true);
//Second Frame
JFrame frame2 = new JFrame();
//Second Fish Panel that will go to frame2
Fishs fish2 = new Fishs();
fish2.setVisible(true);
frame2.setLayout(new BorderLayout());
frame2.add(fish2, BorderLayout.CENTER);
frame2.pack();
frame2.setVisible(true);
/// you can add many objects to draw as you like in a static way anywhere in your code, they will render in every fish panel at same time
Fishs.getLstOvalObjects().add(new OvalObj(0, 0, 50, 50));
Fishs.getLstOvalObjects().add(new OvalObj(20, 20, 50, 50));

How to get JPanel to fill parent JFrame

I am trying to draw a circle in the center of a window, and I can't seem to get it right, should be really easy! My understanding is that if you set a JPanel as the content pane of a JFrame, the default layout is a flowLayout and that drawing should start from the top left of the screen as 0,0. To try and figure out what's going on I drew a blue background filling the JPanel, but it seems to have a margin like so:
When the window gets smaller than the blue rectangle, the drawing starts to get clipped from the opposite side:
What's going on! Here is my code:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public static void main(String args[])
{
Test test = new Test();
test.Start();
}
public void Start()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
CirclePanel circlePanel = new CirclePanel();
this.setContentPane(circlePanel);
this.setVisible(true);
}
public class CirclePanel extends JPanel
{
private int radius = 200;
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
int diameter = radius * 2;
g.fillOval(getX(), getY(), diameter, diameter);
}
public int getX()
{
return (this.getWidth()/2) - radius;
}
public int getY()
{
return (this.getHeight()/2) - radius;
}
}
}
One big issue, you're unknowingly overriding two critical methods used by the layout managers to position components, the getX() and getY() methods, and thereby you're messing with the JPanel's placement.
So first and foremost, rename these methods so you don't accidentally move the JPanel.
Also, don't forget to call the super's paintComponent method, and avoid calling setSize(). Instead override getPreferredSize on your JPanel, and pack your JFrame.
e.g.,
public int getMyX() {
return myX;
}
public int getMyY() {
return myY;
}
For example
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public static void main(String args[]) {
//!!
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test test = new Test();
test.Start();
}
});
}
public void Start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setSize(500, 500);
CirclePanel circlePanel = new CirclePanel();
setContentPane(circlePanel);
pack();
setVisible(true);
}
public class CirclePanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private int radius = 200;
public void paintComponent(Graphics g) {
super.paintComponent(g); //!!
g.setColor(Color.blue);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
int diameter = radius * 2;
g.fillOval(getMyX(), getMyY(), diameter, diameter);
}
//!!
public int getMyX() {
return (this.getWidth() / 2) - radius;
}
//!!
public int getMyY() {
return (this.getHeight() / 2) - radius;
}
//!!
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
}
The problem is that you're overriding getX() and getY(). Rename those to something else, and your code will work as expected. Also, it's a good idea to turn on compiler warnings for missing #Override annotations, and heed those warnings. (That would have notified the methods override superclass methods).
Try using this.add(circlePanel) instead if this.setContentPane(circlePanel) and set the size of the JPanel to be the same size as the JFrame

Categories

Resources