Good day.
I have a question of the following nature. There is a class Sd and a class Main. Sd - contains the creation of the frame, and Main draws figures. How to make so that from the class of Main, figures painted on the class Sd, i.e. frame
Help please, I'm still learning Java :)
Main class:
package org.jazzteam.teamtask; import java.applet.*; import java.awt.*; public class Main extends Applet { int width, height; public void paint( Graphics g ) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor( Color.red ); g2d.fillRect( 10, 20, 10, 15 ); g2d.setColor( Color.pink ); g2d.fillRect( 30, 40, 20, 20 ); g2d.setColor( Color.red ); g2d.fillRect( 10, 20, 10, 15 ); g2d.setColor( Color.green ); g2d.fillRect( 40,75, 30, 30 ); g2d.setColor( Color.red ); g2d.fillRect( 10, 20, 10, 15 ); g2d.setColor( Color.blue ); g2d.fillRect( 80, 60, 40, 20 ); } } package org.jazzteam.teamtask; import java.awt.Dimension; import java.awt.Font; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.UIManager; public class Sd extends JFrame { public static final Font FONT = new Font("Verdana", Font.PLAIN, 11); public static void createGUI() { JFrame frame = new JFrame("Игровое Поле"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowListener() { public void windowActivated(WindowEvent event) { } public void windowClosed(WindowEvent event) { } public void windowClosing(WindowEvent event) { Object[] options = { "Да", "Нет!" }; int n = JOptionPane.showOptionDialog(event.getWindow(), "Закрыть окно?", "Подтверждение", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == 0) { event.getWindow().setVisible(false); System.exit(0); } } public void windowDeactivated(WindowEvent event) { } public void windowDeiconified(WindowEvent event) { } public void windowIconified(WindowEvent event) { } public void windowOpened(WindowEvent event) { } }); frame.setPreferredSize(new Dimension(700, 500)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { UIManager.put("Button.font", FONT); UIManager.put("Label.font", FONT); JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); createGUI(); } }); } }
I tried to create an object on the Main class:
private Main main;
and in the run () method, call the method from the Main class:
Sd object = new Sd();
object.main.paint (); - paint is a method from the Main class, it asks to pass parameters, in my case, if I write null, it does not resolve anything, if I write g (the name of an interchange), it gives an error. Help to understand, please.