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());
Related
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();
}
}
package easteregg;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
}
}
public class EasterEgg {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
my problem is i dont know how to put lines inside the oval
i want to make a easterEgg please help me
i have modified the paint method to draw one pattern please check the below code
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
g.drawLine(120, 40, 140, 60);
g.drawLine(140, 60, 150, 40);
g.drawLine(150, 40, 170, 60);
g.drawLine(170, 60, 190,40);
}
and the output is
this is just a hint to draw lines but better to create one method which will draw that pattern form one end to other end of the oval
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'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