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.

    1 answer 1

    I'm not sure that I understood what you need but draws like this:

     package org.jazzteam.teamtask ; import java.applet.Applet ; import java.awt.* ; import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import javax.swing.JDialog ; import javax.swing.JFrame ; import javax.swing.JOptionPane ; import javax.swing.UIManager ; public class Sd extends JFrame { private class MainApplet extends Applet { @Override public void paint ( final Graphics g ) { super.paint ( g ) ; final 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 ) ; } } public static final Font FONT = new Font ( "Verdana", Font.PLAIN, 11 ) ; public Sd () { super ( "Игровое Поле" ) ; setDefaultCloseOperation ( JFrame.DO_NOTHING_ON_CLOSE ) ; addWindowListener ( new WindowAdapter () { @Override public void windowClosing ( final WindowEvent event ) { final Object[] options = { "Да", "Нет!" } ; final 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 ) ; } } } ) ; add ( new MainApplet () ) ; setPreferredSize ( new Dimension ( 700, 500 ) ) ; pack () ; setLocationRelativeTo ( null ) ; } public static void main ( final 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 ) ; new Sd ().setVisible ( true ) ; } } ) ; } }