I'm trying to set lines on JPanel on JFrame as standard - java

I've got some lines (tablature for music writing) and I'm trying to set them so that the lines show up as standard with the JPanel. I've created this class and tried to create a new instance at runtime, but drawing blanks. I've tried a bunch of things and wanted to create my own method but I might just end up using paint(), if that's easier? What do you think?
public class Lines extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g.drawLine(20, 50, 1200, 50);
g.drawLine(20, 60, 1200, 60);
g.drawLine(20, 70, 1200, 70);
g.drawLine(20, 80, 1200, 80);
g.drawLine(20, 90, 1200, 90);
g.drawLine(20, 140, 1200, 140);
g.drawLine(20, 150, 1200, 150);
g.drawLine(20, 160, 1200, 160);
g.drawLine(20, 170, 1200, 170);
g.drawLine(20, 180, 1200, 180);
g.drawLine(20, 230, 1200, 230);
g.drawLine(20, 240, 1200, 240);
g.drawLine(20, 250, 1200, 250);
g.drawLine(20, 260, 1200, 260);
g.drawLine(20, 270, 1200, 270);
}
}
and I'm trying to create a new instance here;
class panelControls extends JPanel {
private GUIPanel panel;
private Lines lines;
public panelControls(GUIPanel panel) {
this.panel = panel;
this.panel.add(lines);
}
then this makes the new panel;
public class GUIPanel extends JPanel {
BufferedImage image;
public GUIPanel(int width, int height) {
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = this.image.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
this.setFocusable(true);
}
#Override
public Dimension getSize() {
return (new Dimension(this.image.getWidth(), this.image.getHeight()));
}
#Override
public void paintComponent(Graphics graphics) {
Graphics g = graphics.create();
g.drawImage(this.image, 0, 0, null);
}
}
ok, here's tabGUI;
private GUIPanel panel;
private panelControls controls;
public tabGUI(String title, int width, int height) {
super(String.format("Title", title));
this.panel = new GUIPanel(width, height);
this.controls = new panelControls(this.panel);
this.getContentPane().add(this.controls, BorderLayout.CENTER);
}
public GUIPanel getGUIPanel() {
return (this.panel);
}
}
and this is the main;
public class mainGUI implements Runnable {
private tabGUI tabGui;
private String title;
public mainGUI(String title) {
this.title = title;
this.tabGui = new tabGUI(this.title, 1110, 700);
}
public void setTitle(String title){
this.tabGui.setTitle(this.title);
}
public void run() {
this.tabGui = new tabGUI(this.title, 1110, 700);
this.tabGui.setTitle(this.title);
this.tabGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.tabGui.setPreferredSize(new Dimension(1240, 800));
this.tabGui.pack();
this.tabGui.setVisible(true);
}
public static void main(String[] args) throws Exception {
// JFrame.setDefaultLookAndFeelDecorated(true);
String title = "piece1";
//put the tab source file to run here
mainGUI tabGui = new mainGUI(title);
javax.swing.SwingUtilities.invokeLater(tabGui);
}
}
getting this;
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at readtabcode.panelControls.<init>(panelControls.java:15)
at readtabcode.tabGUI.<init>(tabGUI.java:17)
at readtabcode.mainGUI.<init>(mainGUI.java:14)
at readtabcode.mainGUI.main(mainGUI.java:37)
Java Result: 1

Related

Shapes are not drawn in real time

So, if I try to move the shape, it actually moves, but there is no motion animation. In order for the moved shape to be drawn, the application window must be minimized or maximized.
Here is the PentaminoShape class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class PentominoShape extends JFrame implements MouseListener, MouseMotionListener {
JPanel shapePane;
Container contentPane;
private Polygon currPolygon;
private int x, y;
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
JFrame frame;
public PentominoShape(JFrame frame){
this.frame = frame;
initShape();
}
private void initShape() {
Polygon fig1 = new Polygon(new int[]{10, 50, 50, 10}, new int[]{10, 10, 200, 200}, 4);
Polygon fig2 = new Polygon(new int[]{130, 210, 210, 170, 170, 130, 130, 90, 90, 130}, new int[]{80, 80, 120, 120, 200, 200, 160, 160, 120, 120}, 10);
Polygon fig3 = new Polygon(new int[]{290, 330, 330, 250, 250, 290}, new int[]{50, 50, 200, 200, 160, 160}, 6);
Polygon fig4 = new Polygon(new int[]{10, 90, 90, 50, 50, 10}, new int[]{280, 280, 400, 400, 360, 360}, 6);
Polygon fig5 = new Polygon(new int[]{170, 210, 210, 170, 170, 130, 130, 170}, new int[]{240, 240, 360, 360, 400, 400, 320, 320}, 8);
Polygon fig6 = new Polygon(new int[]{250, 370, 370, 330, 330, 290, 290, 250}, new int[]{280, 280, 320, 320, 400, 400, 320, 320}, 8);
Polygon fig7 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 10}, new int[]{480, 480, 520, 520, 480, 480, 560, 560}, 8);
Polygon fig8 = new Polygon(new int[]{170, 250, 250, 290, 290, 170}, new int[]{520, 520, 440, 440, 560, 560}, 6);
Polygon fig9 = new Polygon(new int[]{330, 370, 370, 410, 410, 450, 450, 410, 410, 330}, new int[]{520, 520, 480, 480, 440, 440, 520, 520, 560, 560}, 10);
Polygon fig10 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 90, 90, 50, 50, 10}, new int[]{680, 680, 640, 640, 680, 680, 720, 720, 760, 760, 720, 720}, 12);
Polygon fig11 = new Polygon(new int[]{170, 210, 210, 250, 250, 210, 210, 170}, new int[]{640, 640, 600, 600, 760, 760, 680, 680}, 8);
Polygon fig12 = new Polygon(new int[]{330, 410, 410, 370, 370, 290, 290, 330}, new int[]{640, 640, 680, 680, 760, 760, 720, 720}, 8);
polygons.add(fig1);
polygons.add(fig2);
polygons.add(fig3);
polygons.add(fig4);
polygons.add(fig5);
polygons.add(fig6);
polygons.add(fig7);
polygons.add(fig8);
polygons.add(fig9);
polygons.add(fig10);
polygons.add(fig11);
polygons.add(fig12);
Color[] c = new Color[12];
c[0] = new Color(25, 165, 25);
c[1] = new Color(255, 165, 25);
c[2] = new Color(255, 50, 50);
c[3] = new Color(150, 45, 90);
c[4] = new Color(25, 165, 150);
c[5] = new Color(25, 165, 255);
c[6] = new Color(255, 40, 190);
c[7] = new Color(180, 90, 60);
c[8] = new Color(90, 80, 70);
c[9] = new Color(70, 80, 90);
c[10] = new Color(150, 30, 20);
c[11] = new Color(80, 80, 80);
shapePane = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(c[0]); g2.fill(fig1);
g2.setColor(c[1]); g2.fill(fig2);
g2.setColor(c[2]); g2.fill(fig3);
g2.setColor(c[3]); g2.fill(fig4);
g2.setColor(c[4]); g2.fill(fig5);
g2.setColor(c[5]); g2.fill(fig6);
g2.setColor(c[6]); g2.fill(fig7);
g2.setColor(c[7]); g2.fill(fig8);
g2.setColor(c[8]); g2.fill(fig9);
g2.setColor(c[9]); g2.fill(fig10);
g2.setColor(c[10]); g2.fill(fig11);
g2.setColor(c[11]); g2.fill(fig12);
}
};
/*contentPane = this.getContentPane();
contentPane.add(shapePane);
this.pack();*/
frame.add(shapePane);
shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);
/*shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);*/
}
public void mousePressed(MouseEvent e) {
for(Polygon polygon: polygons) {
if (polygon.contains(e.getPoint())) {
System.out.println("Pressed");
currPolygon = polygon;
x = e.getX();
y = e.getY();
}
}
}
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint();
}
}catch (NullPointerException ex){
}
}
public void mouseReleased(MouseEvent e){
currPolygon = null;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}
And the main Pentamino class:
import javax.swing.*;
public class Pentomino extends JFrame {
JFrame frame;
PentominoShape shape;
PentominoPanel panel;
public Pentomino(){
initUI();
}
private void initUI(){
frame = new JFrame("Пентамино");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1500, 900);
setResizable(false);
shape = new PentominoShape(frame);
panel = new PentominoPanel(frame);
/*frame.add(shape);
frame.add(panel);*/
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Pentomino game = new Pentomino();
}
}
I'm new to Java and I don't understand how to solve this problem. Tried searching the internet for a similar problem but couldn't find anything.
Your bug is here:
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint(); // ***** here ****
}
}catch (NullPointerException ex){
}
}
You're calling repaint() on the enclosing class, a JFrame, one that is never displayed, and this will have not have the effect desired.
In fact, ask yourself, why this...
public class PentominoShape extends JFrame // ...
Why is PentominoShape extending JFrame at all, when it isn't behaving as a JFrame, when it shouldn't be behaving as a JFrame?
Instead, call repaint on the JPanel that holds the shapes, the shapePane JPanel, and yes, get rid of catching the NullPointerException. That should never be in your code:
public void mouseDragged(MouseEvent e) {
if (currPolygon == null) {
return;
}
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
shapePane.repaint(); // now we're repainting the correct JPanel!
}
}
Side note: I'd clean things up a bit including
Not having any class extend JFrame if possible
Create my own polygon type of class:
public class PentominoShape2 {
private Polygon polygon;
private Color color;
public PentominoShape2(Polygon polygon, Color color) {
this.polygon = polygon;
this.color = color;
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.fill(polygon);
}
public Polygon getPolygon() {
return polygon;
}
public Color getColor() {
return color;
}
public boolean contains(Point p) {
return polygon.contains(p);
}
}
and then in the drawing JPanel
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (PentominoShape2 poly : polys) {
poly.draw(g);
}
}
same for the mouse listener

Laggy window when displaying images for the first time with JScrollPane

I am trying to display a bunch of ImagePanels in a JScrollPane. The problem with this application is that every time a new row of panels enters the viewport the whole window freezes for a small amount of time. Once scrolled through to the end though, the window will not lag again.
What is the cause of this lagging and how can I prevent it from happening in the first place?
My guess is the panels don't get painted until they are scrolled to. So I tried to call repaint on them after adding them to the background but that didn't work :(
MainClass.java:
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame frame = new MainFrame();
}
});
}
}
MainFrame.java:
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = -254980289568295701L;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel background = new JPanel();
JScrollPane scrollPane = new JScrollPane(background,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ImagePanel ipanel;
background.setPreferredSize(new Dimension(this.getWidth(),
this.getHeight() + 3000));
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.getVerticalScrollBar().setUnitIncrement(20);
add(scrollPane);
for (int i = 0; i < 40; i++) {
ipanel = new ImagePanel();
ipanel.setPreferredSize(new Dimension(300, 400));
ipanel.setSize(new Dimension(300, 400));
background.add(ipanel);
}
setVisible(true);
}
}
ImagePanel.java:
import java.awt.*;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1997246878999790104L;
private Image image;
private int width;
private int height;
public ImagePanel() {
width = 300;
height = 400;
BufferedImage bufferedImage = new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, 2000, 2000);
g2d.setColor(Color.black);
g2d.fillOval(0, 0, 2000, 2000);
g2d.setColor(Color.yellow);
g2d.fillOval(1500, 300, 400, 400);
g2d.fillOval(1200, 600, 400, 400);
g2d.fillOval(900, 900, 400, 400);
g2d.fillOval(600, 1200, 400, 400);
g2d.fillOval(300, 1500, 400, 400);
g2d.dispose();
image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
I changed your code to us a JLabel to display the image.
It takes longer to load, but the scrolling worked fine:
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
//public class ImagePanel extends JPanel {
public class ImagePanel extends JLabel {
private static final long serialVersionUID = 1997246878999790104L;
private Image image;
private int width;
private int height;
public ImagePanel() {
width = 300;
height = 400;
BufferedImage bufferedImage = new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, 2000, 2000);
g2d.setColor(Color.black);
g2d.fillOval(0, 0, 2000, 2000);
g2d.setColor(Color.yellow);
g2d.fillOval(1500, 300, 400, 400);
g2d.fillOval(1200, 600, 400, 400);
g2d.fillOval(900, 900, 400, 400);
g2d.fillOval(600, 1200, 400, 400);
g2d.fillOval(300, 1500, 400, 400);
g2d.dispose();
image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
setIcon( new ImageIcon(image) );
}
// #Override
protected void xxxpaintComponent(Graphics g) {
super.paintComponent(g);
// g.drawImage(image, 0, 0, this);
g.drawImage(image, 0, 0, null);
}
}

Java Swing - Draw graphics inside of a Panel

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());

Objects disappear on resize and when the window is minimized

i need some help with this code.
I already have the functions of paint the objects and clear the objects from the window but how i can make that when i make a resize or when i minimize the windows the objects doesn't disappear?
This is the code i have at the moment:
public class miClass implements ActionListener{
JFrame ventana;
JPanel panel;
JButton p,c;
Graphics g;
Image img;
Font font1,font2,font3;
public miClass(){
ventana = new JFrame("Aplicacion.");
p = new JButton("P");
c = new JButton("C");
panel = new JPanel();
ventana.setLayout(null);
ventana.setBounds(100,100,600,600);
ventana.getContentPane().add(panel);
ventana.add(p);
p.addActionListener(this);
c.addActionListener(this);
p.setBounds(20,20,120,45);
ventana.add(c);
c.setBounds(200,20,120,45);
ventana.setFocusable(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
g = ventana.getGraphics();
Toolkit tool = Toolkit.getDefaultToolkit();
img = tool.getImage("prueba.png");
g.drawImage(img,0,100,null);
font1 = new Font("Helvetica",Font.PLAIN,22);
g.setFont(font1);
g.drawString("Hola", 100, 300);
font2 = new Font("TimesRoman",Font.BOLD,20);
g.setFont(font2);
g.drawString("Mundo", 100, 340);
font3 = new Font("Courier",Font.BOLD+Font.ITALIC,25);
g.setFont(font3);
g.drawString("WASAAAA!", 100, 400);
g.setColor(Color.green);
g.drawOval(300, 200, 150, 100);
g.setColor(Color.red);
g.drawArc(200, 400, 250, 64, 135, 46);
g.setColor(Color.blue);
g.drawLine(400, 200, 150, 100);
g.setColor(Color.magenta);
g.drawRect(300, 250, 160, 50);
g.setColor(Color.cyan);
g.fillRect(100,400,20,240);
g.setColor(Color.lightGray);
g.fillOval(100,340,14,30);
if(e.getSource() == c){ //Clean all objects on the window//
g.clearRect(0,100,900,800);
}
}
public static void main(String args[]){
miClass GUI = new miClass();
}
}
Your frame is reset to its initial state when it is resized, and your code only redraws it when a button is clicked, not when it is resized. The paintComponent method on any subclass of Component is called after the parent frame is resized, so you can fix this issue by overriding that method.
package SO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class miClass implements ActionListener {
JFrame ventana;
JPanel panel;
JButton p, c;
Graphics g;
Image img;
Font font1, font2, font3;
public miClass() {
ventana = new JFrame("Aplicacion.");
p = new JButton("P");
c = new JButton("C");
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
// your stuff
font1 = new Font("Helvetica", Font.PLAIN, 22);
g.setFont(font1);
g.drawString("THIS GETS REDRAWN", 100, 300);
}
};
panel.setSize(400, 400);
ventana.setLayout(null);
ventana.setBounds(100, 100, 600, 600);
ventana.getContentPane().add(panel);
ventana.add(p);
p.addActionListener(this);
c.addActionListener(this);
p.setBounds(20, 20, 120, 45);
ventana.add(c);
c.setBounds(200, 20, 120, 45);
ventana.setFocusable(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
g = panel.getGraphics();
Toolkit tool = Toolkit.getDefaultToolkit();
img = tool.getImage("prueba.png");
g.drawImage(img, 0, 100, null);
font1 = new Font("Helvetica", Font.PLAIN, 22);
g.setFont(font1);
g.drawString("Hola", 100, 300);
font2 = new Font("TimesRoman", Font.BOLD, 20);
g.setFont(font2);
g.drawString("Mundo", 100, 340);
font3 = new Font("Courier", Font.BOLD + Font.ITALIC, 25);
g.setFont(font3);
g.drawString("WASAAAA!", 100, 400);
g.setColor(Color.green);
g.drawOval(300, 200, 150, 100);
g.setColor(Color.red);
g.drawArc(200, 400, 250, 64, 135, 46);
g.setColor(Color.blue);
g.drawLine(400, 200, 150, 100);
g.setColor(Color.magenta);
g.drawRect(300, 250, 160, 50);
g.setColor(Color.cyan);
g.fillRect(100, 400, 20, 240);
g.setColor(Color.lightGray);
g.fillOval(100, 340, 14, 30);
if (e.getSource() == c) { // Clean all objects on the window//
g.clearRect(0, 100, 900, 800);
}
}
public static void main(String args[]) {
miClass GUI = new miClass();
}
}

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