How to add a string using a separate class?












0















I'm coding a game with score. I need to make a new class called "score" that can add text to my original class, this is the class.



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Frame {

JFrame frame;

Frame()
{
frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setLayout(null);

ImageIcon img = new ImageIcon("Blocks.png");
frame.setIconImage(img.getImage());
frame.getContentPane().setBackground(Color.blue);
frame.setResizable(false);
frame.setContentPane(new MainPanel());
frame.setVisible(true);

JOptionPane.showMessageDialog(null, " Press Ok To
Start","Start", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String args)
{
new Frame();
}
}

class MainPanel extends JPanel
{
MainPanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.red);
g.drawString("Score:", 325, 35);
Image image = Toolkit.getDefaultToolkit().getImage("Blocks.png");
g.drawImage(image, 420, 500, this);
g.drawRect(10, 10, 300, 500 );
}
}


Give some help. I've tried using extensions but that won't show up
and if I try doing it in the frame class it removes everything else and I want it in a different class so I can use code to change the string inside the class.



I've tried this for the Score class



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Score {
}

class ScorePanel extends JPanel
{
ScorePanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.white);
g.drawString("0000", 340, 35);
}
}


If there's something I have to change please tell me.










share|improve this question

























  • You may want to take a look at this: How to create a POJO?.

    – prasad_
    Dec 28 '18 at 6:41











  • No not what I need.

    – Deco
    Dec 28 '18 at 21:28











  • I need to use paintcomponent to add a string to the JFrame from the other class

    – Deco
    Dec 29 '18 at 2:16











  • Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

    – prasad_
    Dec 29 '18 at 6:45











  • So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

    – Deco
    Dec 29 '18 at 21:00
















0















I'm coding a game with score. I need to make a new class called "score" that can add text to my original class, this is the class.



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Frame {

JFrame frame;

Frame()
{
frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setLayout(null);

ImageIcon img = new ImageIcon("Blocks.png");
frame.setIconImage(img.getImage());
frame.getContentPane().setBackground(Color.blue);
frame.setResizable(false);
frame.setContentPane(new MainPanel());
frame.setVisible(true);

JOptionPane.showMessageDialog(null, " Press Ok To
Start","Start", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String args)
{
new Frame();
}
}

class MainPanel extends JPanel
{
MainPanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.red);
g.drawString("Score:", 325, 35);
Image image = Toolkit.getDefaultToolkit().getImage("Blocks.png");
g.drawImage(image, 420, 500, this);
g.drawRect(10, 10, 300, 500 );
}
}


Give some help. I've tried using extensions but that won't show up
and if I try doing it in the frame class it removes everything else and I want it in a different class so I can use code to change the string inside the class.



I've tried this for the Score class



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Score {
}

class ScorePanel extends JPanel
{
ScorePanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.white);
g.drawString("0000", 340, 35);
}
}


If there's something I have to change please tell me.










share|improve this question

























  • You may want to take a look at this: How to create a POJO?.

    – prasad_
    Dec 28 '18 at 6:41











  • No not what I need.

    – Deco
    Dec 28 '18 at 21:28











  • I need to use paintcomponent to add a string to the JFrame from the other class

    – Deco
    Dec 29 '18 at 2:16











  • Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

    – prasad_
    Dec 29 '18 at 6:45











  • So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

    – Deco
    Dec 29 '18 at 21:00














0












0








0


0






I'm coding a game with score. I need to make a new class called "score" that can add text to my original class, this is the class.



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Frame {

JFrame frame;

Frame()
{
frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setLayout(null);

ImageIcon img = new ImageIcon("Blocks.png");
frame.setIconImage(img.getImage());
frame.getContentPane().setBackground(Color.blue);
frame.setResizable(false);
frame.setContentPane(new MainPanel());
frame.setVisible(true);

JOptionPane.showMessageDialog(null, " Press Ok To
Start","Start", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String args)
{
new Frame();
}
}

class MainPanel extends JPanel
{
MainPanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.red);
g.drawString("Score:", 325, 35);
Image image = Toolkit.getDefaultToolkit().getImage("Blocks.png");
g.drawImage(image, 420, 500, this);
g.drawRect(10, 10, 300, 500 );
}
}


Give some help. I've tried using extensions but that won't show up
and if I try doing it in the frame class it removes everything else and I want it in a different class so I can use code to change the string inside the class.



I've tried this for the Score class



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Score {
}

class ScorePanel extends JPanel
{
ScorePanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.white);
g.drawString("0000", 340, 35);
}
}


If there's something I have to change please tell me.










share|improve this question
















I'm coding a game with score. I need to make a new class called "score" that can add text to my original class, this is the class.



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Frame {

JFrame frame;

Frame()
{
frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setLayout(null);

ImageIcon img = new ImageIcon("Blocks.png");
frame.setIconImage(img.getImage());
frame.getContentPane().setBackground(Color.blue);
frame.setResizable(false);
frame.setContentPane(new MainPanel());
frame.setVisible(true);

JOptionPane.showMessageDialog(null, " Press Ok To
Start","Start", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String args)
{
new Frame();
}
}

class MainPanel extends JPanel
{
MainPanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.red);
g.drawString("Score:", 325, 35);
Image image = Toolkit.getDefaultToolkit().getImage("Blocks.png");
g.drawImage(image, 420, 500, this);
g.drawRect(10, 10, 300, 500 );
}
}


Give some help. I've tried using extensions but that won't show up
and if I try doing it in the frame class it removes everything else and I want it in a different class so I can use code to change the string inside the class.



I've tried this for the Score class



import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;

public class Score {
}

class ScorePanel extends JPanel
{
ScorePanel()
{
setOpaque(true);
setBackground(Color.blue);
}

protected void paintComponent(Graphics g)
{
paintComponent(g);
g.setFont(g.getFont().deriveFont(27.0F));
g.setColor(Color.white);
g.drawString("0000", 340, 35);
}
}


If there's something I have to change please tell me.







java swing class pojo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 1:22







Deco

















asked Dec 28 '18 at 6:28









DecoDeco

33




33













  • You may want to take a look at this: How to create a POJO?.

    – prasad_
    Dec 28 '18 at 6:41











  • No not what I need.

    – Deco
    Dec 28 '18 at 21:28











  • I need to use paintcomponent to add a string to the JFrame from the other class

    – Deco
    Dec 29 '18 at 2:16











  • Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

    – prasad_
    Dec 29 '18 at 6:45











  • So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

    – Deco
    Dec 29 '18 at 21:00



















  • You may want to take a look at this: How to create a POJO?.

    – prasad_
    Dec 28 '18 at 6:41











  • No not what I need.

    – Deco
    Dec 28 '18 at 21:28











  • I need to use paintcomponent to add a string to the JFrame from the other class

    – Deco
    Dec 29 '18 at 2:16











  • Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

    – prasad_
    Dec 29 '18 at 6:45











  • So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

    – Deco
    Dec 29 '18 at 21:00

















You may want to take a look at this: How to create a POJO?.

– prasad_
Dec 28 '18 at 6:41





You may want to take a look at this: How to create a POJO?.

– prasad_
Dec 28 '18 at 6:41













No not what I need.

– Deco
Dec 28 '18 at 21:28





No not what I need.

– Deco
Dec 28 '18 at 21:28













I need to use paintcomponent to add a string to the JFrame from the other class

– Deco
Dec 29 '18 at 2:16





I need to use paintcomponent to add a string to the JFrame from the other class

– Deco
Dec 29 '18 at 2:16













Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

– prasad_
Dec 29 '18 at 6:45





Can you explain little bit about what is that "other class" is from where you want to get the string from and which paintComponent you are taliking about? And, your Score class has nothing in it.

– prasad_
Dec 29 '18 at 6:45













So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

– Deco
Dec 29 '18 at 21:00





So I'm using java.awt.Graphics to add in a string (see near end) to put in the score class so I can change what the score is. And I want it to display 0000 on my JFrame in my first class but it won't when I try. I need to get it to show up and I've tried extending the class for the JFrame but it wont work.

– Deco
Dec 29 '18 at 21:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53954538%2fhow-to-add-a-string-using-a-separate-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53954538%2fhow-to-add-a-string-using-a-separate-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas