I am trying to display a JComponent inside a JPanel.
I am using null layout because location of the components can be changed during runtime and I have to control them.
But the following code does not work. The JComponent only becomes visible on display if I explicity call the "paintComponent" method, which I think is not good practice.
My JComponent Class
public class MyIcon extends JComponent
{
private double xPos;
private double yPos;
private double radius = 30;
public MyIcon(double xPos, double yPos)
{
this.xPos = xPos;
this.yPos = yPos;
this.setBounds((int)xPos, (int)yPos, (int)radius, (int)radius);
this.setPreferredSize(new Dimension((int)radius, (int)radius ) );
}
public Dimension getPreferredSize()
{
return ( new Dimension( (int)radius, (int)radius ) );
}
public Dimension getMinimumSize()
{
return new Dimension((int)radius, (int)radius);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawOval( (int)xPos, (int)yPos, (int)radius, (int)radius);
g2d.fillOval((int)xPos, (int)yPos, (int)radius, (int)radius);
System.out.println("Icon.paintComponnet() called");
}
}
My Panel Class
public class MyPanel extends JPanel
{
private MyIcon myIcon;
private Graphics2D graphics;
private int width = 700;
private int height = 500;
public MyPanel()
{
myIcon = new MyIcon(20,30);
init();
}
private void init()
{
setLayout(null);
setBackground(Color.WHITE);
setPreferredSize( new Dimension(width, height) );
setBorder(BorderFactory.createLineBorder(Color.BLACK));
add( myIcon );
myIcon.repaint();
}
public void paintComponent(Graphics g)
{
graphics = (Graphics2D) g;
super.paintComponent(g);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
System.out.println("MyPanel.paintComponnet() called");
// why my Icon only gets painted if I explicitly call for it ?
//myIcon.paintComponent(g);
}
}
My Frame Class
public class Editor {
public static void main(String[] args)
{
Editor editor = new Editor();
}
private MyPanel myPanel;
private JFrame frame;
private JToolBar toolBar;
public Editor()
{
myPanel = new MyPanel();
init();
}
private void init()
{
frame = new JFrame("Editor");
Container content = frame.getContentPane();
frame.setSize(800, 600);
frame.setLayout(new BorderLayout(15,15));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(myPanel,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
I'd be use paintIcon() for Class that returns Icon Object
public void paintIcon(Component c, Graphics g, int x, int y) {
for example
Maybe a good approach would be that the Icon class would not extend JComponent but would be just a simple object.
Then, you rename the current Icon.paintComponent you have to something like drawIcon and call the drawIcon method from the MyPanel.paintComponent directly and passing the reference to the Graphics object.
This way you don't have to wonder about using a null layout and you can control the place to display the icon to by just using the Graphics(2d) APIs.
Related
I am trying to write simple code in java for a moving ball program. im new to java, i mean I know the basics one so here it is my code, in case you can help me.I cant move the ball object. I created the class frame which have the gui component and also the ball class with the features of he ball
public class Frame extends JFrame{
private static final int width= 500;
private static final int height=500;
private static final Color cbw= Color.BLACK;
private Ball ball; // the moving object
private DrawCanvas canvas; // the custom drawing canvas
private JPanel btnpanel; // the panel of the buttons
private JPanel mainpanel; // the mainpanel
private JButton button_ML; // move_Left button
private JButton button_MR;// move_Right button
public Frame (){
setProperties();
init();
setUI();
}
private void setProperties() {
setSize(width, height);
setTitle("MOVE THE BALL");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void init(){
btnpanel = new JPanel(new FlowLayout());
mainpanel= new JPanel(new BorderLayout());
button_ML = new JButton("Move left");
button_MR = new JButton("Move right");
//creating the ball with its features
ball= new Ball (30,30,width/2-2,height/2-10,Color.red);
canvas = new DrawCanvas();
// it makes possible the ball to be seen though we have a main panel.
canvas.setPreferredSize(new Dimension(width,height));
button_ML.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
move_Left();
}
});
button_MR.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
move_Right();
}
});
}
private void setUI(){
mainpanel.add(btnpanel, BorderLayout.SOUTH);// adds the button panels to the main panel
btnpanel.add(button_ML);// adds the button to the panel of buttons
btnpanel.add(button_MR);
mainpanel.add(canvas, BorderLayout.CENTER); // adds the canvas to mainpanel
add(mainpanel); // adds the panel in the frame
}
class DrawCanvas extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(cbw); // puts color to the background
ball.paint(g); // the ball paints itself
}
}
private void move_Left(){
int savedX = ball.x;
ball.x -=10;
canvas.repaint(savedX, ball.y, ball.WIDTH, ball.HEIGHT);
canvas.repaint(ball.x,ball.y,ball.WIDTH,ball.HEIGHT);
}
private void move_Right(){
int savedX = ball.x;
ball.x += 10;
canvas.repaint(savedX, ball.y, ball.WIDTH, ball.HEIGHT);
canvas.repaint(ball.x,ball.y,ball.WIDTH,ball.HEIGHT);
}
}
// the ball class
public class Ball extends JFrame {
//variables for the construction of the circle
int x, y; // actual position of the ball
int WIDTH, HEIGHT;// parameters for the size of the rectangle where the circle is posited.
Color color = Color.RED;// the color of the ball
// the constructor of the class
public Ball(int x, int y, int WIDTH, int HEIGHT,Color color) {
this.x = x;//
this.y = y;
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
this.color=color;
}
// method for the color and drawing the ball
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(80,70, 350,350);
}
}
Start by using the values of Ball in it paint methods instead of :
// method for the color and drawing the ball
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(80,70, 350,350);
}
It should look like
// method for the color and drawing the ball
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(x,y, WIDTH, HEIGHT);
}
I cannot seem to get a rectangle to be displayed in the JFrame. Per the parameters of this project, I have to have this class implement the Icon interface. When I run the code as is, I get the JFrame, but nothing shows up inside. It should display a black square. I'm assuming the problem has something to do with how I am initializing the graphics instance variable. I don't have much experience working with GUI graphics, so I'm not entirely clear on how to do this correctly.
And yes, I do know that the getIconWidth and getIconHeight methods are redundant since I am using constants, but I have to have these methods in order to implement the interface.
public class MugDisplay extends JFrame implements Icon {
private int width;
private int height;
private JPanel panel;
private Graphics graphics;
private static final int ICON_WIDTH = 100;
private static final int ICON_HEIGHT = 100;
public MugDisplay() {
this.configureGui();
this.panel = new JPanel();
this.panel.setLayout(new BorderLayout());
this.add(this.panel, BorderLayout.CENTER);
this.graphics = this.getGraphics();
int xPos = (this.panel.getWidth() - this.getIconWidth()) / 2;
int yPos = (this.panel.getHeight() - this.getIconHeight()) / 2;
this.paintIcon(this.panel, this.graphics, xPos, yPos);
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(x, y, ICON_WIDTH, ICON_HEIGHT);
}
#Override
public int getIconWidth() {
return ICON_WIDTH;
}
#Override
public int getIconHeight() {
return ICON_HEIGHT;
}
private void configureGui() {
this.setPreferredSize(new Dimension(600, 600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.pack();
this.setVisible(true);
}
}
In the interest of having a MCVE, here is the driver class that calls this class.
public class Main {
public static void main(String[] args) {
MugDisplay md = new MugDisplay();
md.setVisible(true);
}
}
this.graphics = this.getGraphics() is not how custom painting works in Swing. What you should do is create a panel, override its paintComponent method, make the call to super, and then do your painting. For instance:
panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xPos = ...
paintIcon(this, g, xPos, yPos);
}
}
Calling getGraphics will provide you with a short-lived Graphics object that can soon become invalid, which is why you should opt for overriding paintComponent instead, which will always give you a usable Graphics object. See Performing Custom Painting.
On a separate note, it looks like you're calling setVisible(true) before you're finished adding the necessary components to the JFrame. To ensure that your components show up, call setVisible(true) after adding them all to the frame.
I am creating a simulation type application, I want one background layer and another layer on top for all of the animations. Im currently using JlayeredPanes but i cannot get the background on the top layer to show as transparent so I can see the background, any help is much appreciated, heres the code:
Background layer
public class SimBackground extends JLayeredPane{
private Model theModel;
private SimulationArea simulationArea;
public SimBackground(Model theModel){
this.theModel=theModel;
setBackground(new Color(0, 230, 0));
setOpaque(true);
setPreferredSize(new Dimension(500,500));
setVisible(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for(int x=0;x<50;x++){
for(int y=0;y<50;y++){
g.drawRect((x*10), (y*10), 10, 10);
}
}
}
Top layer
public class SimulationArea extends JLayeredPane {
private int SPEED = 100;
private Model theModel;
Timer timer;
public SimulationArea(Model theModel){
this.theModel = theModel;
setPreferredSize(new Dimension(500,500));
setLocation(0,0);
setOpaque(false);
setBackground(new Color(0,0,0,0));
setVisible(true);
//Swing Timer
timer = new Timer(SPEED,new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
update();
repaint();
revalidate();
}
});
}
private void update() {
theModel.update();
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
//test get 1 active object
ArrayList<ActiveObject> activeObjects = theModel.getActiveObjects();
//System.out.println(activeObjects.size());
for(int i=0; i<activeObjects.size(); i++){
ActiveObject activeObject = theModel.getActiveObjects().get(i);
int x = activeObject.getCoordinates().getX();
int y = activeObject.getCoordinates().getY();
int size = activeObject.getSize();
g2d.fillRect (x ,y , size, size);
}
}
Can someone please tell me what i'm missing here?
Don't use a JLayeredPane, but if you do need to use JLayeredPane in the future, you will want to read the tutorial here since as per my comment you're not using them correctly at all. Instead I recommend that you simplify by doing all drawing in a single JPanel, drawing your background into a BufferedImage perhaps in the constructor, and then drawing that image and your sprites within the JPanel's paintComponent method.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class SimExample extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final Color BKGD_COLOR = new Color(0, 230, 0);
private BufferedImage bkgrnd = new BufferedImage(PREF_W, PREF_H,
BufferedImage.TYPE_INT_ARGB);
public SimExample() {
Graphics2D g = bkgrnd.createGraphics();
g.setBackground(BKGD_COLOR);
g.clearRect(0, 0, PREF_W, PREF_H);
g.setColor(Color.black);
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
g.drawRect((x * 10), (y * 10), 10, 10);
}
}
g.dispose();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (bkgrnd != null) {
g.drawImage(bkgrnd, 0, 0, null);
}
// draw sprites here
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
SimExample mainPanel = new SimExample();
JFrame frame = new JFrame("SimExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
My drawing class: for example I just want to draw a simple line
public class DrawNot1 extends JPanel {
private BasicStroke BS = new BasicStroke(2);
private int x;
private int y;
public DrawNot1(int x, int y){
setSize(100, 100);
this.x = x;
this.y = y;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(BS);
g2d.drawLine(x, y, x, y+10);
}
my JFrame class:
public class Main extends JFrame{
private int x;
private int y;
public Main() {
initUI();
}
public void initUI() {
setSize(600, 500);
setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new DrawNot1(20, 20));
add(new JButton("button1"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Main ex = new Main();
ex.setVisible(true);
}
});
}
}
I want to show my drawing next to the button but that doesn't appear the only component that appear is the button, my drawing does not.
My ultimate goal is when I press the button my drawing is appear near the button.
JFrame uses a BorderLayout by default, adding two components to the default (CENTRE) position means that only the last one added will be shown.
Try adding the button to the SOUTH position instead
add(new JButton("button1"), BorderLayout.SOUTH);
You may also find the overriding the getPreferredSize method of DrawDot1 and providing a suitable value will also result in a better output
Here is my code it is a guy and a background but only one thing shows up at a time or sometimes nothing at all.
I have a brain class, a frame class, a redPlayer class, and a background class.
The way it works is the brain makes a player and a brain and adds it to the frame.
I think it has something to do with layouts but I tried everything but nothing works.
Please Help!!!
Thanks in advance.
here is the brain:
public class Brain
{
private Frame frame;
private static RedPlayer redPlayer;
private Background background;
private SensorKeys sensor;
public Brain()
{
frame = new Frame();
redPlayer = new RedPlayer();
background = new Background();
sensor = new SensorKeys();
frame.addComponent(redPlayer);
frame.addComponent(background);
frame.addKeySensor(sensor);
redPlayer.revalidate();
}
public static void setRedPlayerVelX(double vx)
{
redPlayer.setVelX(vx);
}
public static void setRedPlayerVelY(double vy)
{
redPlayer.setVelY(vy);
}
public static void makeRedPlayerBullet()
{
}
}
`
here is the frame class
public class Frame
{
private JFrame jf;
public Frame()
{
drawFrame();
}
public void drawFrame()
{
jf = new JFrame();
jf.setSize(800, 600);
jf.setLocation(10, 10);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBackground(Color.WHITE);
jf.setLayout(null);
}
public void addComponent(JComponent jc)
{
jc.setBounds(jc.getX(), jc.getY(), 100, 100);
jf.add(jc);
}
public void addPanel(JPanel jp)
{
jf.add(jp);
}
public void addKeySensor(KeyListener kl)
{
jf.addKeyListener(kl);
}
}
here is the player class:
public class RedPlayer extends JComponent implements ActionListener
{
private int x,y;
private double velX = 0, velY = 0;
private Timer timer = new Timer(2,this);
private Image redplayer;
public RedPlayer()
{
x = 100;
y = 100;
ImageIcon II = new ImageIcon("redPlayerRight.png");
redplayer = II.getImage();
revalidate();
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
graphics.drawImage(redplayer, x,y, null);
}
public void actionPerformed(ActionEvent arg0)
{
x += velX;
y += velY;
repaint();
revalidate();
}
public void setVelX(double vx)
{
velX = vx;
}
public void setVelY(double vy)
{
velY = vy;
}
}
And Lastly here is the background class:
public class Background extends JComponent
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
graphics.setColor(Color.green);
graphics.fillRect(0, 400, 500, 200);
}
}
I think it has something to do with layouts
Yes, you should read the Swing tutorial on Using Layout Managers to better understand how they work and for examples.
The default layout manager for a JFrame is a BorderLayout. You can't just add 3 components to the same area of the BorderLayout.
I don't know what you are trying to accomplish but start simple. Since you have a background the basic code should be something like:
redPlayer = new RedPlayer();
background = new Background();
background.add( redPlayer );
frame.addComponent(background);
So the red player should display on top of the background. And the background is added to the frame.
Of course you must use a proper layout manager for the background. And you must make sure you override the getPreferredSize() method when you do custom painting so the layout manager knows what the size of every component should be.
Get those two components working first, then move on to the 3rd component.