Problem with displaying text using drawString - java

When I run this program without the "g.drawString ("Play",50,50)" it runs fine. The back ground of the frame is a solid white colour. But when I add that line of code, the back ground of the frame becomes almost transparent. It's like a screen shot of my screen in the 450 by 800 pixel area
import javax.swing.*;
import java.awt.*;
public class NotTicTacToe extends JFrame {
public static void main(String[] args) {
new NotTicTacToe();
}
public NotTicTacToe() {
setTitle("Not Tic Tac Toe");
setSize(450, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
JTextField textfield1= new JTextField ("PLAY",50);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawRect(200, 257, 50, 50);
g.setColor(Color.BLUE);
g.fillRect(201, 258, 49, 49);
g.drawString("Play",50, 50);
}
}

Add super.paint(g); to the start of your paint method. This calls the paint method in the JFrame class that your NotTicTacToe class inherits from.

Related

Trying to draw lines in java over an image i already drew but i can't get it on top of the image?

I have to draw an archery target with two black lines in the innermost circle that forms a cross, but every time i adjust the lines so that the lines are closer to the centre it goes behind the image instead of appearing on top. How can I stop this? Does it need to have a separate set of instructions entirely?
This is my code:
package sumshapes;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class SumShapes extends JFrame
implements ActionListener {
private JPanel panel;
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.drawLine(250, 200, 250, 200);
g.drawOval(140,90,200,200);
g.setColor(Color.BLACK);
g.fillOval(140,90,200,200);
g.drawOval(162,109,155,155);
g.setColor(Color.BLUE);
g.fillOval(162,109,155,155);
g.drawOval(183,129,112,112);
g.setColor(Color.RED);
g.fillOval(183, 129, 112, 112);
g.drawOval(210,153,60,60);
g.setColor(Color.YELLOW);
g.fillOval(210, 153, 60, 60);
g.setColor(Color.BLACK);
}
public static void main(String[] args) {
SumShapes frame = new SumShapes();
frame.setSize(500,400);
frame.setBackground(Color.yellow);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout (new FlowLayout());
}
public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
paper.drawLine(20,80,120,80);
}
}
All your drawing should go into the paintComponent method of a lightweight component, such as a JPanel.
There should never be a need to call getGraphics. If you wish to change the drawing upon a particular action you should a) program the logic into paintComponent b) alter the logic in the Action c) call repaint on the Component
For example:
private JPanel panel = new JPanel(){
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);//call parent method first thing
//paint here
}
#Override
public Dimension getPreferredSize(){//provided so you can size this component as necessary
return new Dimension(500,400);
}
};
....
frame.add(panel);
frame.pack();
As an aside, I'd recommend placing all calls to to Swing components on the EDT - this means wrapping your Swing calls in the main method with SwingUtilities. eg
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable(){
#Override
public void run() {
SumShapes frame = new SumShapes();
....
}
});
}

Java - Using both the GUI and 2Dgraphics

I am trying to use java swing GUI at the same time I am drawing 2Dgraphics for the game I am creating. My goal is to have a drop down over the top of the graphics. Whenever i try to make a button or drop down in swing I cannot see it. This I believe is the 2Dgrahpics covering the swing elements.
This is my class decleration and my JFrame creation:
public class Main extends JFrame{
public Main(){
setBackground(Color.LIGHT_GRAY);
setSize(1000,600);
setTitle("Scaling Tests");
setDefaultCloseOperation(3);
setVisible(true);
This is how I am double buffering and drawing the graphics:
public void paint(Graphics g){
dbImage = createImage(1000, 600);
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this.getWidth(), this.getHeight() , this);
repaint();
}
public void draw(Graphics g){
g.setColor(Color.red);
g.fillRect(100, 100, 200, 200);
I have tried to draw a JLabel like this in the constructor and the draw method:
JLabel mylabel = new JLabel();
mylabel.setText("Hello World!");
mylabel.setBounds(0, 0, 1280, 720);
mylabel.setVisible(true); //unnecessary
this.add(mylabel);
This has not worked. I cannot see the Jlabel no matter where in the code I put it. I think there is some problem with the graphics covering it up.
Your problem is that you are breaking the painting chain because you didn't invoke super.paint() so none of the child components are painted.
However you should NOT override paint() on a JFrame.
Instead, custom painting is done by overriding paintComopnent() on a JPanel and then you add the panel to the frame. And don't forget to invoke super.paintComponent(g) as the first statement in the method.
Then you can add the JLabel to the panel.
Also, never invoked repaint() in a painting method. This will cause an infinite loop.
This works -except the label is all the background you have to use some layout manager to handle it
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame{
public Main(){
setBackground(Color.LIGHT_GRAY);
setSize(1000,600);
setTitle("Scaling Tests");
setDefaultCloseOperation(3);
JLabel mylabel = new JLabel();
mylabel.setText("Hello World!");
mylabel.setBounds(0, 0, 1280, 720);
add(mylabel);
setVisible(true);
}
public static void main(String args[]) {
Main m=new Main();
}
public void paint(Graphics g){
Image dbImage = createImage(1000, 600);
Graphics dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this.getWidth(), this.getHeight() , this);
}
public void draw(Graphics g){
g.setColor(Color.red);
g.fillRect(100, 100, 200, 200);
}
}

Java GUI does not appear

This is for my university project and I am busting my brain around why my Java GUI does not work. This is the situation: the code compiles and executes without an issue.
This code should create 300 X 300 frame center the desktop and create circle and it print my name underneath.
I got it working until the frame, but no circle
package gui;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
public class GUI extends JFrame{
public void Paint (Graphics g){
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(50, 50, 200, 200);
g.setColor(Color.BLACK);
g.drawArc(75, 60, 150, 150, -25, -125);
g.fillOval(100, 100, 25, 25);
g.fillOval(175, 100, 25, 25);
g.setColor(Color.BLUE);
g.setFont(new Font("Serif", Font.BOLD,18));
g.drawString("My Nanme is BOB", 33, 275);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
GUI GUI = new GUI() ;
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setSize(300,300);
GUI.setTitle("BOB's GUI App");
GUI.setVisible(true);
GUI.setLocationRelativeTo(null);
I really appreciate your output. also please give me a hint why it does not work
Java is case-sensitive :
public void Paint (Graphics g)
Will never override
public void paint (Graphics g)
In your main, you have written
GUI GUI = new GUI() ;
You should make the second GUI something else. For example,
GUI gui = new GUI();
Your paint() method should have a lowercase 'P'. As you currently have it, you're not overriding the existing JFrame method.
To avoid such issues in the future, I would investigate the #Overrides annotation. If you use this on the above, it will tell you you're not overriding anything!

Why paintComponent doesn't work?

I'm having issues with this code. For some reason, paintComponent(Graphics g) simply doesn't work and there doesn't seem to be an answer how to force it to. This is my code:
import javax.swing.JFrame;
public class Robotron extends JFrame
{
public Robotron ()
{
//add(this); This one gave me an error
setSize(800, 600);
new TestFrame();
setVisible(true);
}
public static void main(String [ ] args)
{
new Robotron();
}
and this is my TestFrame class that has the paintComponent function:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class TestFrame extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillRect(100, 100, 10, 30);
System.out.println("Abdullah Paint");
}
public TestFrame()
{
setFocusable(true);
repaint();
}
}
What can I do in order to make paintComponent actually play. What I get in the end is just an empty JFrame, with no running of the System.out.println thingy.
Thanks a whole lot, been tackling this for a long time.
You're not adding anything to the JFrame, because of this
//add(this);
Here, you were trying to add the component to itself, which is not going to fly.
Instead, you need to add the TestFrame instance to the Frame.
add(new TestFrame());
setSize(800, 600);
setVisible(true);
You need to add the panel to the frame:
public Robotron ()
{
//add(this); This one gave me an error
setSize(800, 600);
add(new TestFrame());
setVisible(true);
}
As mentioned by others, you need to add the panel to the frame:
public Robotron() {
add(new TestFrame());
setSize(800, 600);
setVisible(true);
}
As not mentioned by others, you need to call super.paintComponent(g) the first thing in your overridden method:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillRect(100, 100, 10, 30);
System.out.println("Abdullah Paint");
}
Notes:
Don't call repaint() in the constructor. At best it does nothing.
Don't call setSize(...) for the frame, instead call pack(). You will need to override the getPreferredSize method in your panel for this.

Simple Java Graphic Program Not Displaying

Basically i'm starting to learn graphics in java so I made a simple program to display two rectangles and a string on the screen. The program compiles fine but does not display the two rectangles or the string. Any input on my problem would be greatly appreciated.
//ClassOne.java
import javax.swing.*;
public class ClassOne {
public static void main(String[] args)
{
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ClassTwo object = new ClassTwo();
f.add(object); //add object to frame
f.setSize(400,250);
f.setVisible(true);
}
}
//ClassTwo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClassTwo extends JPanel {
public void paintComponet(Graphics g) //takes an object from a graphics class
{
super.paintComponent(g);
this.setBackground(Color.BLACK);
g.setColor(Color.WHITE);
g.fillRect(25, 25, 100, 30); //x,y,width, height
g.setColor(new Color(190,81,215));
g.fillRect(25, 70, 100, 30);
g.setColor(Color.RED);
g.drawString("Text", 25, 120);
System.out.print("hi");
}
}
It's
public void paintComponent(Graphics g)
not
public void paintComponet(Graphics g) {
Add the #Override annotation to allow the compiler to check for the method

Categories

Resources