I'm just trying to load in 9 identical .gif's of doges. That's all I wanna do. Please help!
It's for a project of a modified version of tic-tac-toe, and for some reason I can't get the images to appear in my window. Help!
package jerryTacToe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
class picture extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
ImageIcon doge;
public picture() throws IOException
{
doge = new ImageIcon("dogeRotates.gif", null);
JPanel panel = new JPanel();
JLabel label = new JLabel("", doge, JLabel.CENTER);
panel.add(label, BorderLayout.CENTER);
}
public void paintComponent(Graphics g)
{
((Icon) g).paintIcon(this, g, 10, 10);
/* Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Float(100, 100, 250, 260);
g2.draw(line);*/
}
public void changeDoge(boolean human) throws IOException
{
if(human)
doge = new ImageIcon("./dogeIntensifies.gif", null);
else
doge = new ImageIcon("./ferociousDoge.gif", null);
}
}
class Closer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.out.println("much derp");
System.exit(0);
}
}
public class Gui extends JFrame
implements ActionListener, MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
boolean human;
JButton goSecond;
picture centerCenter; picture topLeft; picture topCenter;
picture topRight; picture centerLeft; picture centerRight;
picture bottomLeft; picture bottomCenter; picture bottomRight;
public void mouseClicked(MouseEvent e){
if(e.getSource()==topLeft || e.getSource()==topCenter
||e.getSource()==topRight ||
e.getSource()==centerLeft ||e.getSource()==centerCenter
||e.getSource()==centerRight ||
e.getSource()==bottomLeft ||e.getSource()==bottomCenter
||e.getSource()==bottomRight)
{
/*
* Call the AI here.
*
*
* hello?
* yes, this is doge.
* */
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
}
public Gui() throws IOException
{
addWindowListener( new Closer() );
setTitle("DOGE-TAC-TOE (very0.0.4)");
setVisible(true);
setSize(1600,900);
goSecond=new JButton("wow second");
topLeft=new picture();
topCenter=new picture();
topRight=new picture();
centerLeft=new picture();
centerCenter=new picture();
centerRight=new picture();
bottomLeft=new picture();
bottomCenter=new picture();
bottomRight=new picture();
centerCenter.addMouseListener(this);
topLeft.addMouseListener(this);
topCenter.addMouseListener(this);
topRight.addMouseListener(this);
centerLeft.addMouseListener(this);
centerRight.addMouseListener(this);
bottomRight.addMouseListener(this);
bottomCenter.addMouseListener(this);
bottomLeft.addMouseListener(this);
goSecond.addActionListener(this);
Container stuff=getContentPane();
stuff.setLayout( new BoxLayout(stuff, BoxLayout.PAGE_AXIS) );
JPanel panelTop=new JPanel();
panelTop.setLayout(new GridLayout(1, 3, 300, 100));
panelTop.add(topLeft);
panelTop.add(topCenter);
panelTop.add(topRight);
JPanel panelCenter=new JPanel();
panelCenter.setLayout(new GridLayout(1, 3, 150, 100));
panelCenter.add(centerLeft);
panelCenter.add(centerCenter);
panelCenter.add(centerRight);
JPanel panelBottom=new JPanel();
panelBottom.setLayout(new GridLayout(1, 3, 300, 100));
panelBottom.add(bottomLeft);
panelBottom.add(bottomCenter);
panelBottom.add(bottomRight);
stuff.add(goSecond);
stuff.add(panelTop);
stuff.add(panelCenter);
stuff.add(panelBottom);
}
public static void main(String [] args)throws IOException
{
JFrame it=new Gui();
}
}
You are accessing images in different ways as shown below:
doge = new ImageIcon("dogeRotates.gif", null);
doge = new ImageIcon("./dogeIntensifies.gif", null);
Please check the image placement again.
If you are facing problem in accessing the images then have a look at my post
How to retrieve image from project folder?
Why don't you use simply GridLayout in this game of tic-tac-toe?
Find sample codes below
Java TicTacToe game winCondition?
Tic Tac Toe java
Related
I just started using Intelli J Idea and one of my first projects is to plot some geometric forms to a JPanel of a GUI defined in a form. In the end I want to plot some graphs. I found a tutorial where a class extending the JPanel was defined and the paintCompontent() method was overloaded.
public class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int y2 = (int)(40 * Math.random());
Line2D line = new Line2D.Double(10, 10, 60, y2);
Rectangle2D rectangle = new Rectangle2D.Double(200, 120, 70, 30);
Ellipse2D oval = new Ellipse2D.Double(400, 200, 40, 60);
g2.draw(line);
g2.setPaint(Color.RED);
g2.fill(rectangle);
g2.setPaint(Color.ORANGE);
g2.fill(oval);
}
}
This would run fine if I use it together with this code:
public class MainClass {
public static void main(String[] args) {
MyPanel s = new MyPanel();
JFrame f = new JFrame();
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
}
}
Then I tried combining this with a form I created using Intelli J Idea. And this is where I have problems. I would like to have a form with a button and a JPanel. When I press the button some geometric figures are being drawn on the JPanel defined in the form. I think my best try is like this:
public class MainWindow {
private JPanel panelMain;
private JButton buttonCalculate;
private JPanel panelPlot;
public MainWindow() {
buttonCalculate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panelPlot = new MyPanel();
panelPlot.setBackground(Color.CYAN);
panelPlot.setSize(200, 200);
panelPlot.setVisible(true);
}
});
}
public static void main(String[] args) {
JFrame f = new JFrame("MyFirstGraphTool");
f.setContentPane(new MainWindow().panelMain);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setVisible(true);
}
}
But simply saving my derived JPlane object to the bound property does not change anything.
And also the setBackgroundColor() method does not change anything.
Do you know any tutorials or more detailed explanation of how this can be done?
EDIT: Please find below an image of the component tree.
Component tree from Intelli J Idea
Thanks and kind regards,
David
You've made lots of mistakes in your code. I try to explain you, what's wrong.
public class MainWindow {
private JPanel panelMain; // panelMain is not initialized, so when you try to add it to any window/panel, you'll get a NullPointerException
private JButton buttonCalculate; // same as before. Also this button is not added to any container (window/panel)
private JPanel panelPlot; // panel is not added to any container
public MainWindow() {
buttonCalculate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panelPlot = new MyPanel();
panelPlot.setBackground(Color.CYAN);
panelPlot.setSize(200, 200); // this code will not be honored because the layout manager will recalculate panel bounds.
// use setPreferredSize instead.
panelPlot.setVisible(true);
}
});
}
public static void main(String[] args) {
JFrame f = new JFrame("MyFirstGraphTool");
f.setContentPane(new MainWindow().panelMain);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setVisible(true);
}
}
Here is the correct version of your class
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* <code>MainWindow</code>.
*/
public class MainWindow {
private JPanel panelMain = new JPanel();
private JButton buttonCalculate = new JButton("Calculate");
private JPanel panelPlot; // panel is not added to any container
public MainWindow() {
buttonCalculate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panelPlot = new MyPanel();
panelPlot.setOpaque(true);
panelPlot.setBackground(Color.CYAN);
panelPlot.setPreferredSize(new Dimension(200, 200));
panelMain.add(panelPlot);
panelMain.revalidate(); // cause layout manager to recalculate component bounds
}
});
panelMain.add(buttonCalculate);
}
public static void main(String[] args) {
JFrame f = new JFrame("MyFirstGraphTool");
f.setContentPane(new MainWindow().panelMain);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 400);
f.setVisible(true);
}
static class MyPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int y2 = (int) (40 * Math.random());
Line2D line = new Line2D.Double(10, 10, 60, y2);
Rectangle2D rectangle = new Rectangle2D.Double(200, 120, 70, 30);
Ellipse2D oval = new Ellipse2D.Double(400, 200, 40, 60);
g2.draw(line);
g2.setPaint(Color.RED);
g2.fill(rectangle);
g2.setPaint(Color.ORANGE);
g2.fill(oval);
}
}
}
Please also read about layout managers in Swing
How to check these kinds of problem. When i checked the menus i have for a desktop application, some shows proper display of tooltip for close button which should always be at the front. But some are displayed at the back of the modal dialog.
Screenshot of the bug:
I kind of have the same problems as the one who posted this: https://coderanch.com/t/460688/java/Glasspanes-tooltips
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class GlassPaneContent extends JPanel {
GlassPaneContent() {
setSize(200, 50);
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
JButton button = new JButton("A button");
button.setToolTipText("A tooltip");
add(button);
}
}
class GlassPane extends JPanel {
private static final Color BG_COLOR = new Color(0, 0, 0, 96);
private GlassPaneContent content = new GlassPaneContent();
public GlassPane() {
setLayout(null);
setOpaque(false);
add(content);
}
#Override
protected void paintComponent(Graphics g) {
g.setColor(BG_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());
int x = (getWidth() - content.getWidth()) / 2;
int y = (getHeight() - content.getHeight()) / 2;
content.setLocation(x, y);
super.paintComponent(g);
}
}
public class MainWindow extends JFrame {
public MainWindow() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
GlassPane gp = new GlassPane();
getRootPane().setGlassPane(gp);
gp.setVisible(true);
}
public static void main(String[] args) {
new MainWindow().setVisible(true);
}
}
We're using JAVA Swing. Kindly comment below if ever i need to post the codes. Thank you!
Try out this one:
public class MainWindow extends JFrame {
public MainWindow() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
GlassPane gp = new GlassPane();
setContentPane(gp);
this.setVisible(true);
}
public static void main(String[] args) {
new MainWindow();
}
}
I have a scrollpane where load an image. I wont this image with her natural size, and if this image is too big, I wont activated the scrollbar, but this instruction
g.drawImage(immagine, 0, 0, getWidth(), getHeight(), this);
scaled image for placing in scrollpane. What can I do?
Class Gui:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Gui implements ActionListener {
private JFrame frmEditor;
private Mappa content;
private JMenuItem mntmSfondo;
private JScrollPane scrollabile;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui window = new Gui();
window.frmEditor.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Gui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmEditor = new JFrame();
frmEditor.setFont(UIManager.getFont("TextArea.font"));
frmEditor.setBounds(50, 50, 1024, 768);
frmEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEditor.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panelTile = new JPanel();
panelTile.setLayout(new BorderLayout(0, 0));
content = new Mappa(null);
content.setMinimumSize(new Dimension(150, 150));
scrollabile = new JScrollPane(content);
frmEditor.getContentPane().add(scrollabile, BorderLayout.CENTER);
inizializzaMenu();
}
/**
* Initialize the menu.
*/
private void inizializzaMenu() {
JMenuBar menuBar = new JMenuBar();
frmEditor.setJMenuBar(menuBar);
JMenu mnAltro = new JMenu("Modify");
menuBar.add(mnAltro);
mntmSfondo = new JMenuItem("Load Background");
mntmSfondo.addActionListener(this);
mnAltro.add(mntmSfondo);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == mntmSfondo) {
JFileChooser fc = new JFileChooser("tuttiSfondi");
int result = fc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
content.setImage(file);
//content = new Mappa(file);
//scrollabile.setViewportView(content);
} catch (Exception ex) {
}
}
if (result == JFileChooser.CANCEL_OPTION) {
}
}
}
}
Class Mappa:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Mappa extends JPanel {
BufferedImage immagine;
public Mappa(File fileImmagine) {
if (fileImmagine != null ) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(fileImmagine.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
this.immagine = img;
}
repaint();
}
public void setImage(File file) throws IOException {
this.immagine = ImageIO.read(file);
String name = file.getPath();
System.out.println(name);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 4000, 4000);
g.drawImage(this.immagine, 0, 0, getWidth(), getHeight(), this);
System.out.println("Called Repaint() on Mappa");
}
}
JScrollPane, or more to the point JViewport will use the component's (or in this case the "view's") preferred size as a bases for determining how big the view should be.
When the view expands beyond the size of the scroll pane, it will show the scroll bars.
So basically, you need to override the getPreferredSize of the public class Mappa extends JPanel { panel, for example
public class Mappa extends JPanel {
//...
public Dimension getPreferredSize() {
return immagine == null ? new Dimension(200, 200) : new Dimension(immagine.getWidth(), immagine.getHeight());
}
//...
}
This will encourage the JViewport to always be the same size as the image.
Also, two things...
First, you shouldn't rely on magic numbers, for example
g.clearRect(0, 0, 4000, 4000);
Should be more like...
g.clearRect(0, 0, getWidth(), getHeight());
And secondly,
super.paintComponent(g);
Will do this any way, so calling clearRect is kind of pointless...
You might also like to take a look at Scrollable, but it is quite an advanced topic
I wont this image with her natural size, and if this image is too
big, I wont activated the scrollbar,
Using JLabel to contain the image and wrap it in a JScrollPane should easily achieve what you want. Take hints from the following example:
class AFrame extends JFrame
{
public AFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Image view Demo with JScrollPane");
ImageIcon image = new ImageIcon("myImage.png"); // pass the file location of an image
JLabel label = new JLabel(image);
JScrollPane scrollPane = new JScrollPane(label);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane, BorderLayout.CENTER);
pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new AFrame().setVisible(true);
}
});
}
}
I have a following code ( trying to learn swing and java). I created a ladder using rectangular components using class and placed on the main frame. Everything works okay but if I resize it even slightly, the ShapeManager object (i.e, the ladder) disappears. I don't know what is going on. Any help please.
GUIMain Class:
package mainProg;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.*;
public class GUIMain {
static JPanel mainPanel;
static JButton[] newButtons;
static ShapeManager newShape;
private static class BtnEvtHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//System.exit(0);
JOptionPane.showMessageDialog( null, "WELCOME" );
}
}
private static JButton[] createButtons() {
JButton[] buttonArray= new JButton[2];
buttonArray[0]=new JButton("OK");
buttonArray[1]=new JButton("MOVE");
BtnEvtHandler okButtonHandler= new BtnEvtHandler();
( buttonArray[0]).addActionListener(okButtonHandler);
return buttonArray;
}
private static ShapeManager createShape(int x) {
ShapeManager newContent=new ShapeManager(x);
return newContent;
}
private static JPanel mainContainer() {
JPanel mainPanel= new JPanel();
mainPanel.setSize(400, 400);
return mainPanel;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame(" DB ");
mainPanel= mainContainer();
mainPanel.setLayout(new BorderLayout(10, 10));
newButtons= createButtons();
newShape= createShape(20);
newButtons[0].setHorizontalAlignment(0);
mainPanel.add(newButtons[0],BorderLayout.PAGE_START);
newButtons[1].setHorizontalAlignment(0);
mainPanel.add(newButtons[1],BorderLayout.PAGE_END);
newShape.setPreferredSize(new Dimension(400, 400));
mainPanel.add(newShape, BorderLayout.LINE_END);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(500,200);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
ShapeManager Class:
package mainProg;
import javax.swing.JPanel;
import java.awt.*;
#SuppressWarnings("serial")
class ShapeManager extends JPanel {
int rectPos;
ShapeManager(int rectPos) {
setPreferredSize(new Dimension(400,400));
this.rectPos=rectPos;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
while (rectPos<150) {
g.setColor(Color.BLUE);
g.drawRect(rectPos+10, rectPos+10, 100, 10);
g.fillRect(rectPos+10, rectPos+10, 100, 10);
rectPos=rectPos+10;
}
}
}
You never reset rectangle position, so after the first paint it remains above 150. You need to reset it after you exit your while loop.
Try this:
g.setColor(Color.BLUE);
int position = rectPos;
while (position<150) {
position += 10;
g.drawRect(position, position, 100, 10);
g.fillRect(position, position, 100, 10);
}
I've hit a wall (in my brain) trying to update my board on button presses. Am I right in thinking that the GameBoard class is the one that needs to be repaint()ed?
GameBoard.java
public class GameBoard extends Panel {
static Compass compass = new Compass();
private static final long serialVersionUID = 1;
Graphics2D g2d;
static final Dimension WINDOW_SIZE = new Dimension(1150, 800);
public void boardMaker() throws Exception {
JFrame frame = new JFrame("Display image");
JPanel panel = new JPanel();
/* unimportant stuff
.....
*/
//
DieRoll roll = new DieRoll("Roll Dies");
roll.setC(compass);
roll.setG2D(g2d);
//
Button button = new Button("new");
button.setGameBoard(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
buttonPanel.add(roll);
buttonPanel.setPreferredSize(new Dimension(200,100));
frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
//
frame.getContentPane().add(panel);
frame.setVisible(true);
}
public void paint(Graphics g) {
// not important I think
}
}
Button.java
public class Button extends JButton implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JFrame frame = new JFrame();
Compass c = new Compass();
GameBoard gb = new GameBoard();
Button(String text) {
this.setText(text);
this.addActionListener(this);
}
void setGameBoard(GameBoard gb) {
this.gb = gb;
}
#Override
public void actionPerformed(ActionEvent e) {
gb.g2d.setColor(Color.black);
gb.g2d.fillRect(100, 100, 100, 200);
gb.repaint();
}
}
This gives a null pointer exception. So any idea how to repaint my GameBoard? I'm not mad if I've to rewrite everything because of stupidity! ;)
Thanks
You have the wrong idea about how to draw in Java. Components like Panels draw themselves, and all drawing takes place on the UI thread.
Check out this tutorial: docs.oracle.com/javase/tutorial/2d/index.html
The article Painting in AWT and Swing may offer some perspective on application-triggered painting. The example below illustrates the principle. Note that setForeground() calls repaint() automatically because the foreground color is a bound property, but you can always call it yourself.
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SwingPaint {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
final GamePanel gp = new GamePanel();
f.add(gp);
f.add(new JButton(new AbstractAction("Update") {
#Override
public void actionPerformed(ActionEvent e) {
gp.update();
}
}), BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
private static class GamePanel extends JPanel {
private static final Random r = new Random();
public GamePanel() {
this.setForeground(new Color(r.nextInt()));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
public void update() {
this.setForeground(new Color(r.nextInt()));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = this.getSize();
int d = Math.min(size.width, size.height) - 10;
int x = (size.width - d) / 2;
int y = (size.height - d) / 2;
g.fillOval(x, y, d, d);
g.setColor(Color.blue);
g.drawOval(x, y, d, d);
}
}
}