Painting JComponent Onto JPanel

May 13, 2014

I am attempting to make some Java2D shapes by wrapping them in subclasses of JComponent and giving them functionality. Unfortunately after defining paintComponent and adding it to the JPanel the shape is not showing up. As in my last thread the code is mostly irrelevant but the perhaps worth a skim. The only methods truely relevant are the paintComponent method in my shape and Sandbox constructor.

Java Code:

public class Sandbox extends JPanel
{
private Timer timer;
private Circle c;
public Sandbox()
{
c = new Circle();
add(c);
setPreferredSize(new Dimension(400, 300));

[Code] ....

View Replies


ADVERTISEMENT

Another Way To Add Keylistener To JComponent?

Jan 13, 2014

I wrote this program that makes a little square move around with the wasd keys. A huge problem that I had while creating it is adding the keylistener to my JFrame. In the end, I ended up creating an anonymous KeyListener class and adding that anonymous class to the JFrame. However, this anonymous class requires two other methods that I don't use, so any other thing I could have done to add these keylisteners to the JFrame without creating an anonymous class? I had to create 4...

Java Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

[Code] .....

View Replies View Related

Who Keeps Track Of Which JComponent Has Focus

Aug 31, 2014

I have two JFrames: frame1 and frame2. frame2 currently has focus, and I want to determine which component of frame1 would have focus if I were to switch focus to frame1, hopefully without actually temporarily changing focus.

Is it the KeyboardFocusManager who keeps track of which element in each frame has focus? Or does each container itself keep track? How does Java figure out which element in a frame gets focus when I switch to that window?

View Replies View Related

Painting 2 Components From Different Classes

Jul 14, 2014

I want to make a simple Table Tennis game. I have 2 drawing classes. 1st should draw oval and 2nd rectangle. But second class, which should draw rectangle doesn't work and if i use it together with oval class, then oval doesn't appear too.

Java Code:

public static void main(String[] args) throws InterruptedException
{
Intro i = new Intro();
Pall pall = new Pall();
Laud laud = new Laud();
i.frameID().add(pall); // this adds oval
i.frameID().add(laud); // this adds rectangle
i.frameID().pack();
i.frameID().setSize(400, 400);

[code]...

View Replies View Related

Tetris Game - Painting A Custom Shape

Dec 10, 2014

I am currently trying to make a Tetris game, so far I've got a class drawing that extends a JPanel as my canvas, a class Square that is 1 square(20 by 20) and a class LineShape that should draw 4 squares above each other. The class Frame way below is merely to set things up.

Currently i'm able to draw objects of my class Square, so I made a class LineShape that combines both and makes a 4Square long line.

My problem is when I try to draw an object of the class LineShape, it shows nothing, removes what is already painted(the square) and gives an error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at LineShape.<init>(Screen.java:96)
at Drawing.paint(Screen.java:79)

I put my classes together so you can copy paste and run, make a new screen to start, to see the problem occur, go to the drawing class's paint method and uncomment these lines:

// LineShape shape = new LineShape(this);
// shape.paint(g);

Below are my classes:

import javax.swing.*;
import java.awt.*;
public class Screen {
private Frame frame;
private Frame inputFrame;
private JPanel panel;

[Code] ....

View Replies View Related

Applets :: Initial Painting - Content Overlap?

Feb 10, 2014

I have 4 small applets on a page that simply prints out the quadratic equation for values passed in. There are 4 applets on a page that have different values assigned. They display fine.... sometimes.... if I refresh or generate new numbers sometimes one applet looks as though it has numbers overlapping the new values. If I resize or drag the browser everything displays fine again - no need to refresh the browser or anything. It's as though old values or something are being overwritten by the new or something similar is happening. What should I include in the paint method of the JPanel that might clear the applet before initial painting happens..... if anything?

View Replies View Related

Swing/AWT/SWT :: Painting Entire Line In Jtable

Apr 1, 2014

I have in my app a JTable. I would like to know how can i do to, when i click in a cell, my jtable paint the entire line of this cell. Here is what i did.

Method call:
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int row = jTable1.rowAtPoint( evt.getPoint() );// Don't know how to send this info through setDefaultRenderer.
jTable1.setDefaultRenderer( Object.class, new RowRender() );

[Code] ....

View Replies View Related

How To Keep Painting Image To Show Smooth Animation

Feb 12, 2015

public void actionPerformed(ActionEvent e)
{
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, WIDTH, HEIGHT);
myBuffer.setColor(Color.red);
for(int x = 17; x < WIDTH; x += 30) //vertical lines

[Code] ....

View Replies View Related

Painting In Swing - Parent And Child Components?

Aug 11, 2014

I keep hearing these two term when it comes to painting in Swing, however, I'm not sure which is which. To my understanding is that the child components are the ones that already exist on screen (could be a JButton, JFrame, or custom painting) . and the parent components are the one to be added/drawn next. (hence, if we override the paintChildren() method when painting, the components that were already on the screen don't appear any more) ....

View Replies View Related

Applet Painting Is Updating Only When Resized Or Re-maximized

Apr 17, 2014

Pretty much the title. Everything was running fine until a moment, when pain updated only once after the first resize. I've seen some worries about th.start(); or repaint(); but I did not manage to track the problem down since I'm a rookie at programming. Here's the Main class.

import java.applet.*;
import java.awt.*;
public class Main extends Applet implements Runnable
{
// variables
Thread th;
Player player;
static Shot [] shots;

[Code] ....

View Replies View Related

Loading JPanel From Another JPanel When Button Is Pressed

Jun 9, 2014

I am making a game in java and i want to make a title screen. What I am trying to do is make a jpanel and load another jpanel when a button is pressed in one of the japenls. Here is the code for the runner

import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Runner extends JFrame {
// Have these set up so they can be seen everywhere
public static final int GAMEWIDTH = 540;
public static final int GAMEHEIGHT = 710;
static boolean loading = false;

[Code] ....

here is the code for the jpanel that loads the other jpanel

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

[Code] ...

How do load a jpanel when a button is pressed in another jpanel

View Replies View Related

Program That Compute Cost Of Painting And Installing Carpet In A Room

Jan 30, 2015

I need to create a code for this asign:

Write a program that computes the cost of painting and installing carpet in a room. Assume that the room has one door, two windows, and one bookshelf. Your program must do the following:a. Prompts the user to enter, in feet, the length, width, and height of a room.

A. Read the dimensions of the room.

b. Prompts the user to enter the width and heights, in feet, of the door, each window, and the bookshelf. Read these quantities.

c. Prompts the user to enter the cost, per square foot, of painting the walls. Read these quantities.

d. Prompts the user to enter of cost, per square foot, of installing carpet. Read these quantities.

e. Output the cost of painting the walls and installing the carpet.

View Replies View Related

NullPointerException Adding JPanel To JPanel

Jun 17, 2014

I have a Main() class that extends JPanel and I'm trying to add Card() that also extends JPanel.When I add a Card() or Main() object to the JFrame directly, it works fine. The problem arises when I try to add a Card() object to the Main() object.

I edited the code a little bit to make it easier to read so hopefully it's fine.

Main() code:
public class Main extends JPanel implements Runnable {
// HARDCODED OPTIONS
public static final int WIN_WIDTH = 1200;
public static final int WIN_HEIGHT = 800;

public static final int GRID_X = 9;
public static final int GRID_Y = 5;

[Code] ...

View Replies View Related

How To Get JPanel To Refresh

Mar 18, 2015

I have tried to get my JPanel to refresh and show a new combo box, labels and fields but I can't get it working with revalidate or repaint.

package Part4; 
import javax.swing.*;
 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 public class AddressGUI extends JFrame implements ActionListener{
 Address address = new Address();

[code]...

View Replies View Related

How To Add Image In JPanel

Mar 18, 2015

how do i add image in JPanel

View Replies View Related

Second JPanel Not Working

Oct 14, 2014

I code a program using Eclipse Keppler with Java 8.0. I made a program with 2 JPanel. The first JPanel is working well. From a button in first JPanel, I excute second JPanel, but It is not working. No error when I compile the code. Here is, I attached the source code.

import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;

[code]....

View Replies View Related

JPanel Won't Show

May 8, 2015

I want my program to show 1st panel and when a person press image label 2nd panel showup which will have 2 tabs "oneway" and "round trip". But rightnow I only see 2nd panel with only 1 tab and don't see 1st panel at all.

import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;

[code]....

View Replies View Related

Fix Dimensions Of JPanel

Aug 27, 2014

I'm trying to fix the length of expenses to a appropriate size on my Jpanel but not sure how.

package mybudgetapp;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

[code]....

View Replies View Related

Can't Draw Very Far In A JPanel

Apr 16, 2015

I have a JPanel embedded in a JScrollPane. I draw in the JPanel, using the paint() method. I have to draw some shapes that can go far in the y direction, that's why I put my JPanel in a scroll pane, in order to scroll down to be able to discover the shapes down. But even though I put my JPanel in a JScrollPane, it soon blocks in the y direction and does not show the shapes far in the y direction.

Note: I used a GridBagConstraints which is correctly set, with its fill attribute etc.

View Replies View Related

How To Add JScrollPane Into JPanel

Jan 16, 2015

As you can see in the above image. I have create One jFrame inside it their is one jPanel. Inside jPanel their are dynamic buttons whose range can change. (its like text box if you add more text scroll bar will appear)
 
I want to add jScrollBarr/jScollPane to jPanel so that when more than 6buttons will be their, I can scroll it down/up. So my question is how should I place jScollPane using this DRAG-DROP tool. When i clicked on jScrollPane and tried to put it inside jPanel I am unable to set it's position. It's my first time to add jScrollPane for jPanel inside jFrame.

View Replies View Related

JPanel Buttons Not Showing Up?

Feb 21, 2015

In my if statement on line 46 & 47 im trying to add the buttons to the screen if the player has lost and the "game over" screen have popped up. But the buttons aren't showing... what am i doing wrong?

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

[code]...

View Replies View Related

JButtons Not Appearing In JPanel

Apr 17, 2014

I want to add a "play" and a "stop" button to stop and play a sound file. Sound file works good, but buttons aren't appearing in the window.

public class SpelaLjud extends JPanel implements ActionListener {
JButton bPlay, bStop;
AudioClip ac;
public SpelaLjud() {
bPlay = new JButton ("PLAY");
bStop = new JButton ("STOP");
add(bPlay);
add(bStop);
bPlay.addActionListener(this);
bStop.addActionListener(this);

[code]....

View Replies View Related

JPanel Is Not Displaying All The Time

Jul 2, 2014

Yes my JPanel is not displaying all the time like you would think it would. Before you beat me up yes I am a bit new I am just working on the GUI part of java over my summer break. Obviously it is not suppose to do that. Secondly, As you can see i am using the alphabet to create JButtons. Shouldnt there be an easier way to use a for( loop) . I tried before but I didn't want to get too side tracked.

Java Code:

import javax.swing.*;
import java.awt.*;
public class hello {
private JFrame alphaframe1;
private JFrame alphaframe2;

[Code] .....

View Replies View Related

Order Buttons In A JPanel

Sep 22, 2014

I'm learning the swing options of Java, and my first exercise was the typical Chat Room. I got everything right: TextField, TextArea, the jpanel at left side and the buttons inside of it. BUT I can't order the 2 buttons (1 north, 1 south). I've already looked for answers, but didn't find anything and I tried to asked the teacher, but she was too lazy to look for the error. Here is my code:

f=new JFrame("Chat Room");
b1=new JButton("Send");
b2=new JButton("Emoticons");
public void showWindow(){
p1=new JPanel();
f.getContentPane().add(p1,BorderLayout.EAST);
p1.add(b1,BorderLayout.NORTH);
p1.add(b2,BorderLayout.SOUTH);
}

View Replies View Related

How To Create Insets For JPanel

May 6, 2014

Is there a way to create insets for the JPanel without using the GridBagLayout, or createEmptyBorder() because the panel already wants to have an etched border?

View Replies View Related

Adding Object To JPanel

Nov 28, 2014

I am making a graph to implement Breadth First Search and Depth First Search. I changed the program because I want each button to be its own node. However I am having an issue placing them into a panel. When I run the program I get the following error:

The method add (Component) in the type Container is not applicable for the arguments (Buttons)

JPanel matrixPan(){
Buttons[][] matrixBtn = new Buttons[25][25];
JPanel panel = new JPanel();
panel.setSize(550,550);
panel.setLayout(new GridLayout(25,25));

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved