Objects disappear on resize and when the window is minimized - java

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

Related

JPanel paintComponent() won't draw in JFrame

Here I have a JPanel object which handles my 2048 game, but when I try to add it to a JFrame, the paintComponent() doesn't do anything.
I know it's not the implementation of paintComponent() as I've tried drawing very simple things (shown below) and I know the JPanel is working (because it prints out "test").
import javax.swing.JFrame;
public class GameEngine
{
public static void main(String[] args)
{
JFrame frame = new JFrame("2048");
frame.setSize(700, 700);
frame.setLocation(100, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new newGamePanel());
frame.setVisible(true);
}
}
/******Different File******/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class newGamePanel extends JPanel{
Color[] colorOfTiles = new Color[8];
Color gridColor = new Color(187, 173, 160);
Color backTileColor = new Color(204, 192, 180);
Color twoFourText = new Color(238, 228, 218);
Color notTwoFourText = new Color(255,255,255);
int[][] tiles;
public newGamePanel(){
System.out.println("test");
colorOfTiles[1] = new Color(238, 228, 218);colorOfTiles[2] = new
Color(236, 224, 200);
colorOfTiles[3] = new Color(242, 177, 121);colorOfTiles[4] = new
Color(246, 141, 83);
colorOfTiles[5] = new Color(245, 124, 95);colorOfTiles[6] = new
Color(233, 89, 55);
colorOfTiles[7] = new Color(241, 208, 75);
setLayout(new BorderLayout());
setPreferredSize(new Dimension(700, 700));
setFont(new Font("Arial", Font.BOLD, 48));
setFocusable(true);
JPanel subpanel = new JPanel();
subpanel.setLayout(new GridLayout(4,4));
add(subpanel, BorderLayout.CENTER);
tiles = new int[4][4];
}
public void paintComponent(Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, 100 ,100);
}
//The rest of the methods are commented out
}
I expect a black, filled rectangle to be drawn, but nothing appears on the JFrame.

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

JButton on JFrame with Graphics [duplicate]

This question already has answers here:
Components in second JFrame not showing up
(2 answers)
Closed 7 years ago.
I'm trying to make a JFrame that will have graphics and 5 JButtons (which correspond with graphics on screen).
Yesterday my code ran, but now it is glitching and the graphics and buttons only appear when I am resizing the JFrame (pulling it with the cursor!). This is for a game I am making for a class project. If know what the problem may be/is, please tell me. I've been staring at this for so long and I think someone with fresh eyes or more skill than myself could see why the code is wrong. >.<
It has 2 classes, one for graphics and one with a main.
The class for the graphics:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Polygon;
public class drawingComponentMap extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
drawingComponentMap DCM = new drawingComponentMap(); // this is used in
// the class
// below
Color door = new Color(255, 218, 185); // the graphics are making a
// house, hence the colours
Color glass = new Color(173, 216, 230);
Color mapGrass = new Color(144, 238, 144);
g.setColor(mapGrass);
g2.fillRect(0, 0, 500, 500);
// circles to correspond with buttons (as in each button reps a circle)
Color minigame = new Color(221, 160, 221);
g.setColor(minigame);
g2.fillOval(10, 20, 50, 50);
g2.fillOval(220, 70, 50, 50);
g2.fillOval(20, 200, 50, 50);
g2.fillOval(100, 300, 50, 50);
g2.fillOval(200, 200, 50, 50);
Color black = new Color(0, 0, 0);
g.setColor(black);
g2.drawOval(10, 20, 50, 50);
g2.drawOval(220, 70, 50, 50);
g2.drawOval(20, 200, 50, 50);
g2.drawOval(100, 300, 50, 50);
g2.drawOval(200, 200, 50, 50);
// the house graphic
Color walls = new Color(210, 105, 3);
g.setColor(walls);
g2.fillRect(300, 300, 150, 200);
Color roof = new Color(165, 42, 42);
g.setColor(roof);
g2.drawRect(300, 300, 150, 200);
int[] xPoints = { 300, 375, 450 };
int[] yPoints = { 300, 225, 300 };
Polygon imageTriangle = new Polygon(xPoints, yPoints, 3);
g2.fillPolygon(imageTriangle);
g.setColor(black);
g2.drawPolygon(imageTriangle);
g.setColor(glass);
g2.fillRect(380, 350, 50, 50);
g.setColor(door);
g2.fillRect(325, 450, 30, 50);
g.setColor(black);
g2.drawRect(325, 450, 30, 50);
g2.fillOval(330, 470, 5, 5);
g2.drawRect(380, 350, 50, 50);
g2.drawString("Where do you want to go?", 10, 400);
}
}
this is the class which uses the graphics, and where the jframe, jlabel, and jbuttons are made.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Map {
JFrame window = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton();
public static void main(String[] args) {
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(500, 500);
window.setTitle("Map");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JButton button1 = new JButton("World 1");
JButton button2 = new JButton("World 2");
JButton button3 = new JButton("World 3");
JButton button4 = new JButton("World 4");
JButton button5 = new JButton("World 5");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
window.add(panel);
panel.setVisible(true);
drawingComponentMap DCM = new drawingComponentMap();
window.add(DCM);
}
}
Sorry about the formatting of the code/text portion of this question. I'm in class right now.
It's just a single statement issue - window.setVisible(true).
Move your window.setVisible(true) statement to the end. Your main method contents should now look like this (just for your reference):
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(500, 500);
window.setTitle("Map");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("World 1");
JButton button2 = new JButton("World 2");
JButton button3 = new JButton("World 3");
JButton button4 = new JButton("World 4");
JButton button5 = new JButton("World 5");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
window.add(panel);
panel.setVisible(true);
drawingComponentMap DCM = new drawingComponentMap();
window.add(DCM);
window.setVisible(true);

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

I am trying to make a triangle out of a sloop can in Java

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

Categories

Resources